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

Unable to get page number with pagination plugin and query_posts WordPress

  • SOLVED

Hey -

So I have been trying to fiqure out this issue, here and there for weeks, I have a solution, but not a good one. I am using this plugin: http://wordpress.org/extend/plugins/wp-paginate/ for pagination.

<strong>Here is the problem:</strong>
The navigation does not advance and shows the same posts due to the paginate function not being able to grab the current page number. It shows up fine and shows the number of pages needed, but clicking on the page numbers shows the same page 1 and posts and the url will change to 2/3/4, etc. You can see it in action here: http://blog.rosettastone.com at the bottom of the page.

<strong>Here is what I am using in the loop:</strong>

query_posts('cat=4&paged='.get_current_page());
if(have_posts()){
while(have_posts()){
the_post();
global $more;
$more = 0;


<strong>The get current solution is a hack, that I do not want to use and it looks like this, called from functions.php:</strong>

/* Get current page for paginaton navigation */
function get_current_page() {
$pageURL = 'http';
//check what if its secure or not
if ($_SERVER["HTTPS"] == "on") {
$pageURL .= "s";
}
//add the protocol
$pageURL .= "://";
//check what port we are on
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
//cut off everything on the URL except the last 3 characters
$urlEnd = substr($pageURL, -3);
//strip off the two forward shashes
$page = str_replace("/", "", $urlEnd);
//return just the number
return $page;
}

<strong>I have also tried the following:</strong>

if(have_posts()){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts($query_string . "&cat=4");
while(have_posts()){
the_post();
global $more;
$more = 0;


Which will work on one main page but not the other.

<strong>I have been told the following:</strong>

"Calls to query_posts() made within the theme have no affect on pagination. To affect pagination links you need to hook into the parse_query action and manipulate the main query. Paging is done off of the results of the main query."

<strong>As well as:</strong>
Look into the parse_query filter and see if I can do what is needed there . . .

Can you help? I have not worked much with filters (learning that stuff now) and could use some help on this simple, yet difficult issue. I would rather do it right vs. using the hack I worked out and pieced together.

PLEASE HELP!!!

Answers (5)

2011-01-20

Joachim Kudish answers:

Can you try this for your loop?

$paged = get_query_var( 'page' );
query_posts('cat=4&paged='.$paged);
if(have_posts()) { while(have_posts()) { the_post();

etc...

Since WordPress 3.0.2, the query variable is 'page' and not 'paged' which is why your second attempt didn't work.
Also, I noticed your blog is hosted on WordPress.com, are you nonetheless able to modify your theme?

Hope it works.
Cheers


Chris MacDonald comments:

Hey -

Thanks for the response, the crazy things is that this works and then when I go to the other main page on the site that needs to do the same thing with category of 3. It does not work on that page, but does on one page. Any idea why this would be?


Joachim Kudish comments:

Can you provide the link for that page and the loop it's using?


Chris MacDonald comments:

The site is live and I am developing this fix locally. The 2 live pages are as follows:
Rvoice:http://blog.rosettastone.com/
Living Language: http://blog.rosettastone.com/language-journeys/

The loops I am using locally are as follows (it is the code you gave me):

<strong>Rvoice: </strong>

$paged = get_query_var( 'page' );

query_posts('cat=4&paged='.$paged);

if(have_posts()) { while(have_posts()) { the_post();


<strong>Living Language: </strong>

$paged = get_query_var( 'page' );

query_posts('cat=3&paged='.$paged);

if(have_posts()) { while(have_posts()) { the_post();


Joachim Kudish comments:

So the living language seems to be working fine.

As for Rvoice, are you modifying the loop in index.php or somewhere else?


Chris MacDonald comments:

The Rvoice page I am working on locally works fine once I added what you gave me. When I add it there (which is the homepage or first page on the site) and then add the same thing but change the category for the second section it does not work, on living language (locally)

Live on the site - it advances the posts, but does not update the active link or page in the pagination nav at the bottom of the page, notice how it stays on page one, even though it advances.

Locally when I implement your solution, it works on Rvoice and not on Living Language.

Sorry this is so complicated . . . to explain


Joachim Kudish comments:

Okay. I did notice that the active state of the page buttons wasn't moving forward. Can you share the code you are using to generate those buttons and I should be able to help you out.

As for local vs. live not updating right away, do you have any caching plugins on the live site installed?


Chris MacDonald comments:

I am using this plugin for the pagination: http://wordpress.org/extend/plugins/wp-paginate/

<strong>The code to display the nav is: </strong>

<?php if(function_exists('wp_paginate')) {
wp_paginate();
} ?>


I am not pushing this live since it is hosted on VIP through Wordpress.com and requires SVN and approval. I have tried to implement the wrong solution and need to get them to approve it first, meaning it has to work locally . . .


Joachim Kudish comments:

I am a little confused about what is working and not working at this point. Since I can't preview what you are doing locally it's a little hard to visualize.

Can you quickly re-explain what's not working and what you tried to do to fix it?


Chris MacDonald comments:

yeah here is the deal the 2 pages posts each from one category and with a formatted post format and separate headers and sidebars.

<strong>The Rvoice page is working (local) using the code:</strong>

$paged = get_query_var( 'page' );

query_posts('cat=4&paged='.$paged);

if(have_posts()) { while(have_posts()) { the_post();


<strong>The living language page is the SAME CODE, different category of posts and different header/sidebar I am using the same code you gave me, but different category and it does not work, same problem.</strong>


$paged = get_query_var( 'page' );

query_posts('cat=3&paged='.$paged);

if(have_posts()) { while(have_posts()) { the_post();


<strong>I posted the full code for both pages to my discourse page, does all that make sense?</strong>

2011-01-20

Pau answers:

try this

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array( 'cat' => '4', 'paged' => $paged );
query_posts($args);

2011-01-20

Nile Flores answers:

The loop itself naturally even in a category, or tag or for recent posts should work fine on your most recent posts. What you are doing is the same thing StudioPress has done in some of their themes when it is not necessary to use the custom WP Query when the natural WordPress loop will work and display the most recent posts.

Even using the custom query, it will call the same exact thing to the page no matter if it is page 2, page 3, and so on.

You would only need a special query if you only want to display a specific category or custom post type, or even from tags.

Then you can put in plugins like WP-PageNavi or others that you want to use ( like the navigation I use at blondish.net at the bottom of the front page, and similar to what you are trying to put on yours.) That is using a normal WordPress loop to call the most recent posts. ( http://codex.wordpress.org/The_Loop )

Side note- If you need me to give hands on help, drop me a line.



---------
You do not need anything fancy, just a normal WordPress loop.

This will eliminate your problem :)


Chris MacDonald comments:

Hey -

Thanks. Yeah both these pages show posts from a specific category and sidebar recent posts from that category and authors from that category. They are the same things really one uses cat 3 and the other 4, but the plugin I am using does not work, it works fine in the archives for that page, but not the main pages - they use query_posts and I have been told (by WP) that . . .

"Calls to query_posts() made within the theme have no affect on pagination. To affect pagination links you need to hook into the parse_query action and manipulate the main query. Paging is done off of the results of the main query."

I was told to look into the parse_query filter which I have never worked with and not sure what to do at this point . . .

2011-01-20

Denzel Chia answers:

Hi,

Viewed your scripts on the discourse section.

Your WordPress loop if(have_posts()) { while(have_posts()) { the_post(); is opened.

You forget to add in endwhile; endif; to close the WordPress Loop at the end of the codes.
Perhaps this is what caused the pagination errors.

Thanks.
Denzel


Chris MacDonald comments:

where and what code exactly would I add?

2011-01-21

Rashad Aliyev answers:

Hello,

I suggest you best page navigation plugin. [[LINK href="http://wordpress.org/extend/plugins/wp-pagenavi/"]]That's WP-PageNavi[[/LINK]]

setup it and anywhere. <?php wp_pagenavi(); ?>