admin管理员组

文章数量:1317915

So I'm trying to get a shortcode working where it calls the posts in the featured story category but I'm having issue getting it to output even just the title from the post. There's no error on the page. But there's nothing else showing either but white space between the nav and footer.

function featured_story() {
    $args = array(
        'posts_per_page' => 5,
        'category_name' => 'featured_story'
    ); 

    $last_5_posts_query = new WP_Query( $args );
    
    while($last_5_posts_query->have_posts()) :
        $last_5_posts_query->the_post();
        $title = get_the_title();

        $content .= '<div class="featured-stories">';
        $content .= '<h3>' .$title. '</h3>';

    endwhile;

return $content;

}
add_shortcode( 'featured-story', 'featured_story' );

So I'm trying to get a shortcode working where it calls the posts in the featured story category but I'm having issue getting it to output even just the title from the post. There's no error on the page. But there's nothing else showing either but white space between the nav and footer.

function featured_story() {
    $args = array(
        'posts_per_page' => 5,
        'category_name' => 'featured_story'
    ); 

    $last_5_posts_query = new WP_Query( $args );
    
    while($last_5_posts_query->have_posts()) :
        $last_5_posts_query->the_post();
        $title = get_the_title();

        $content .= '<div class="featured-stories">';
        $content .= '<h3>' .$title. '</h3>';

    endwhile;

return $content;

}
add_shortcode( 'featured-story', 'featured_story' );

Share Improve this question asked Nov 10, 2020 at 7:52 ladycladyc 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

First thing : initialize variable $content=''; before the while loop. other than that your code is right. there is something you are doing wrong to use shortcode. you should check this link https://developer.wordpress/reference/functions/do_shortcode/#user-contributed-notes and make changes.

also if using for custom post type please add post_type argument in $args

本文标签: postsFeatured Story Shortcode not outputting content