Featured Story Shortcode not outputting content

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' );

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.org/reference/functions/do_shortcode/#user-contributed-notes
and make changes.

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x