$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>customization - wp_trim_words() does not trim the_content() in WordPress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

customization - wp_trim_words() does not trim the_content() in WordPress

matteradmin8PV0评论

In my WordPress website, I want to show a short text of each blog_post on the custom blog template. I want to show a Read More button with a link to the post at the end so a user can click on that link and view the full post.

But I am always getting the whole post instead of its summary.

Here is my code:

$moreLink = '<a href="' . the_permalink() . '"> Read More...</a>';
$wp_query = new WP_Query();
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php echo wp_trim_words( the_content(), 55, $moreLink); ?>
<?php endwhile; ?>

I have tried different codes instead of wp_trim_words(the_content(), 55, $moreLink); found online with no luck. Even I have used the same line of code on another custom template and it is working fine. However, with this template, it is not working.

Am I doing any mistake in my code?

In my WordPress website, I want to show a short text of each blog_post on the custom blog template. I want to show a Read More button with a link to the post at the end so a user can click on that link and view the full post.

But I am always getting the whole post instead of its summary.

Here is my code:

$moreLink = '<a href="' . the_permalink() . '"> Read More...</a>';
$wp_query = new WP_Query();
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php echo wp_trim_words( the_content(), 55, $moreLink); ?>
<?php endwhile; ?>

I have tried different codes instead of wp_trim_words(the_content(), 55, $moreLink); found online with no luck. Even I have used the same line of code on another custom template and it is working fine. However, with this template, it is not working.

Am I doing any mistake in my code?

Share Improve this question asked Mar 8, 2019 at 8:01 teccraftteccraft 451 gold badge1 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 11

The problem lies in this line:

<?php echo wp_trim_words( the_content(), 55, $moreLink); ?>

You call the_content function in there. This function prints all the content and doesn't return anything. It means that you print the content, and then pass an empty string to the wp_trim_words.

It should be:

<?php echo wp_trim_words( get_the_content(), 55, $moreLink); ?>

Be careful because, as described in codex, get_the_content() does not pass the content through the 'the_content' filter. This means it won't execute shortcodes. If you want to get exactly what the_content() prints, you have to use

<?php
$my_content = apply_filters( 'the_content', get_the_content() );
echo wp_trim_words( $my_content, 55, $moreLink);
?>

I suggest to also use wp_strip_all_tags(), otherwise you may have problems with open tags that have been trimmed.

Full example:

<?php
$my_content = apply_filters( 'the_content', get_the_content() );
$my_content = wp_strip_all_tags($my_content);
echo wp_trim_words( $my_content, 55, $moreLink);
?>
Post a comment

comment list (0)

  1. No comments so far