Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom sidebar menu WordPress

  • SOLVED

Hi, I need a sidebar and menu for wp 3.0 that does the following.

<strong>1. If there are no children pages - do not load the sidebar at all

2. If there are children pages show only the children pages in the sidebar menu. This applies if the page is itself a child page - show only sub-pages.

3. If there are no children - but there is a parent - show the same sidebar menu that the parent page shows.</strong>

hopefully this makes sense.

Thanks for your efforts.
Connie


Answers (5)

2010-11-11

idt answers:

This will display sub-pages if it has, if it doesn't it will display siblings(menu that the parent page shows). If no siblings and no sub-pages, nothing will be displayed.

<?php
if (is_page()) {
if($post->post_parent){
// will display the subpages of this top level page
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if (!$children) {
$children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0&depth=1');
}
} else {
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");
}
?>
<?php if ($children) { ?>
<ul class="subpages">
<?php echo $children; ?>
</ul>
<?php }
}
?>


Connie Taylor comments:

Nice - this works "Exactly" as I want it to. could you extend a small favor and tell me if I could use any part of this code to determin what div to load for the content? Without the sidebar I will need to load different styles to set the width of the content area. Thank you? - you win :)


Connie Taylor comments:

Help please - this only seems to work two levels down. Now that we are starting with content I find that the client wants children of children of children pages:

top
- child page
--child page
---child page

How can I ensure that this will drill down as far as they go?


Connie Taylor comments:

You can see the issue on this page:

http://www.brandmagik.com/gt/?page_id=35

2010-11-11

Michael Fields answers:

I believe that this is what you are looking for:

[[LINK href="http://wordpress.mfields.org/2010/selective-page-hierarchy-for-wp_list_pages/"]]http://wordpress.mfields.org/2010/selective-page-hierarchy-for-wp_list_pages/[[/LINK]]


Connie Taylor comments:

Looks like it might work but I have some questions about implementation. Wordpress.org's reference page for the walker class [[LINK href="http://codex.wordpress.org/Function_Reference/Walker_Class"]][[/LINK]] sites that it was part of wp 2.2 - all of the links inside that page's content show "page not found" So does this class exist in 3.0?

Next question is how do I implement this and where do I put the code.

Thanks


Michael Fields comments:

The Walkers are alive and kicking in WordPress 3.0! The code in the link was tested against 3.0 as well. You need to stick the class definition in functions.php:
/**
* Create HTML list of pages.
*
* @package Razorback
* @subpackage Walker
* @author Michael Fields <[email protected]>
* @copyright Copyright (c) 2010, Michael Fields
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
* @uses Walker_Page
*
* @since 2010-05-28
* @alter 2010-10-09
*/
class Razorback_Walker_Page_Selective_Children extends Walker_Page {
/**
* Walk the Page Tree.
*
* @global stdClass WordPress post object.
* @uses Walker_Page::$db_fields
* @uses Walker_Page::display_element()
*
* @since 2010-05-28
* @alter 2010-10-09
*/
function walk( $elements, $max_depth ) {
global $post;
$args = array_slice( func_get_args(), 2 );
$output = '';

/* invalid parameter */
if ( $max_depth < -1 ) {
return $output;
}

/* Nothing to walk */
if ( empty( $elements ) ) {
return $output;
}

/* Set up variables. */
$top_level_elements = array();
$children_elements = array();
$parent_field = $this->db_fields['parent'];
$child_of = ( isset( $args[0]['child_of'] ) ) ? (int) $args[0]['child_of'] : 0;

/* Loop elements */
foreach ( (array) $elements as $e ) {
$parent_id = $e->$parent_field;
if ( isset( $parent_id ) ) {
/* Top level pages. */
if( $child_of === $parent_id ) {
$top_level_elements[] = $e;
}
/* Only display children of the current hierarchy. */
else if (
( isset( $post->ID ) && $parent_id == $post->ID ) ||
( isset( $post->post_parent ) && $parent_id == $post->post_parent ) ||
( isset( $post->ancestors ) && in_array( $parent_id, (array) $post->ancestors ) )
) {
$children_elements[ $e->$parent_field ][] = $e;
}
}
}

/* Define output. */
foreach ( $top_level_elements as $e ) {
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
}
return $output;
}
}


and then you will want to add the following line to sidebar.php or which ever file you are using to display your sidebar:
$walker = new Razorback_Walker_Page_Selective_Children();
wp_list_pages( array(
'title_li' => '',
'walker' => $walker,
) );


Hope this helps. Feel free to let me know if you have any other questions.

2010-11-11

Jim Dugan answers:

This is the code I use.


if($post->post_parent)
/* if this post has a post_parent make a linked list of the children called $children */
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
/* OR if this post IS a post_parent */
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
/* if $children is not empty, make the navblock */
?>
<li class="childnav">
<ul>
<?php echo $children; ?>
</ul>
</li>
<?php } ?>


Jim Dugan comments:

This is the code I use.


if($post->post_parent)
/* if this post has a post_parent make a linked list of the children called $children */
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
/* OR if this post IS a post_parent */
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
/* if $children is not empty, make the navblock */
?>
<li class="childnav">
<ul>
<?php echo $children; ?>
</ul>
</li>
<?php } ?>


Connie Taylor comments:

so if the child page - has no children of it's own - will this display the menu shown on it's parent's sidebar?

2010-11-11

Nilesh shiragave answers:

It will display subpages as you want


<?php
if($post->post_parent) {
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&sort_column=menu_order");
$title_heading = get_the_title($post->post_parent);
}

else {
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>


Nilesh shiragave comments:

Try this it is 100% working solution


<?php
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&sort_column=menu_order");
if(!$children)
{
if($post->post_parent)
{
$children =wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&sort_column=menu_order");
}
}
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>

2010-11-12

rilwis answers:

Here is my simple solution:

if (is_page()) {
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
echo "<ul>$children</ul>";
} elseif ($post->post_parent) {
wp_list_pages("title_li=&child_of=".$post->post_parent);
}
} else {
wp_list_pages("title_li="); // comment out this line if you don't want to display all pages when you're on homepage
}