Skip to:
Content
Pages
Categories
Search
Top
Bottom

Functions for generating a menu of WordPress pages


  • TonyVitabile
    Member

    @tonyvitabile

    OK, here’s the functions.php file that I created in my theme to reproduce the drop down menu I had on my WordPress side.

    Please note that this code does not attempt to reproduce all of the functionality of the WordPress wp_list_pages() function. Rather, it just tries to create the HTML needed for the drop down menu code that came with my WordPress theme to work. If you need anything more than that, feel free to modify this or strike out on your own.

    Overview

    Before the code, just a quick overview of how it works.

    The function bb_list_pages() is a recursive function that returns a string containing the HTML for all of the pages that descend from a particular ancestor. The process is started by calling it with a parent ID of 0. The way the wp_posts table in WP is designed, this returns the highest level.

    The function calls a helper function called get_pages() to retrieve the list of child pages from the database. It then loops through all of the pages returned by get_pages() & constructs the list item & anchor tags. It then calls itself to build the HTML for any descendants of the current page.

    Here’s the code:

    <?php
    /**
    * This file contains useful functions that can be called in any of the template's files.
    *
    * Version 1.0
    * Date: 23-July-2009
    * Author: Tony Vitabile
    */

    function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://".$_SERVER["SERVER_NAME"];
    if ($_SERVER["SERVER_PORT"] != "80") {
    $pageURL .= ":".$_SERVER["SERVER_PORT"];
    }
    $pageURL .= $_SERVER["REQUEST_URI"];

    return $pageURL;
    }

    /**
    * Compute the name of the WordPress pages table & return it
    */
    function pages_table() {
    global $bb;

    // Compute the name of the table we need to query
    $table = $bb->wp_table_prefix;
    if ($bb->wordpress_mu_primary_blog_id != "")
    $table .= $bb->wordpress_mu_primary_blog_id."_";
    $table .= "posts";
    return $table;
    }

    /**
    * Retrieve a list of pages from the WordPress posts table whose parent has the ID
    * passed to this function.
    *
    * @param int $parent ID of the page that is the parent of the ones we're searching for
    * @return array List of pages matching defaults or $args
    */
    function get_pages($parent = 0) {
    global $bbdb;

    // Compute the name of the table we need to query
    $table = pages_table();

    // Build our query string
    $parent = (int) $parent;
    $query = "SELECT * FROM {$table} WHERE post_type = 'page' AND post_status = 'publish' AND post_parent = {$parent} ORDER BY menu_order";

    // Get an array of rows back from the database
    $pages = $bbdb->get_results($query);

    // Return whatever we got back to the caller
    return $pages;
    }

    /**
    * Simple function to recursively scan the WordPress posts table looking for pages.
    * It builds a string consisting of <ul><li></li>...</ul> items.
    *
    * @param int $parent ID of the parent page. 0 = no parent
    * @param int $depth How far down in the heirarchy to go
    * @param string $thisPage The name of the page that is currently being displayed
    * @returns string A <ul><li></li>...</ul> list of page navigation information
    */

    function bb_list_pages($parent = 0, $depth=0, $parent_uri='', $indent=' ') {
    // Initialize the output of the function
    $output = "";

    // Is the depth = 0?
    if ($depth == 0) {
    // It is. Return the empty string now
    return $output;
    }

    // Get the child rows of $parent
    $pages = get_pages($parent);

    // Did we get any pages back?
    if ( empty($pages) ) {
    // No, we didn't. Return the empty string now
    return $output;
    }

    // Yes, we got pages back. Loop through all of the pages in our results
    foreach ( $pages as $page ) {
    // Compute this page's URI
    $page_uri = $parent_uri;
    if (substr($page_uri, -1) != '/')
    $page_uri .= "/";
    $page_uri .= $page->post_name . "/";

    // Build the <li> tag
    $output .= "{$indent}<li class="page_item page-item-{$page->ID}";
    if ($page_uri == curPageURL() ) {
    $output .= " current_page_item";
    }
    $output .= "">";

    // Now build the rest of this item's information
    $output .= "<a href="{$page_uri}">".$page->post_title."</a>n";

    if ($page->ID > 0) {
    // Get this page's children recursively
    $kids = bb_list_pages($page->ID, $depth -1, $page_uri, '', $thisPage, $indent . " ");

    // Does this page have any children?
    if ($kids <> "") {
    // It does. Add the information for the kids surrounded by <ul></ul> tages
    $output .= "{$indent}<ul>n" . $kids . "{$indent}</ul>n";
    }
    }

    // Output the closing </li>
    $output .= "</li>";
    }

    // Return the string to the caller
    return $output;
    }

    ?>

    Tony

Viewing 10 replies - 1 through 10 (of 10 total)

  • chrishajer
    Participant

    @chrishajer

    I would think that would be useful as a plugin for people wanting to integrate their WordPress page navigation into bbPress. has anyone tried it out?

    where will you put this file. and how would you link it to the theme? I would love to try it but I am not sure how to address it.


    chrishajer
    Participant

    @chrishajer

    If you are using 1.0, create a file called functions.php in your current theme directory, and put all that code into it.

    If you’re using less than 1.0, you can create a plugin from that code. Just add a plugin header to the top of all that, like this:

    <?php
    /*
    Plugin Name: WordPress pages for bbPress
    */
    ?>

    And put it into file called something like wordpress-pages.php and drop that into folder called my-plugins at the same level as bb-plugins. If the folder is not there, you can create it. After it’s in the folder, log into the admin, you should see it there, then activate it. And report back how it worked for you.

    Thanks

    Hello chrishajer,

    I am running WordPress 2.8.2 and BBPress 1.0. I have put it into a file called functions.php in the kakumei dir. because I am using the kakumei theme.

    I noticed that the new functions file is address to wordpress mu. and I am not running wordpres mu. what should I do now. what code should I put in the header to call that functions to deliver the header. I know also that I neeed to change “wordpress_mu_primary_blog_id” to some that call the regular wordpress but what itis?

    THANK U for sparing some time and help with me!

    So.. is there a BBpres and WordPress integration in the site:

    http://granadagazette.com

    aprendedor

    This is how I understand the code:

    function pages_table() {

    global $bb; //calls global variable

    // Compute the name of the table we need to query

    $table = $bb->wp_table_prefix; //this calls the wp prefix from the wordpress integration settings, ex: wp_

    if ($bb->wordpress_mu_primary_blog_id != “”) //checks for a WPMU id in the WP integration settings

    $table .= $bb->wordpress_mu_primary_blog_id.”_”; //appends the id, ex: wp_1

    $table .= “posts”; //appends “posts”, ex: wp_posts or wp_1posts

    return $table;

    }

    To answer your questions the code works no matter which type of WP flavor you are using

    One other note, unlike wp_list_pages() you need to call this function with echo:

    <?php echo bb_list_pages();?>

    or modify the last few lines of the function from:

    // Return the string to the caller

    return $output;

    to:

    // Return the string to the caller

    echo $output;

    return;

    then you can call the function similar to wp_list_pages()

    <?php bb_list_pages();?>


    Rose
    Member

    @thorned-rose

    Does anyone know if this still works with the current incarnations of wordpress and bbpress? I’ve tried it but it comes up with nada (using the default bbpress template with <?php echo bb_list_pages();?> added).


    mr_pelle
    Participant

    @mr_pelle

    The code above has some errors in it: $thisPage is used when calling bb_list_pages() recursively, but this param isn’t defined anywhere but in the function’s description… =P In function definition that param is instead named $parent_uri.

    Moreover $kids <> "" should be replaced by !empty($kids) and I’m not sure that wordpress_mu_primary_blog_id has any meaning now that WP 3.0 has merged WP and WPMU…

    Last, calling bb_list_pages() with no args, makes if ($depth == 0) true, resulting in an empty string returned. It would be good to use the same template of wp_list_pages(), which lists all pages if no param is passed to it (see docs). By the way, this could be the reason why you get nothing displayed when calling the function.


    Rose
    Member

    @thorned-rose

    I had my husband look at it, who’s far more php savvy than I am and while we were able to make some headway and get it partially working thanks to what you provided Mr Pelle. However, we realised it was a lot of work to get it working with WP3 so I ended up going with Pixopoint Theme Generator to spit out the html for the menu and then made a cufon mini-plugin for the custom font. Far less styley but a lot faster and well at this point, at least it works lol.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.
Skip to toolbar