get posts from only one category. Is there a better practice?

I did create a custom page template and assigned to a page.

I have this code inside:

<?php
/*
Template Name: Ultime novità
*/
?>

<?php get_header(); ?>

<?php
$news_filter = array(
‘category’ => ’53’,
‘posts_per_page’ => 5
);

$news = get_posts( $news_filter );

echo count ( $news );

foreach ($news as $post) : setup_postdata($post);
the_post_thumbnail();
the_title();
the_content();
endforeach;
?>

It works, but I would learn more and more wordpress so, I would ask, if is there a better code to do this.

For example, I know existing of (custom) filter. Instead to make the loop twice (by WP and by me), can we add a filter, block WP and perform a custom loop?

Read more here:: get posts from only one category. Is there a better practice?

Leave a Reply

Your email address will not be published. Required fields are marked *