Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

pagination with custom query with permalinks in functions.php WordPress

  • SOLVED

Hi,
I've spent all day trying to figure this out, I've done all the tutorials that I could find on the web, but I still can't get the pagination to work properly.
WP-PageNavi shows 3 pages (there are 19 posts total) and the limit per page is 7, but when clicking on the nav buttons just reloads the same posts.
this is an example of the problem:
http://www.dekritischebelegger.nl/archives
/category/Aandelen/2010/05/page/3/

I have used this code to get archives by category
http://www.jayminkapish.com/2009/06/16/date-archive-for-a-category/

but I modified it to suit my needs. Anyway, it doesn't say anything about pagination


function my_date_archive_for_category() {

...
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'cat' => $category_array["term_id"],
'year'=> $request_parts["4"],
'monthnum'=>$request_parts["5"],
'paged' => $paged
));

if ( have_posts() ) :

while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?>>
<?php global $post; ?>

<?php endwhile; ?>

<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
<?php
else:
$is404 = true;
endif;
}
else {
$is404 = true;
}
}
else {
$is404 = true;
}

if($is404) {
include(TEMPLATEPATH . '/404.php');
} ?>
</div><!--end for left-r-->

</div>

<div id="right">
<?php get_sidebar(); ?>
</div>

</div>

<?php get_footer();
exit;
}
}

add_action('init','my_date_archive_for_category');


I haven't posted all the code, but I attached the functions.php file

Answers (2)

2010-06-01

Oleg Butuzov answers:

after the function <strong>query_posts</strong> run please make var_dump($GLOBALS['wp_query']) to debug variables that passed to the query_posts


paul de wouters comments:

done


Oleg Butuzov comments:

you can remove var_dump. $paged isn't there =)

wait a minute....


Oleg Butuzov comments:

function get_page_variable(){
$url = $_SERVER['REQUEST_URI'];
@list(,$page) = @explode('page/', $url);
$page = rtrim($page, '/');
return intval($page) == 0 ? 1 : intval($page);
}

$paged = get_page_variable();


insted

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;


paul de wouters comments:

Oleg

that functions seems to have done the trick.
how does it work?


Oleg Butuzov comments:

well. my last expirience with category/date/date wasn't susseful. even if i had rewrites rules all gose to canonical rewrites ... i have fix all this stuff in a way you show to people right now.

i have get a problem with paging =) but i have solve it in a bit different way - i am adding rewrite rules to match the url pattern. its complicated way for a non proffesional. so in your way a batter was just to parse a url for a page variable =)

cheers!


paul de wouters comments:

thanks for the explanation.
so you are using the .htaccess rules to do the same?
if you want you can also post that solution, just out of curiosity

2010-06-01

Bill Hunt answers:

Looks like a simple typo maybe?

<blockquote>

<strong>$paged</strong> = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts(array(

'cat' => $category_array["term_id"],

'year'=> $request_parts["4"],

'monthnum'=>$request_parts["5"],

'paged' => $paged

));
</blockquote>

Give that a shot, maybe?


Bill Hunt comments:

Looks like a simple typo maybe?

<blockquote>

<strong>$paged</strong> = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts(array(

'cat' => $category_array["term_id"],

'year'=> $request_parts["4"],

'monthnum'=>$request_parts["5"],

'paged' => $paged

));
</blockquote>

Give that a shot, maybe?


Bill Hunt comments:

Sorry, changed <strong>$page</strong> to <strong>$paged</strong> in that first bit.


paul de wouters comments:

Hi Bill,

thanks for the typo, but that didn't fix it.

the URL was not displayed, so here it is again
http://www.dekritischebelegger.nl/archives/category/Aandelen/2010/05/page/3/


Bill Hunt comments:

Everything else in that block is correct; I assume that it's not getting all of the necessary arguments from before? Can you post the full source from above that part, and var_dump($category_array, $request_parts) and post the result?


paul de wouters comments:

I added
var_dump($category_array, $request_parts);
var_dump($GLOBALS['wp_query']);

is that correct?


paul de wouters comments:

also the whole functions.php is attached