Richard S. Crawford's World
Code monkey by day, word monkey by night...
+ Increase Font | - Reduce Font
  • Home
  • Bloginomicon
  • Writing
  • Stuff
  • Contact
  • About Richard
  • WordPress Plugin: Sub Page Functions

    Plugin Name: Sub Page Functions
    Current Version: 1.0
    Release: 10 March 2007
    Download: sub_page_functions.zip

    I use WordPress throughout my site to organize content. In particular, I’ve found that WordPress “pages” are useful posting the short stories that I’ve written and that I’ve released under Creative Commons licenses. I wanted a particular sidebar to show up on the pages that are stories, and I immediately thought that if I made these stories a subpage of my “Published Stories” page, I could do that easily with a “is_subpage” conditional tag. Unfortunately, there is no such tag built in to WordPress. After experimenting with a few different ideas, I finally decided to create my own tags and create a plugin. “Sub Page Functions” is that plugin.

    About the Plugin

    Sub Page Functions introduces two new tags to use in WordPress templates. The first is a conditional tag which returns a boolean result; the second returns an object containing information about a subpage’s parent page.

    This plugin has been tested with WordPress versions 2.1 through 2.3, and should work with most other versions.

    rsc_is_subpage([$parent_page_id])

    This function determines whether the current page is a subpage or not. The parameter $parent_page_id is optional; if it is passed, then the function determines whether the current page is a subpage of the parent page with the given ID.

    Examples:

    < ?php if (rsc_is_subpage()) echo 'This is a subpage!'; ?>

    This simply prints the phrase “This is a subpage!” if the page displayed is indeed a subpage.

    < ?php
    if (rsc_is_subpage(167)) {
    echo 'This is a subpage of page 167!';
    } else {
    echo 'This is not a subpage of page 167!';
    ?>

    In my own site, I use this function to include a different sidebar for subpages of my “Published Stories” page:

    < ?php
    if (rsc_is_subpage(167)) {
    include ("story_sidebar.php");
    } else {
    include ("other_sidebar.php");
    }
    ?>

    rsc_get_parent()

    This function returns a $post object containing information about the current page’s parent page, if any. If there is no parent page, the function returns false.

    Example:

    < ?php
    if ($parent = rsc_get_parent()) {
    echo "The parent page is $parent->post_title";
    } else {
    echo 'There is no parent page';
    }
    ?>

    Installation

    To install this plugin, unzip the .zip file and upload the file sub_page_functions.php to the wp-content/plugins subdirectory of your WordPress installation. Then go to the Plugins page in your Admin screen, and activate the plugin there. That’s it!

    Print This Page Print This Page

    All original content on this site copyright ©1994 - 2009 by Richard S. Crawford. Unless noted otherwise, all content is protected by a
    Creative Commons Attribution/NonCommercial/NoDerivatives 3.0 license.


    Creative Commons License

    Powered by WordPress