简体   繁体   中英

Get Wordpress excerpt in list of posts

I'm building a custom WordPress widget/plugin to return the title and excerpt of a post within a permalink to the article. I am almost there but am struggling with getting the excerpt to show. Here is my current code:

<?php
$args = array( 'numberposts' => '5' );

$recent_posts = wp_get_recent_posts( $args );

foreach( $recent_posts as $recent ){

    $categories = get_the_category($recent["ID"]);

    $excerpt = apply_filters('get_the_excerpt', $recent->post_excerpt);

    echo '<a class="m-item  diet-and-nutrition" href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" > <div class="pix"></div><div class="eyebrow"> <b>' . $categories[0]->name . '</b> / '.   $recent["post_date"].'</div> <figure>' .  get_the_post_thumbnail($recent["ID"], 'full') . '<figcaption class="center"> <span> <h4>'.  $recent["post_title"].'</h4> </span> <span> <p>'.  $excerpt .'</p> </span> </figcaption> </figure> </a>';
}
?>

It's an array so:

$recent->post_excerpt

should be:

$recent['post_excerpt'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM