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

Need help with wp-page-navi WordPress

  • SOLVED

Hi There,

I have a custom wordpress theme that I have built. It has a portfolio page that pulls in post from a particular category.

My theme has a custom back-end that allows the user to determine how many portfolkio items to display on the page.

Everything is working fine, but the issue I am having is with wp-page-navi. The page navi shows up but when you click one of the pages, it just stays on the same page (ie. it saysd page 1 of 3 and when you click page 2 it just stays on page 1).

I had a similar problem on my blog page, which was fixed by an expert on here:
http://www.wpquestions.com/question/show/id/337

I'm assuming I need to do something similar but my "loop" is a bit more complicated than my blog loop. Here is the loop that pulls in the posts. Any help is greatly appreciated:


<?php
//$category_id = get_cat_id($featport);
$category_id = get_post_meta($post->ID, '_multiple_portfolio_cat_id', true);
$posts_p_p = stripslashes(get_settings( $shortname."_portitems" ) );
$query_string ="posts_per_page=$item_count&cat=$category_id&posts_per_page=$posts_p_p";

query_posts($query_string);

$count = 0;
$col = 0;

if (have_posts()) : while (have_posts()) : the_post();

$count++;
$col ++;
$mod = ($count % 3 == 0) ? 0 : 3 - $count % 3;
?>

Answers (2)

2010-04-28

Lew Ayotte answers:

You need to add the $paged stuff to your loop...

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

$category_id = get_post_meta($post->ID, '_multiple_portfolio_cat_id', true);

$posts_p_p = stripslashes(get_settings( $shortname."_portitems" ) );

$query_string ="posts_per_page=$item_count&cat=$category_id&posts_per_page=$posts_p_p&paged=$paged";

query_posts($query_string);


BTW, you have two "posts_per_page" in that query_string... you probably want to remove one :)

Lew

2010-04-28

Milan Petrovic answers:

WP PageNavi works with the page WP_Query object, not a loop. query_posts is not exactly good choice for what you need. You need to change $wp_query object. Replace your query_posts with something like this:

<?php

global $wp_query;
$master_wpq = $wp_query;
$wp_query = new WP_Query($query_string);

?>


After your loop ends, it's good to restore old query:

<?php

$wp_query = $master_wpq;

?>


WP Answers comments:

Thank you very much for this code.

It isn;t quite working as required, but I think it is closer. Perhaps I didn;t add your code correctly? Can you check this to see if it is right?

After added the code, the pages sort of work. When you click page 2, it goes to page 2 (in the address bar /page/2/), but the "active state" on the page-navi is still on page 1, and the content is the same as page 1.

Any ideas what could be wrong? (sorry I'm fairly new to custom wordpress coding)

New code:

<?php
//$category_id = get_cat_id($featport);
$category_id = get_post_meta($post->ID, '_multiple_portfolio_cat_id', true);
$posts_p_p = stripslashes(get_settings( $shortname."_portitems" ) );
$query_string ="posts_per_page=$item_count&cat=$category_id&posts_per_page=$posts_p_p";

global $wp_query;
$master_wpq = $wp_query;
$wp_query = new WP_Query($query_string);

$count = 0;
$col = 0;

if (have_posts()) : while (have_posts()) : the_post();

$count++;
$col ++;
$mod = ($count % 3 == 0) ? 0 : 3 - $count % 3;
?>