Try Out New Website HTML Table- Creator HTML Table Generator

Here Is A Quick Cure For QUERY_POSTS() PAGINATION PROBLEM

query_posts() Pagination problem Ok, this has bothered me for a long time and now i have finally found a solution for it thanks to Kafkaesqui moderato

query_posts() Pagination problem

Ok, this has bothered me for a long time and now i have finally found a solution for it thanks to Kafkaesqui moderator on WordPress Support. As the title says when you are using a query_posts() to exclude, include categories or what ever you want pagination dosen't work it shows same posts on every page (This is for all pages that use query_posts not just index, so it's for templates etc...)

query_posts() Pagination problem

Where's the problem? query_posts() is a powerful function, but in this situation it has a flaw: it overrides nearly everything in the standard posts object query, including what the paged offset is.

How to fix query_posts() Pagination problem

How to fix it? To get proper pagination with query_posts() we need to recreate it through the 'paged' parameter or query. Best way to do this is to ask WordPress for the "page" we happen to be on, and use that as our 'paged' value. There's the code for it

<?php if (have_posts()) : ?>   
    <?php query_posts("category_name=somecat"); ?>
        <?php while (have_posts()) : the_post(); ?>  
 
replace with 
 
<?php if (have_posts()) : ?>   
     <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("category_name=somecat&paged=$paged"); ?>
        <?php while (have_posts()) : the_post(); ?>  

And that's it! The $paged = line above uses what's called a ternary operator to test whether the 'paged' query variable is available. If it is, $paged receives its value. If it isn't, we assume we're on the first page and assign it '1'. Then we tell query_posts() what page we're on with the addition of &paged;=$paged.

Rate this article

Loading...

Post a Comment

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing Lantro UI, you agreed to use cookies in agreement with the Lantro UI's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.