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

Echo Parent Name in Submenu WordPress

I'm using the code below in an accordion menu, see here: http://themeforward.com/demo2/?page_id=2535 on the right-hand side

I would like to echo the name of the parent categories so that I can include *Parent Name Overview* as the first menu item in each dropdown. However, this should be done ONLY for parents with children that are already being shown in my menu.

For example, visit my example and click criminal law. I want the first link to be "Criminal Law Overview"

Tell me how to alter my code (below), please.

<?php
$parentid = $post->post_parent ? @ array_pop( get_post_ancestors( $post ) ) : $post->ID;
$children = wp_list_pages(
array(
'child_of' => $parentid,
'depth' => 2,
'title_li' => '',
'echo' => false,
));

if ( $children ) : ?>
<ul id="menu-practice_areas" class="sbmenu">
<li><a href="<?php echo get_permalink( $parentid ) ?>"><?php echo get_the_title( $parentid ) ?></a></li>
<?php echo $children ?>
</ul>
<?php else : ?>
<?php endif ?>

Answers (3)

2014-04-04

Luis Abarca answers:

Try this code


<?php
$parentid = $post->post_parent ? @ array_pop( get_post_ancestors( $post ) ) : $post->ID;
$children = wp_list_pages(
array(
'child_of' => $parentid,
'depth' => 2,
'title_li' => '',
'echo' => false,
));

if ( $children ) :
$parent_cat = get_category($parentid);
?>
<ul id="menu-practice_areas" class="sbmenu">
<li><a href="<?php echo get_category_link( $parent_cat->cat_ID ) ?>"><?php echo $parent_cat->cat_name ?></a></li>
<?php echo $children ?>
</ul>
<?php else : ?>
<?php endif ?>


siouxfan45 comments:

That did not work.

2014-04-04

Firoja_Imtosupport answers:

Hi,

Please can you give me your skype id and i will sort it out.

Thanks

2014-04-05

Bob answers:

Here is code which will get all parent pages loop through it and print child pages under it. It will print first element of child <ul> same as parent page.

<?php
// let's get all parent pages
$pages = get_pages(array('parent' => 0 ));
?>
<ul id="menu-practice_areas" class="sbmenu">
<?php
// loop through all paren t pages
foreach ( $pages as $page ) { ?>
<li><a href="<?php echo get_permalink( $page->ID ) ?>"><?php echo get_the_title( $page->ID ) ?></a>
<?php $subpages = get_pages('child_of=' . $page->ID); // get child pages
if($subpages){ // if child pages found
?>
<ul>
<li><a href="<?php echo get_permalink( $page->ID ) ?>"><?php echo get_the_title( $page->ID ) ?> Overview</a></li><!--parent page again-->
<?php
wp_list_pages( array(
'child_of' => $page->ID,
'depth' => 2,
'title_li' => '',
'echo' => true,
));
?>
</ul>
<?php
}

?>
</li>
<?php } ?>
</ul>