0

Right now, i am using code below to show title and its link of recent posts from my custom post category, but for one of my category i want to show excerpt, how can i achieve that.

CODE i am using to display title:

<?php
    $recent_posts = wp_get_recent_posts(array('post_type'=>'jokes'));
    foreach( $recent_posts as $recent ){
        echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
    }
?>
3

You can use the wp_trim_excerpt() to get the excerpt of the content.

You can use it inside your loop to get the excerpt of each post

the string provided to a maximum of 55 words if it is more then a [...] will be added to the end of the string.

<?php
    $recent_posts = wp_get_recent_posts(array('post_type'=>'jokes'));
    foreach( $recent_posts as $recent ){
        echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
        $excerpt = wp_trim_excerpt( $recent['post_content']); // $excerpt contains the excerpt of the concerned post
        echo $excerpt;
    }
?>

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.