mytechead

Android & Wordpress Help center

How to show all the posts of a category.

Hi Folks!!!

Its been quite some time since i have posted on this blog.

Was working on wordpress and just came across a scenario where i had to show all the posts belonging to a particular category. 

So here it goes..

Background ::

WordPress uses category.php as a template file  to show category pages. category.php can be modified to show all the posts belonging to that particular category. Now the catch here is that THE WHILE loop used to show all the posts belonging to that category limits the number of posts for a single page to be 10 (by default) & if the category has more than 10 pages, then it adds pagination links at the top and bottom of your page.

How to get rid of this :: 

Now if you wan to show all the posts for that category on the single page itself, without any pagination links just do this :

Before the while loop (the one used to loop through all the posts), somehting like this <?php while (have_posts()) : the_post(); ?> , add the following lines –

<?php

global $query_string;
query_posts( $query_string . ‘&posts_per_page=-1’ );

?>

and after the while loop ends, add –

<?php

wp_reset_query();

?>

What we are doing here is, we are modifying the $query_string  which the WP was using and changing its $post_per_page param to  “-1” (-1 for all the posts), additionally we can specify the value of $post_per_page to whatever number of posts we want & add support for pagination.

To know more about WP pagination stay tuned for my next post.

Advertisements

Single Post Navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: