
How To Display The Posts From Specific Category Using Shortcode
To display posts from a specific category using shortcode in WordPress, you can follow these code, add these codes in the function.php
file.
//Post display shortcode from specific category
// function that runs when shortcode is called
function wpb_demo_shortcode() { $category_id = 32; // Replace 3 with the ID of the category you want to display $query = new WP_Query( array( 'category__in' => $category_id ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $result .= '<div class="item">'; $result .= '<div class="itemname1">' . get_the_title() . '</div>'; $result .= '<div class="item-desc">' . wp_trim_words( get_the_content(), 40, '...' ) . '</div>'; $result .= '<a href="'. get_the_permalink() .'">' .'Read More'. '</a>'; $result .= '</div>'; }} wp_reset_postdata(); // Output needs to be return return $result; } // register shortcode add_shortcode('postdisplay', 'wpb_demo_shortcode');