Home > Software engineering >  How to limit the output of characters in the_content()
How to limit the output of characters in the_content()

Time:08-31

I need to display a certain number of characters in the post description on the archive page. How can this be done, can it be done without the filter hook?

$args = array(
  'posts_per_page' => 3,
  'offset' => 0,
  'orderby' => 'post_date',
  'order' => 'DESC',
  'post_type' => 'post',
  'post_status' => 'publish'
);
$query = new WP_Query($args);
if ($query - > have_posts()):
  while ($query - > have_posts()): $query - > the_post(); ? >

<p> <?php the_content(); ?> </p>

CodePudding user response:

Instead of running the_content() you could use get_the_content() & then limit based on that. Here's an example.

<?php
$content = get_the_content();
$limited_content = substr( $content, 0, 50 ); //get first 50 characters
echo "<p>" . $limited_content . "</p>";
?>
  • Related