Search code examples
wordpressmultibyte

How to code custom excerpt function using mb_substr to return 140 multibyte characters in WordPress?


I am not quite sure what I am doing, but found this code on-line for a custom excerpt as I am trying to return 140 multibyte Japanese characters on a custom front page in WordPress. It may be that I do not have the strip_tags function. If so, can someone please tell me what I need to do to get this to work? Thank you.

front.php -
<?php
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=5');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php echo winexcerpt(140); ?></a></li>
<?php endwhile; ?>
</ul>


functions.php - 
function winexcerpt( $length ) {
   global $post;
   $content = mb_substr(strip_tags($post-&gt;post_content),0,$length);
   return $content;
}

Solution

  • strip_tags has been built into PHP since version 4, so it's very unlikely that this is the issue. (That said, if you don't have multibyte string support enabled, the mb_substr will fail.)

    However, it looks like there's been an error when you've copy/pasted the code - the "-&gt;" part within the strip_tags line should be a "->".

    i.e.: Within the winexcerpt function, the line should be:

    $content = mb_substr(strip_tags($post->post_content),0,$length);
    

    As such, it might be as simple as fixing that error.