Customize WordPress Posts(limit, categorize)

by Sam  


It is common to display the list of posts on our wordpress pages or categories. Sometimes, we want to limit the number of posts and per page. In wordpress, it’s really easy to do it. Take a look at the full code below!

FULL CODE


<?php
$query = new WP_Query(
array(
'post_type' => 'post',
'cat' => '65,66' ,
'post_status' => 'publish',
'posts_per_page' => -1
)
);

if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="title">
<h1> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
</div>
<div class="body">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Let’s customize the code.


$query = new WP_Query(
array(
'post_type' => 'post',
'cat' => '65' ,
'post_status' => 'publish',
'posts_per_page' => -1
)
);

Using the code above, we can customize our data by the following:

1. Display the list posts based on categories

if we want to display the list based on category, we need to know first the ID number of a category. To do this, you need to go to Posts>>Categories and click the category. On the uri query string, you can see the ID number as seen below.

If you you want to add multiple categories, you may do it this way. Check this out.

'cat' => '65, 66'

2. Limit the number of Posts 

Initially, we put 'posts_per_page' => -1  . It means that the post has No limit. You can change the value you like.

Conclusion

There different ways to retrieve posts from the database and one of which is using the WP_Query class and we just only pass through the arguments we needed. The code above is a simple way of displaying of posts. If you have additional information, please comment below.



Leave a Reply

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


Need of a Website?

Maybe you're planning to build a landing page for your website. You might also have a plan to create a web application. I can offer you with an affordable but with good quality services. Don't hesitate to submit your specifications by clicking the button below.

Get Quote