This is a working function so you can use it just copy and past in your functions.php file And use short code show it in front end.
Display posts by category wordpress on any page
function short_code(){
ob_start();
//$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Uncategorized',
'posts_per_page' => 5,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_excerpt(); ?>
<!-- <a href="<?php //the_permalink(); ?>">Read More</a> -->
</div>
</article>
<?php
endwhile;
endif;
return ob_get_clean();
// ob_clean();
}
add_shortcode("sc","short_code");
0 Comments