The code snippets provided are used to limit the amount of content displayed on a page when displaying blog posts in WordPress. The first code snippet uses the wp_trim_words() function to trim the content of a post down to a specific number of words, while the second code snippet uses the substr() function in conjunction with the strip_tags() function to trim the content down to a specific number of characters. These snippets can be used to control the amount of content displayed on a page, whether it be to reduce clutter, save space, or to make the page more visually appealing. These can be inserted in the desired template file of your WordPress theme, to control the post length.

1. Words Length:

This code snippet is used to limit the length of a blog post’s content when it’s displayed on a page. Specifically, it’s intended to be used within the WordPress loop, which is used to display posts on a page.

Remove  the <?php the_content();?>  insert the below code

 <?php $content = get_the_content();
  $trimmed_content = wp_trim_words( $content, 60, '<a href="'. get_permalink() .'">...[ read more ]</a>' ); ?>
  <p><?php echo $trimmed_content; ?></p>
(Change the 60 to your desire words length)

The first line of code, <?php $content = get_the_content(); ?>, retrieves the full content of the post currently being processed in the loop. The function get_the_content() is a built-in WordPress function that retrieves the content of the current post.

The next line, $trimmed_content = wp_trim_words( $content, 60, '<a href="'. get_permalink() .'">...[ read more ]</a>' );, uses the wp_trim_words() function to trim the content down to a specific number of words. The first argument is the content you want to trim, in this case, the content of the post. The second argument is the number of words that you want to display on the page, in this case, it’s set to 60 words. The third argument is a string that will be appended to the trimmed content, which in this case is a link to the full post with the text “[ read more ]”.

Finally, the code uses the echo function to print the trimmed content to the page, wrapped in a <p> tag, <p><?php echo $trimmed_content; ?></p>. The 60 in the wp_trim_words( $content, 60, '<a href="'. get_permalink() .'">...[ read more ]</a>' ); is the number of words you want to show on the page, this number can be changed to your desire words length.

2. Character Length :

This code snippet is used to limit the number of characters displayed in a blog post. Specifically, it’s intended to be used within the WordPress loop, which is used to display posts on a page.

Remove  the <?php the_content();?>  insert the below code

<?php echo substr(strip_tags($post->post_content), 0, 46);?>
//(Change the 46 to your desire string length)

The first function strip_tags($post->post_content) is removing all HTML tags from the post’s content and this is assigned to substr function which takes the content and trims it to a specific number of characters. In this case, it’s set to 46 characters. This allows you to limit the content displayed on a page based on the number of characters, rather than the number of words, giving you a different way of controlling the amount of content displayed.

Then echo function is used to print the trimmed content to the page. The 46 in the substr(strip_tags($post->post_content), 0, 46); is the number of characters you want to show on the page, this number can be changed to your desire string length.

Conclusion:

In conclusion, controlling the amount of content displayed on a page can be an important consideration for any website, particularly when displaying blog posts. The two code snippets provided are useful tools for controlling the amount of content displayed on a page in WordPress.

If you’re looking to take your WordPress project to the next level, don’t hesitate to reach out to us. We’re here to help you bring your vision to life. Contact us today to learn more about how we can help your WordPress project stand out and achieve your goals.