WordPress Mark Parent Pages Plugin

Update: This plugin is not needed in 2.5, it has been included in the WordPress core.

This plugin uses DOM to add a class (current_page_ancestor) to the li tag of each page that is an ancestor of the current one. With a little CSS, you can have nice collapsing menus. This code assumes that your pages are listed using the usual wp_list_pages function, and that they are in an element with the id of nav.

#nav ul li.current_page_item ul,
#nav ul li.current_page_ancestor ul {
    display:block;
}
#nav ul li.current_page_item ul ul {
    display:none;
}

I wanted collapsing menus in WordPress, not Javascript based, but something that would display only top level pages AND pages that are either children of the current page or children of the ancestors of the current page. It may sound confusing, but it’s really quite logical. Take a look:

Given a page structure like this:

Web Programming
> WordPress
> > Themes
> > > CSS
> > > JavaScript
> > > PHP
> > Plugins
> > Functions
> CubeCart
> Gallery
Templates
> Free Designs
Portfolio
> Sites
About
Contact

On the main page, I’d like to display all the top level pages: Web Programming, Templates, Portfolio, About, Contact

On page ‘Web Programming’ I would like to display:

Web Programming
> WordPress
> CubeCart
> Gallery
Templates
Portfolio
About
Contact

On page ‘WordPress’ I would like to display:

Web Programming
> WordPress
> > Themes
> > Plugins
> > Functions
> CubeCart
> Gallery
Templates
Portfolio
About
Contact

On page ‘Themes’ I would like to display:

Web Programming
> WordPress
> > Themes
> > > CSS
> > > JavaScript
> > > PHP
> > Plugins
> > Functions
> CubeCart
> Gallery
Templates
Portfolio
About
Contact

The problem was that to display the pages like that, I really needed to know all ancestors of the current page. Now that this plugin classes those, I can do exactly what I wanted.

About Aaron D. Campbell

Owner and lead developer at BlueDog, Aaron has 10+ years of web development experience, it a regular core contributor to the WordPress project, and has released many WordPress plugins.
This entry was posted in Wordpress and tagged . Bookmark the permalink.

11 thoughts on “WordPress Mark Parent Pages Plugin

  1. Doug Stewart says:

    Looks like your server is interpreting the .php file literally, thus I’m just getting blank pages when trying to download. Maybe it needs to be presented as .src or .txt?

  2. Thank you for pointing that out. I changed the attachment to a .phps file. If you want to save it, just right click and save. Otherwise, the link will display the contents as color coded PHP source code.

  3. Doug Stewart says:

    Chalk it up to blatant self interest — I want to use the plugin! *grin*

    Additionally, maybe I’m losing my marbles a bit here, but isn’t a closing PHP short tag missing from the end of the file?

  4. Actually, it’s optional, and after doing a fair bit of work with Zend Framework, I’ve come to appreciate their coding standards.

    For files that contain only PHP code, the closing tag (“?>”) is never permitted. It is not required by PHP. Not including it prevents trailing whitespace from being accidentally injected into the output.

  5. Adrien says:

    Could you explain how to use it?
    Sorry but i can’t find how to apply the “current_page_ancestor” class to my content…

    Regards
    Adrien

  6. Adrien says:

    Ooops sorry, it was because of the “missing the closing tag (”?>”)”

  7. Ryan Foote says:

    Hey I’m getting the following error when attempting to activate the plugin:


    Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in PATH/wp-mark-parent-pages.php on line 35

  8. Ryan Foote says:

    Oh, also, whoops! I’m using version 2.3.3 of wordpress.

  9. All PHP snippets, plugins, etc that you find on this site require PHP5.2 as per the Go PHP 5 directive.

  10. Ryan Foote says:

    Ahh yeah that would explain it, thanks for the quick reply!