WordPress.org

Ready to get started?Download WordPress

Forums

Problem with "current_page_item" navigation highlighting (45 posts)

  1. dwzemens
    Member
    Posted 7 years ago #

    I am using 2.1 and have successfully done dynamic navigation on several sites using "page_item" and "current_page_item" invoked by wp_list_pages(). I am having problems on one site. I have assigned the "Home" page as the static front page and a page called "Blog" as the post page alternative. For some reason, the Blog page does not dynamically highlight that navigation link because wp_list_pages() is not throwing the "current_page_item" into the HTML.

    Can anyone think of a reason why this is occurring? "Blog" is a "page", and I see the the WP documentation says that "current_page_item" is applied as follows: "When wp_list_pages is called while displaying a Page, the list item for that Page will be given the additional class "current_page_item". So, I have concluded that for some reason WP is not seeing the "Blog" page as a "page". Make sense? Any help appreciated.

  2. dansinch
    Member
    Posted 7 years ago #

    I'm having the exact same issue. I'm assuming it has something to do with the way you acheive this effect. I'm not sure what you did, but I have a file called blog.php that is used as the blog template, and a Page called "Blog" with a slug of "blog"

    Then, I have permalinks set up to use the name based URLs and have added '/blog/' to the beginning of the custom blog url.

    When a page is loaded it doesn't use the long blog URL and correctly loads the right page. But then the blog is loaded, I'm guessing it's using the custom permalink URL and not acually loading the "Blog" page.

    Or I could be completely wrong, but either way, the current_page_item class is not being applied properly.

    Anyone have any ideas why this would be happening? I use custom templates on other pages and they work fine.

  3. dwzemens
    Member
    Posted 7 years ago #

    dansinch,

    What version are you using? I determined that my problem was with the newest version - New Releases: 2.1.1 and 2.0.9.

    I stumbled onto this by uploading a theme that functioned properly on a blog I control that is running on WP 2.1, but it too had the issue when running on the server with WP 2.1.1. I then loaded the theme that was originally acting incorrectly in WP 2.1.1 onto a blog that was running 2.1, and it worked correctly at that server.

    When I revert to WordPress 2.1 Ella on my original problem install the problem is resolved. So, right or wrong, I determined that the newest release was the issue.

    Let me know if that is the problem for you, too.

  4. faxxy
    Member
    Posted 7 years ago #

    I'm having the same issue where the 'Blog' page doesn't have "current_page_item" included in the HTML.

    This is after going into Options > Reading > Front Page and assigning the "Home" page as the static front page and a page called "Blog" as the post page alternative

    I'm using 2.1.1

  5. Andrew Ozz
    WordPress Dev
    Posted 7 years ago #

    Maybe something like this will work:

    <?php /* If this is the frontpage */ if ( $post->ID == 'x' ) { ?>
    <li id="current_page_item"><a href="<?php bloginfo('url'); ?>/?page_id=x">Page-x</a></li>
    <?php } else { ?>
    <li><a href="<?php bloginfo('url'); ?>/?page_id=x">Page-x</a></li>
    <?php } ?>

    where "x" is the page_ID.

  6. dwzemens
    Member
    Posted 7 years ago #

    azaozz, I think you are missing the point. Workarounds such as you suggest were necessary before WP 2.1 recognized a static front page. Now wp_list_pages() is designed to, and should, do this work all by itself.

    dz

  7. Andrew Ozz
    WordPress Dev
    Posted 7 years ago #

    Yes, you're right, wp_list_pages() will output nicely formatted list of all pages on your site and takes quite a few arguments. But if you want to turn this list into a more interactive menu, you'll have to use some of the conditional tags http://codex.wordpress.org/Conditional_Tags and even some simple php to make it all look nice.

    As far as I can tell WP 2.1 now supports 2 special front pages: one is a static home page that you can select with is_home(), and the other is the usual front page that would have been displayed if you didn't have static home page. Although it's created as a Page, the second one is an archive listing of the most recent posts and that's probably why wp_list_pages() doesn't insert the extra "current_page_item" class.

    In any case you can use several ways to detect when this page is displayed, like is_page(x), is_page(Blog), $post->ID=='x', etc. This is actually the full goodness of WP :) . With very limited knowledge of php (like mine) you can make your website any way you like it.

  8. dwzemens
    Member
    Posted 7 years ago #

    I hate to disagree, but with the new version of WP no other code is necessary to facilitate the current_page_item tag. Here is all the code I use at this website that is under construction:

    http://www.stoneycreekwebdesign.com/delta/

    `
    <ul class="btn">
    <?php wp_list_pages('sort_column=menu_order&title_li=&exclude=' ); ?>

    The "Home" page is the static Page assigned through the dashboard to act as the home page. No other tweaks or conditional arguments are used, just the code above. As you can see, each page invokes the current_page_item tag and the correct navigation link is highlighted.

    For some reason, which was the basis for this original post, this was not functioning correctly with the project I was referring to. I think the cause, as I mentioned in later posts, was a bad version of the new WP downloads. It works great with 2.1 Ella, but some of the more recent versions were NOT invoking current_page_item on the Home page.

    Make sense now?

  9. dwzemens
    Member
    Posted 7 years ago #

    Here is the most recent example of the problem. These two different sites are using the EXACT SAME CODE to display the pages, which is simply the wp_list_pages() function. The CSS outputs a subtle underline image below the navigation link identified as current_page_item.

    http://www.stoneycreekwebdesign.com/_wp/?page_id=2
    This site, just loaded today with WP 2.1.2 (newest version), DOES NOT HIGHLIGHT the "Blog Page", which is called "News(Blog)", because it does NOT correctly ouput the current_page_item tag as it should, which should be output by wp_list_pages().

    http://www.troyfamilypractice.com/blog/?page_id=4
    This site, running a clean version of WP 2.1.1 (before the cracker infection) DOES HIGHLIGHT the "Blog" page, which is called "In the News" at this website. The exact same wp_list_pages() CORRECTLY outputs the current_page_item tag at this site.

    So, the problem continues to exist and it is dependant on the version of WP being used.

    Wow, I wish I could solve this.

  10. dwzemens
    Member
    Posted 7 years ago #

    Searching the WordPress bug tracking database, the problem seems to be identified.

    http://trac.wordpress.org/ticket/2959

    If I am reading this correctly, the problem was opened months ago, closed as fixed, and reopened on Feb 27, 2007 as still existing. Is that what it looks like to you?

    dz

  11. StrangeAttractor
    Member
    Posted 7 years ago #

    I have noticed the same problem... my static "blog" page (not the same as my "home") page does not get the "current_page_item" class on its link, and so the menu item is not highlighted.

    I have also noticed that this page doesn't get a page title (i.e. all the pages get a title of myURL >> page title but the blog page only shows the URL with no page title).

    I saw the above post that this has been documented as a bug in 2.1.2 which is the version I'm using.

    My question is -- is there a line of code I can put in my index.php file or somewhere else to fix this? I don't want to wait for the next version of WP to fix it...

    (This is my first post here -- hope I'm following protocol. And, full disclosure: I don't know much about PHP at all.)

  12. krypto
    Member
    Posted 7 years ago #

    Trying to figure this out too. The #2959 bug track indicates that the included patch fixes the problem, but the code seems to be no different from 2.1.2. I can't figure out what I'd need to change to solve this - can anybody help?

  13. baptiste
    Member
    Posted 7 years ago #

    If your theme uses wp_list_pages, then you have to change a core WP file to fix this temporarily (the bug above describes the design work going into fixing this, but two different things were done which nullified the fix. The milestone is v2.2)

    Anyway - to fix this in the core file (there may be a more efficient/faster way, but it works), edit wp-includes/classes and look for:

    if ( $page->ID == $current_page )

    which is line 512 in v2.1.3 and change it to:

    if ( $page->ID == $current_page or $page->ID == get_settings('page_for_posts'))

    Now it should work. But it'll break the next time you update :) I may spin a plugin with a quick filter fix.

  14. baptiste
    Member
    Posted 7 years ago #

    Er well, no that didn't work quite right. Grrrr. Sorry about that. Let me try again....

  15. baptiste
    Member
    Posted 7 years ago #

    Well that's cute. When you are viewing the 'page for posts', the page id is set to 0 instead of the true page ID - that's why things are messed up. But you can't just check for '0' since there are other times the page ID is 0 (searching, etc)

    So it won't be perfect, but you COULD try this on line 512:

    if ( $page->ID == $current_page or $page->ID == 0)

    Proper code would check that get_option('page_for_posts) > 0, but if you're making this hack - I'm assuming you knwo you have a static front page.

    If I come up with a decent fix via plugin, I'll post it

  16. baptiste
    Member
    Posted 7 years ago #

    OK it's late. That should read:

    if ( $page->ID == $current_page or $current_page == 0)

  17. baptiste
    Member
    Posted 7 years ago #

    Nope - that didn't work either. It's 3AM. I give up for the night.

  18. ernman
    Member
    Posted 7 years ago #

    Hi,

    Nice designs dwzemens. I admire your work.

    New member here. Did anyone figure out a temp work around for this.

    I have the same problem with my navigation that dwzemens describes.

    I understand it is a reported bug. Just curious if there was a work around for now. I Have tried many many code suggestions and none of them have been able to make the dynamic nav fix. I don't know much php but have picked my way around in it making small tweaks.

    I would be very grateful if any of ya let me know how you fixed it for now.

    Thanks
    ernman

  19. somlor
    Member
    Posted 7 years ago #

    I ended up just building the menu manually:

    <ul class="menu">
              <li class="<?php if ( is_page('2') ) { ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a href="<?php echo get_settings('home'); ?>/">HOME</a></li>
    
              <li class="<?php if ( is_home() or is_archive() or is_single() or is_paged() or is_search() or (function_exists('is_tag') and is_tag()) ) { ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a href="http://www.siteurl.com/blog/" title="title">BLOG</a></li>
    
              <li class="<?php if ( is_page('another-slug') ) { ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a href="http://www.siteurl.com/another-page/" title="title">ANOTHER PAGE</a></li>
    
    		  ...etc
    </ul>
  20. ernman
    Member
    Posted 7 years ago #

    Thank You somlor !!!

  21. ovgray
    Member
    Posted 7 years ago #

    Try changing line 512 of wp-includes/classes.php to

    if ( $page->ID == $current_page ||
        (0==$current_page && 'page'==get_option('show_on_front') && $page->ID==get_option('page_for_posts'))
    )
  22. abcpeter
    Member
    Posted 7 years ago #

    ovgray, the code you suggested fixes the highlighting when the static 'Posts page' is visited. In oder words - when you visit the static Posts page, the corresponding <li> tag gets a "current_page_item" class.

    But there is another problem. Let's say our static Posts page is called "Blog". So, when you perform a search, or when you visit a category page, the <li> tag for the static Posts page ("Blog") is always highlighted :(
    Is it possible to avoid this problem by adding something to the code you suggested?

  23. ovgray
    Member
    Posted 7 years ago #

    If
    is_home()
    is substituted for
    0==$current_page
    in the code fragment above, the posts page tag is only highlighted when the posts page itself is displayed.

  24. walterego
    Member
    Posted 7 years ago #

    Here's one way to go about, it's hardcoded but at least it's something. On your index.php file change the section you use for listing you pages to:

    <ul>
    	<?php
    	wp_list_pages('sort_column=menu_order&title_li=&depth=1&exclude=THE REPLACE THIS WITH THE ID OF YOUR POSTS PAGE');
    	?>
    	<li class="page_item current_page_item"><a href="LINK TO YOUR POSTS PAGE" title="Weblog">Blog</a></li>
    </ul>

    You can also make this change on your single posts page and other blog pages, this way the link to your posts page is always highlighted when looking at the blog

    Don't forget to replace THE TEXT IN CAPS ;-)

  25. slothdog
    Member
    Posted 7 years ago #

    Is there any way to "vote" on bugs? I'd really like to see this one fixed.

  26. LoganSix
    Member
    Posted 7 years ago #

    The static option worked for me, except when it comes to sub-pages.

  27. analogpanda
    Member
    Posted 6 years ago #

    It also seems that setting the "Front page" and "Posts page" in wpAdmin -> Options -> Reading has broken the proper functionality of the is_home() and is_page() functions.

    (wp ver 2.2.1)

    Since I've set up my front page as: example.com and my posts page as example.com/blog, is_home() evaluates to true when I'm at example.com/blog, and I can't get is_page() to work at all now.

    Can anyone confirm this?

    Perhaps I'm just too sleepy and tired, so I'll double check this in the morning to make sure...

  28. Metaleks
    Member
    Posted 6 years ago #

    I can confirm that analogpanda. I am using the same version as you.

    I have the exact same problem as the original poster. For everyone thats viewing this topic, I think you may need a visual to understand whats going on.

    Take a look at my website: http://www.metaleks.net (I know the pages are messy and incomplete, I just started it)

    Now, try clicking on the About, Home and other pages on the top navigation. You will notice that these pages all hilight appropriately once you are on them. However, now try clicking on Blog, and you will notice that that page DOES NOT hilight. I too, am using a static front page, with a /blog directory to store my posts.

    How can you fix that, so the Blog page hilights?

  29. Metaleks
    Member
    Posted 6 years ago #

    BUMP!

  30. Rebecca Markowitz
    Member
    Posted 6 years ago #

    is there a solution for this?? I tried all the methods mentioned above with no luck. any other ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic