David Turner

WordPress Snippits I Find Useful

Now that I'm back online after the dreaded internet outage of twenty ten, I thought I'd share a couple of little bits of WordPress related Code that I've stumbled across, tweaked or coded up myself to make WordPress behave exactly how I want it to.

I'm not sure if anyone will find them useful or not, but I don't see any point in keeping little bits of code to myself when they might help someone else out.

SubPage Redirection

This is a little snippit of code that I discovered whilst working on a client's site. Their About section was broken down into multiple sections, with each defined as a sub-page of an About Page. This left the About Page itself lacking in content. This left me with two options:

  1. Load some content into the About Page
    • This runs into a potential issue with duplicate content, which would be bad from a SEO perspective
  2. Redirect the About Page through to a subpage

In the end I opted for the second option, to avoid any SEO issues that duplicated content might cause. To do this, I made use of the following piece of PHP, placed before any HTML, to redirect to the first subpage of a page:

    if (have_posts()) {
      while (have_posts()) {
        $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
        if ($pagekids) {
          $firstchild = $pagekids[0];

What this code does is check to see if the page exists. If it does, it then checks to see if there are sub-pages. If there are it redirects to the first of the sub-pages. Pretty quick and easy, and rather effective.


Listing Sub and Sibling Pages

This snippit of code also ties in to a client project I was working on. The About Page needed a dynamic list of children pages and, if you were on a child page, to list it's siblings. The following piece of code does this and a little bit more:

    /* if the current pages has a parent, i.e. we are on a subpage */
        //$parent_title = get_the_title($post->post_parent);
        $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable

    /* else if the current page does not have a parent, i.e. this is a top level page */
    else {
        $parent_title = get_the_title($post->ID);
        $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); // form a list of the children of the current page

    if ($children && !is_search()) { ?>
      <ul class="submenu">
        <?php echo $children; /*print list of pages*/ ?>
    <?php } ?>

What this code does is check to see if we are on a parent or child page. If we are on one of these types of page, a query is run to list the child/sibling pages. These pages are then displayed in a list. The heading, courtesy of the previous snippit, is defined as the parent page's title. If you don't use the first snippit then some additional work will be necessary, but should consist of merely uncommenting the piece of code in the if statement.


Wrapping up (for now)

I hope you find some of these snippits useful for the #liveproject or in any future projects you might end up working on using WordPress. If you have any of your own little bits of code you'd like to share, I'm sure that there are people out there who would love to see them.

Especially me.