0

Could use some help... using this code to call up a custom number of posts from a certain categorey and only want to display the first 15 characters. However, it is showing the same excerpt on each post versus showing the unique excerpt for each post. Can anyone help me out on this one?

Code below is resulting in the same excerpt to be added to the posts, even though the correct title and thumbnails are displaying!

I'm a bit of a novice so if you can explain I'd gladly appreciate it!

<?php $posts = get_posts('category=3&orderby=date&numberposts=3'); foreach($posts as $post) { ?>
<a href="<?php the_permalink() ?>" target="_parent">
<div class="aligncenter" style="display: block;">
<?php the_post_thumbnail( 'thumbnail', array( 'class' => 'img-responsive' ) ); ?>
</div>
<h3><?php the_title(); ?></h3></a>
<?php while (have_posts()) : the_post(); ?>
<?php echo excerpt(15); ?>
<?php endwhile; ?>
<a href="<?php the_permalink() ?>" class="blue"" target="_parent">
Read More</a>
</div>
<?php } ?>

here is my functions.php

function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}   
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}


function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}   
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content); 
$content = str_replace(']]>', ']]&gt;', $content);
return $content;
0

If you are simply trying to echo the excerpt in a specific part of a theme, you can simply use the following code within the loop:

<?php 

    $excerpt = get_the_excerpt();
    echo substr( $excerpt, 0, 15 ) . '&hellip;';

?>

If you want to filter the excerpt globally (in archives, blog pages, etcetera) to match any post part of one or more given categories, you can place the following code in your theme's functions.php file:

<?php

    function wpse_225203_category_based_trim_excerpt( $excerpt ) {

        //Return if there is not a except to work with
        if ( '' === $excerpt ) :
            return $excerpt;
        endif;

        //Get the post's categories
        $categories = get_the_category();

        //Get the post's categories ID
        $categories_id = wp_list_pluck( $categories, 'term_id' );

        //Set the post's categories ID we want to match
        $categories_to_trim_ids = array( 1, 3 ); //Modify to match your ids

        //Match the wanted categories ids and the post's categories ids
        if ( ! array_intersect( $categories_to_trim_ids, $categories_id ) ) :
            return $excerpt;
        endif;

        //$excerpt = wp_trim_words( $excerpt, 15 ); //Trim the excerpt to the first 15 words
        $excerpt = substr( $excerpt, 0, 15 ) . '&hellip;'; //Trim the excerpt to the first 15 characters

        return $excerpt;

    }

    add_filter( 'get_the_excerpt', 'wpse_225203_category_based_trim_excerpt' );

?>
  • thanks that's not working for me either. Do you think it's because I'm trying to pull multiple different number of posts from different categories? The excerpts I'm getting do not even match the posts some times.. – Cdunn Apr 29 '16 at 16:33
  • To tell the truth I'm not completely sure what you are trying to achieve. I thought the question was about how to trim the excerpt length of the posts belonging to a given category or a given query. Reviewing your code, you are using functions from the loop inside the get_posts() foreach loop. That's probably why you are getting the same excerpt for all of them. Add setup_postdata( $post ) before the link with the permalink and remove the lines with while and endwhile. – Luis Sanz Apr 29 '16 at 16:52
  • setup_postdata( $post ) worked! Thank you so much! – Cdunn Apr 29 '16 at 17:04
  • Glad you got it working. The way the loop functions work can be a bit tricky at the beginning. We all were there! – Luis Sanz Apr 29 '16 at 17:07
0

Put the following code in your functions.php

 function excerpt($limit) {
          $excerpt = explode(' ', get_the_excerpt(), $limit);
          if (count($excerpt)>=$limit) {
            array_pop($excerpt);
            $excerpt = implode(" ",$excerpt).'...';
          } else {
            $excerpt = implode(" ",$excerpt);
          } 
          $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
          return $excerpt;
        }

        function content($limit) {
          $content = explode(' ', get_the_content(), $limit);
          if (count($content)>=$limit) {
            array_pop($content);
            $content = implode(" ",$content).'...';
          } else {
            $content = implode(" ",$content);
          } 
          $content = preg_replace('/\[.+\]/','', $content);
          $content = apply_filters('the_content', $content); 
          $content = str_replace(']]>', ']]&gt;', $content);
          return $content;
        }

then in your template code you just use..

<?php echo excerpt(25); ?>

OR try this at your template

<?php 

    $excerpt = get_the_excerpt();
    echo substr( $excerpt, 0, 25);

?>
  • Thanks, apparently my paste didn't work as this is actually what is in my functions. This is still resulting in the same excerpt to be added to the posts, even though the correct title and thumbnails are displaying! – Cdunn Apr 29 '16 at 15:37
  • ok no problem i have put another way to show excerpt with limit. Try this one hope you find the solution through this. – Owais Alam Apr 30 '16 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.