ThemeShaper Forums » Thematic

[closed]

Function for current_page in Body Class Instead of Menu

(12 posts)
  • Started 4 years ago by nimrod
  • Latest reply from nimrod
  • This topic is not resolved
  1. nimrod
    Member

    Is there a function that can put a "current_page" class in the body tag when viewing the current page?

    I see that current_page normally this gets placed in the menu. I'm obliged to manually construct my menu and so cannot dynamically generate current_page in the menu.

    EXAMPLE

    With the working function then, the body tag would look something like this on the current page.

    <body class="current_page wordpress y2010 m05 d30 h20 singular pageid-25 [... etc.]">

    NOTE

    I need to keep the other automatically generated class designations in the body tag since I'm using others. So, I don't want to simply replace the body tag class with only current_page in the class. I want to keep what gets generated now and add current_page to the body class.

    Posted 4 years ago #
  2. nimrod
    Member

    This solution adds current_page (or just page_item if not current page) into a manual menu (see code below).

    Wondering though if there's a way to also include current_page_parent and current_page_ancestor awareness?

    function childtheme_menu() { ?>
    <div id="menu">
    <ul>
    	<li class="<?php if ( is_page('about') ) { ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a href="<?php echo get_option('home') ?>/about/" title="About This Blog">About</a></li>
    	<li class="<?php if ( is_page('advertising') ) { ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a href="<?php echo get_option('home') ?>/advertising/" title="Advertise on My Blog">Advertise</a></li>
    	<li class="<?php if ( is_page('contact') ) { ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a href="<?php echo get_option('home') ?>/contact/" title="Contact Me">Contact</a></li>
    </ul>
    </div>
    
    <?php }
    
    add_filter( 'wp_page_menu', 'childtheme_menu' );
    Posted 4 years ago #
  3. cannobbio
    Member

    Why do you want a class "current_page" in your body?, I don't get it.
    I use Subpage View Plugin (http://wordpress.org/extend/plugins/subpage-view/) which make use of wp_list_pages to display subpages. This preserves the current, parent and ancestor classes.

    http://codex.wordpress.org/Template_Tags/wp_list_pages

    Posted 4 years ago #
  4. @nimrod...
    did your "if" statements work?
    I'm needing the same thing....
    Sorry @cannobbio, not EVERYTHING works w/ wp_list_pages.

    Posted 4 years ago #
  5. nimrod
    Member

    @cannobbio: I'm using a manually created unordered list for my nav with images for buttons. So i've broken out of the usual and more flexible build-in method that would add current_page_item, current_page_parent, and current_page_ancestor designations to the nav li's individual classes.

    Incidentally, I abandoned current_item in the body class for the code I had posted in my second reply above.

    @hoss9009: Yes, the if statements in my manually created nav code do work fine. However, I'd still like current_page_parent and current_page_ancestor designations in the appropriate li classes. can't figure out how to make my nav that smart without writing a Byzantine labyrinth of multiple if statements for each child li menu item.

    Posted 4 years ago #
  6. Howdy,

    I'm seeing your custom nav menu and remembering days past when I did exactly that in a most complex and frustrating way.

    My question to you is this why not wait a couple of days for WP 3.0 to be released and upgrade to use the new custom nav-menu feature?

    -Gene

    Posted 4 years ago #
  7. nimrod
    Member

    Well, good question. Two reasons:

    1) WP 3.0 is a significant new release (especially for menus) and I would not want to trust it so soon to client projects (this includes the core as well as themes and frameworks, in my case I'm using Thematic).

    2) I wonder if WP 3 would really make an all-graphic menu any easier to produce. I assume it'll be tuned to text menus based on the pages and categories, etc. created in the database.

    Even if it does facilitate customized menus in some way, will it accommodate graphic buttons, with current_page and parent_page functionality, including rollover sprites, plus intelligent drop menu effects that hold drop menus on sub-pages, and also maintain text for the menus in the HTML for Google indexing purposes while hiding the text too... Whew!

    Posted 4 years ago #
  8. nimrod
    Member

    Also, I found a relatively easy method of adding parent_page classes to the parent pages when appropriate. It is simple actually:

    In the same way you can identify a current page by using is_page to determine what page is currently showing, you can identify parent_page in the same way.

    The example below shows a scenario where an About page exists with a child page set in WP Pages called About Sub Page.

    The About page code is set to display current_page_item in its class if we're viewing the About page or will display current_page_parent if we're viewing the About Sub Page. If there are more sub-pages, then simply add more identifying is_page code to the parent class as needed.

    This then allows me to set my button image sprites to remain in the rollover state to indicate the current page AND to keep drop menus displayed when in parent sections.

    More info and code for fancy drop menus for Thematic are referenced here:
    http://themeshaper.com/wordpress-menu-tricks/
    http://forums.themeshaper.com/topic/create-horizontal-dropdown-child-menu-that-holds-on-subpage

    Here's the code example:

    function childtheme_menu() { ?>
    <div id="menu">
    <ul>
    	<li class="<?php if ( is_page('about') ) { ?>current_page_item<?php } else { ?>page_item<?php } ?> <?php if ( is_page('about-sub-page') ) { ?>current_page_parent<?php } ?>"><a href="/about/" title="About This Blog">About</a>
    		<ul>
    			<li class="<?php if ( is_page('about-sub-page') ) { ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a href="/about/about-sub-page/" title="About Sub Page">About Sub Page</a></li>
    		</ul></li>
    	<li class="<?php if ( is_page('advertising') ) { ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a href="/advertising/" title="Advertise on My Blog">Advertise</a></li>
    	<li class="<?php if ( is_page('contact') ) { ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a href="/contact/" title="Contact Me">Contact</a></li>
    </ul>
    </div>
    
    <?php }
    
    add_filter( 'wp_page_menu', 'childtheme_menu' );
    Posted 4 years ago #
  9. I'm glad you found a solution that works for you :)

    Personally, I'll take an automatically generated nav function over a hand coded nav any day. The major benefits being that I can apply the same code and the same methods to many separate projects quickly and efficiently. Custom coding a nav is fine for one site but what about for 5 different sites. What a pain.

    Also when the client wants to change their nav copy or make some minor tweak to order or nesting, I don't want to have to go back to a text editor and rework it every time.

    Not exactly a beginners task but there is a tremendous amount of customization that you can do by simply setting the nav function's echo parameter to false and then filtering its output with regex to add, remove, or replace whatever you need to before echoing the nav.

    I don't think there is anything to worry about with the new nav function in WP3.0. It still generates basically the same markup as the old nav functions and the rest really depends on the markup in the theme and how you plan on going bout building a graphic menu.

    -Gene

    Posted 4 years ago #
  10. nimrod
    Member

    Thanks for the valued input, Gene!

    Rest assured, I avoid hand-crafted graphic button navs whenever possible!

    I'm intrigued by filtering nav output with regex. I did a few searches around the terms you provided, but didn't come up with anything comprehensible in relation to navs.

    Would you happen to know of any tutorials or explanations online to direct me to that might relate to WordPress?

    Regardless, thanks again for the helpful responses!

    Posted 4 years ago #
  11. Regex is short for regular expressions. The php functions you'd want to look at to start would be: preg_replace
    str_replace

    Try searching Google for those function names in combination with these wordpress nav functions: wp_list_pages
    wp_list_categories

    I think you'll find some good tutorials searching those terms. If you can wrap your head around regex you'll be well on your way.

    Posted 4 years ago #
  12. nimrod
    Member

    I really appreciate your help, Gene. Thanks again!

    Posted 4 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.