I hadn’t noticed it, but someone recently pointed out that this site’s page navigation wasn’t working.
I’d upgraded to WordPress 3 a few weeks back and it quickly became clear that this was part if not all of the reason why, as page navigation worked perfectly after downgrading this site to WordPress 2.9.2.
Why? I had no idea, so I raised the issue on the Webtreats forum.
The Problem
First let’s take a look at how inFocus and WordPress handle page navigation. For this example let’s assume I have a blog with a total of 8 posts spanning a maximum of 3 pages. Permalinks are set to the default ../?p=123.
Table I shows the links that are displayed on summary pages and their corresponding page number.
Link Text | Link URL | Page |
---|---|---|
1 | http://domain.com/ | 1 |
2 | http://domain.com/?paged=2 | 2 |
3 | http://domain.com/?paged=3 | 3 |
Apart from the link to page 1, each link URL contains a parameter named paged and a value corresponding to that particular page.
When a link is clicked WordPress updates a variable named $paged with the value of the paged parameter passed in the link URL.
The variable $paged is then used in the following code block in the file wp-content/themes/infocus/lib/includes/template-blog.php to construct a query string as seen on line 166.
<?php $exclude_blog_cats = preg_replace("!(\d)+!","-${0}$0", $blog_excludecats); $query_string = "cat=".$exclude_blog_cats."&paged=$paged"; query_posts($query_string); $counter=0; ?>
The query string contains the value of the variable $paged. This query string is then passed to the query_posts function which returns an array of the posts to be displayed depending on the value of $paged.
Table II below shows the relationship between the link URL, the $paged variable, the query string and the posts returned by the query_posts() function. The cat= parameter in the query string is not relevant to this discussion and is shown only for continuity. Also note that only links 1, 2 and 3 would be genuine links in our example. The other link URLs are shown purely for interest.
Link Text |
Link URL | $paged Variable |
Query String | Posts Returned for Page # |
Number of Posts Displayed |
---|---|---|---|---|---|
1 | ../ | 0 | cat=&paged=0 | 1 | 3 (1-3) |
../?paged= | 0 | cat=&paged=0 | 1 | 3 (1-3) | |
../?paged=0 | 0 | cat=&paged=0 | 1 | 3 (1-3) | |
../?paged=1 | 1 | cat=&paged=1 | 1 | 3 (1-3) | |
2 | ../?paged=2 | 2 | cat=&paged=2 | 2 | 3 (4-6) |
3 | ../?paged=3 | 3 | cat=&paged=3 | 3 | 2 (7-8) |
../?paged=4 | 4 | cat=&paged=4 | No Posts Returned | N/A |
It’s fairly clear that the value of the variable $paged is fundamental in determining which posts are displayed. With this in mind, Table III below shows the value of $paged after each page selection under WordPress 2.9.2 and WordPress 3.0.1.
Variable | WordPress 2.9.2 | WordPress 3.0.1 | ||||
---|---|---|---|---|---|---|
Page 1 | Page 2 | Page 3 | Page 1 | Page 2 | Page 3 | |
$paged |
0 | 2 | 3 | 0 | empty | empty |
It’s easy to see why page navigation isn’t working under WordPress 3.0.1 as the value of $paged is empty for all but page 1. The consequence of this is shown in Table IV below.
Link Text |
Link URL | $paged Variable |
Query String | Posts Returned for Page # |
Number of Posts Displayed |
---|---|---|---|---|---|
1 | ../ | 0 | cat=&paged=0 | 1 | 3 (1-3) |
2 | ../?paged=2 | empty | cat=&paged= | 1 | 3 (1-3) |
3 | ../?paged=3 | empty | cat=&paged= | 1 | 3 (1-3) |
The Solution
The solution from Webtreats was to add the highlighted line of code to wp-content/themes/infocus/lib/includes/template-blog.php.
<?php $paged = $page ? $page : 1; $exclude_blog_cats = preg_replace("!(\d)+!","-${0}$0", $blog_excludecats); $query_string = "cat=".$exclude_blog_cats."&paged=$paged"; query_posts($query_string); $counter=0; ?>
Here, the existing value of $paged is overwritten with the value of the variable $page or 1 if it’s null. Table V below shows the correct value being passed to the query string and consequently the correct posts being displayed.
Link Text |
Link URL | $page Variable |
Query String | Posts Returned for Page # |
Number of Posts Displayed |
---|---|---|---|---|---|
1 | ../ | null | cat=&paged=1 | 1 | 3 (1-3) |
2 | ../?paged=2 | 2 | cat=&paged=2 | 2 | 3 (4-6) |
3 | ../?paged=3 | 3 | cat=&paged=3 | 3 | 2 (7-8) |
However, this is not quite the end of the story. Table VI below shows the value of $page after each page selection under WordPress 2.9.2 and WordPress 3.0.1.
Variable | WordPress 2.9.2 | WordPress 3.0.1 | ||||
---|---|---|---|---|---|---|
Page 1 | Page 2 | Page 3 | Page 1 | Page 2 | Page 3 | |
$page | null | null | null | null | 2 | 3 |
So, while this solution fixes page navigation under WordPress 3.0.1 it breaks it for WordPress 2.9.2.
The solution if we want to use the same code base for WordPress 3.0.1 and WordPress 2.9.2 is to replace this entire code block…
<?php $paged = $page ? $page : 1; $exclude_blog_cats = preg_replace("!(\d)+!","-${0}$0", $blog_excludecats); $query_string = "cat=".$exclude_blog_cats."&paged=$paged"; query_posts($query_string); $counter=0; ?>
… with this new one. The changes are highlighted.
<?php if ( (int)substr(get_bloginfo ('version'),0, stripos(get_bloginfo ('version'),'.')) < 3 ) { // WordPress 2.x or lower $paged = $paged ? $paged : 1; } else { // WordPress 3.x or higher $paged = $page ? $page : 1; } $exclude_blog_cats = preg_replace("!(\d)+!","-${0}$0", $blog_excludecats); $query_string = "cat=".$exclude_blog_cats."&paged=$paged"; query_posts($query_string); $counter=0; ?>
On line 165 the version of WordPress being used is determined. If it’s less than version 3 the variable $paged is used. If it’s version 3 or higher $page is used. The value from the appropriate variable is assigned to $paged which in turn is used to construct the query string.
Hi Steve
I am not a developer. Just a writer with some vague tech skills. I am wrestling mightily with the page navigation thing and saw your detailed responses. I think I understand the issue but the php files look to me like Japanese characters look to you.
I am hoping you can in a matter of seconds see where the blog.php file is wrong and what needs to change … I already tried changing paged to page in spots, but that didn’t do anything.
Here is the code passage:
// prepeare posts query
$paged = (get_query_var(‘page’)) ? get_query_var(‘page’) : 1;
$args=array(‘page’=>$page);
if($blog_categories != ”) { $args[‘cat’] = $blog_categories; }
if($blog_authors_arg != ”) { $args[‘author’] = $blog_authors_arg; }
if($excluded != ”) { $args[‘category__not_in’] = $excluded; }
query_posts($args);
$max_page = $wp_query->max_num_pages;
if(have_posts())
{
while(have_posts())
Any help much appreciated
Dave Haynes (Canada)
Hi Dave,
Change the 2nd line of your code from:
to:
Regards, Steve.
thanks Steve, I will try that
Hi Steve,
First off, great post and very informative topic. Since we’re on the topic of navigation and you’re using inFocus theme I was just wondering if you could shed any lights on a small issue I currently have. I came across your site while digging around for solution(s), asked the developer, but leads to nowhere.
I have just recently updated my inFocus 1.6 running on WordPress 3.1in testing and staging environment. The setup is a WAMP (Windows) based setup.
When I used the option that came with inFocus to hide Portfolio category from showing up on the Blog page then navigate with Breadcrumbs one level back (up) I got a blanked page with un-styled and un-formatted layout.
For example: Home >> Portfolio >> Post_name. So when I used Breadcrumbs to go back to Portfolio page I got that blanked page, but if I didn’t exclude that category in the inFocus Blog’s menu everything showed up just fine.
Your thoughts are greatly appreciated!
Louis
P.S. Everyone’s suggestions are welcome.
Steve! You are the man! My pagenavi seems to be broken on my Awake theme, but webtreats is saying “hey it works in our test environment, is must be something you did”. I’m pretty new at all of the web stuff, so I haven’t tweaked anything.
I guess I’m asking if you have found out anything else, especially with recent release of 3.0.4, that might be helpful. I still don’t have any pages on my blog: www.homesystemintegration.com/articles
Any help would be greatly appreciated!
Thanks,
Tom
Hi Tom,
I still don’t know why upgrading from WordPress 2.9.x to 3.0.x broke the page navigation when using inFocus. The developers didn’t know either, but the fix detailed in this post corrected the issue. While I’m not familiar with their Awake theme, I can only assume there may be a similar issue. Have you tried this fix?
Regards, Steve.
never mind. fixed!
btw, now the infocus twitter feed doesn’t update :S
Thanks Steve! It worked great! 😀
Glad it helped, Mark.
Regards, Steve.
Great looking site, btw how did you do that rating thing where you can choose how many stars for a post? I would love to have something like this on my WP.
Thanks Greg,
…how did you do that rating thing where you can choose how many stars for a post?
I use the WP-PostRatings plugin.
Regards, Steve.
The error is related only to inFocus and Awake themes. Thanks anyway Sir, you gave me your valuable time.
Sir, here are some links to empty blog page problem:
http://mysitemyway.com/support/topic/blog-page-doesnt-work#post-1465
http://mysitemyway.com/support/topic/cant-post-to-blog#post-2827
http://mysitemyway.com/support/topic/empty-blog-page#post-3545
the solutions given range from not to have a folder named blog in the same directory, deleting files to reinstalling wordpress itself, which are not viable for my project. Moreover my site is on the intranet of our organisation so I cannot have remote diagnostics done by others.
Any ideas sir as to what I could do? Thanks anyway.
Hi dev,
…here are some links to empty blog page problem…
If the error is specifically related to either the inFocus or Awake themes and the developers are unable to give you an answer then, as I said, I can’t really be of any help.
If it’s a wider issue, have you tried asking in the WordPress support forums?
Regards, Steve.
My inFocus Blog page displays nothing but a msg “Sorry, that document can not be found.” Webtreats forum guys have no definite answer, they are just going by trail and error method. Any idea why this is happening. I am using WordPress 3.0.1.
Hi dev,
…My inFocus Blog page displays nothing but a msg “Sorry, that document can not be found.”…
Sorry to hear you’re having problems with inFocus. I’ve never seen this message before. Couldn’t even find it on Google. To be honest, if Webtreats don’t have an answer yet there’s not much I can add, I’m afraid. Hope you get it resolved soon.
Regards, Steve.
Hi again,
Thanks a lot! It works great.
I just changed to link to the cat. name + added “child_of=” and done. 🙂
Thank you very much,
Keep the good work comming 🙂
Nico
You’re welcome Nico.
Regards, Steve
Hi Nico,
I’m not entirely sure what you’re looking for as the link didn’t show in your post. However, if you open header.php and on or around line 91 replace this line of code:
with this …..
…. it should give you what I think you’re looking for.
You can see an example in this blog’s top navigation.
Regards, Steve.
Hi Steve,
Thanks for your great posts!
I’ve got a question though: Is it possible to show categories on the top navigation with a dropdown menu (subcategories of that categorie) ? I manages to show it with:
But it doesn’t put in a dropdown menu with the subcategories.
Any thoughts?
Thanks a lot,
Nico
You sir are a genius!
I actually tried to install the pagenave plug-in to get the options, but that just got me a major malfunction instead. Your sollution did the trick though and for that, I salute you!
Keep up the good work!
Glad it worked for you Pontus.
…I actually tried to install the pagenave plug-in to get the options, but that just got me a major malfunction instead…
It is possible to install WP-PageNavi with the inFocus theme. First though, you’d need to comment-out line 36 in the functions.php file, like so:
I’ve installed it successfully and it works, but to be honest I’ve not used it enough to know for sure it hasn’t broken something elsewhere. You’d probably be better sticking with the inFocus version.
Regards, Steve.
Hi Steve, nice blog!
Speaking about Page Navigation in inFocus; I’m trying to translate the text “Page x of x” by editing line 202 in wp-pagenavi.php located in lib/includes. But the changes doesn’t seem to make any difference. Any thought on that?
Thanks Pontus,
…the changes doesn’t seem to make any difference
You may know that WP-PageNavi is originally a plug-in that comes with a configuration page. The inFocus version is lacking the configuration options and appears only to create/save the options once, but doesn’t allow any updates.
The quick-and-dirty way around this is to first delete just the pagenavi_options from the wp_options table (your table prefix may not be wp_ ) in your WordPress database. Make the changes to line 202 in wp-pagenavi.php and then re-load the page. The pagenavi_options will be re-created.
You should of course take the usual precautions before making any changes to the WordPress database tables.
Regards, Steve.
EDIT
You can ignore the above. The much cleaner way is to simply change line 220 in wp-pagenavi.php from…
…to
The update_option() function will not only update existing options, but also create them if they don’t exist.
You can now amend the pagenavi_options in wp-pagenavi.php as you’d originally tried.