inFocus Page Navigation Error with WordPress 3.0.x

Steve August 8 Blogging , , with 26 and 0
Home/Blogging/inFocus Page Navigation Error with WordPress 3.0.x

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.

These instructions are only applicable for versions of inFocus up to and including v1.6

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.

TABLE I
Link TextLink URLPage
1http://domain.com/        1
2http://domain.com/?paged=22
3http://domain.com/?paged=33

 

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.

TABLE II
Link
Text
Link URL$paged
Variable
Query StringPosts Returned
for Page #
Number of
Posts Displayed
1../        0cat=&paged=013 (1-3)
 ../?paged= 0cat=&paged=013 (1-3)
 ../?paged=00cat=&paged=013 (1-3)
 ../?paged=11cat=&paged=113 (1-3)
2../?paged=22cat=&paged=223 (4-6)
3../?paged=33cat=&paged=332 (7-8)
 ../?paged=44cat=&paged=4No Posts ReturnedN/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.

TABLE III
VariableWordPress 2.9.2WordPress 3.0.1
Page 1Page 2Page 3Page 1Page 2Page 3

$paged

0230emptyempty

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.

Table IV
Link
Text
Link URL$paged
Variable
Query StringPosts Returned
for Page #
Number of
Posts Displayed
1../        0cat=&paged=013 (1-3)
2../?paged=2emptycat=&paged= 13 (1-3)
3../?paged=3emptycat=&paged= 13 (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.

Table V
Link
Text
Link URL$page
Variable
Query StringPosts Returned
for Page #
Number of
Posts Displayed
1../        nullcat=&paged=113 (1-3)
2../?paged=22cat=&paged=223 (4-6)
3../?paged=33cat=&paged=332 (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.

Table VI
VariableWordPress 2.9.2WordPress 3.0.1
Page 1Page 2Page 3Page 1Page 2Page 3
$pagenullnullnullnull23

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.

UPDATE 22nd March 2012 See WordPress 3.x Page Navigation Error with inFocus Theme: Revisited for an updated solution.

About the Author

A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of Fish & Chips and writing the occasional blog post he hopes others will find helpful.

26 Responses

25 Comments

  • Dave Haynes
    on July 16th, 2011 at 15:25
    Reply

    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)

    • Author
      Steve
      on July 17th, 2011 at 09:29
      Reply

      Hi Dave,

      Change the 2nd line of your code from:

      $args=array("page"=>$page);
      

      to:

      $args=array("paged"=>$paged);
      

      Regards, Steve.

  • Louis
    on April 1st, 2011 at 15:57
    Reply

    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.

  • Tom Abell
    on January 13th, 2011 at 11:56
    Reply

    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: http://www.homesystemintegration.com/articles

    Any help would be greatly appreciated!

    Thanks,

    Tom

    • Author
      Steve
      on January 16th, 2011 at 12:05
      Reply

      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.

  • Mark
    on November 29th, 2010 at 09:27
    Reply

    never mind. fixed!

  • Mark
    on November 29th, 2010 at 09:24
    Reply

    btw, now the infocus twitter feed doesn’t update :S

  • Mark
    on November 29th, 2010 at 09:12
    Reply

    Thanks Steve! It worked great! 😀

    • Author
      Steve
      on November 29th, 2010 at 09:24
      Reply

      Glad it helped, Mark.

      Regards, Steve.

  • Greg
    on October 12th, 2010 at 02:21
    Reply

    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.

    • Author
      Steve
      on October 12th, 2010 at 12:26
      Reply

      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.

  • dev
    on September 23rd, 2010 at 14:29
    Reply

    The error is related only to inFocus and Awake themes. Thanks anyway Sir, you gave me your valuable time.

  • dev
    on September 22nd, 2010 at 23:25
    Reply

    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.

    • Author
      Steve
      on September 23rd, 2010 at 08:50
      Reply

      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.

  • dev
    on September 22nd, 2010 at 02:08
    Reply

    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.

    • Author
      Steve
      on September 22nd, 2010 at 06:37
      Reply

      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.

  • Nico
    on August 29th, 2010 at 23:01
    Reply

    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

    • Author
      Steve
      on August 29th, 2010 at 23:34
      Reply

      You’re welcome Nico.

      Regards, Steve

  • Author
    Steve
    on August 27th, 2010 at 14:14
    Reply

    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:

    <?php wp_list_pages("sort_column=menu_order&exclude=$page_exclusions&title_li="); ?>
    

    with this …..

    <?php wp_list_pages("sort_column=menu_order&exclude=$page_exclusions&title_li="); ?>
    <li><a href='#'>Categories</a>
    	<ul>
    		<?php echo wp_list_categories('title_li='); ?>
    	</ul>
    </li>
    

    …. it should give you what I think you’re looking for.

    You can see an example in this blog’s top navigation.

    Regards, Steve.

  • Nico
    on August 26th, 2010 at 17:52
    Reply

    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

  • Pontus Ekman
    on August 18th, 2010 at 22:26
    Reply

    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!

    • Author
      Steve
      on August 18th, 2010 at 23:31
      Reply

      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:

      // Load wp-pagenavi
      if ( !function_exists('wp_pagenavi') ) :
      //require_once(WEBTREATS_INCLUDES . '/wp-pagenavi.php');
      endif;
      

      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.

  • Pontus Ekman
    on August 18th, 2010 at 05:27
    Reply

    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?

    • Author
      Steve
      on August 18th, 2010 at 12:01
      Reply

      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…

      	add_option('pagenavi_options', $pagenavi_options, 'PageNavi Options');
      }
      

      …to

      	update_option('pagenavi_options', $pagenavi_options, 'PageNavi Options');
      }
      

      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.

1 Pingback

  • wp-popular.com » Blog Archive » inFocus Page Navigation Error with WordPress 3.0.x | Tech Otaku
    on August 22nd, 2010 at 10:03
    Reply

    […] this article: inFocus Page Navigation Error with WordPress 3.0.x | Tech Otaku Tags: infocus, […]

Add comment

©Tech Otaku 2009 - 2017