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

php navigation list parent and child only WordPress

  • SOLVED

Hi,
I'd like to change my sidebar navigation which currently lists all ancestors to just parent and child without any siblings. I'd also like to deactivate the link on the current page in the navigation.
So for example my parent page is 'Animals' and underneath 'Animals' is 'Dog', 'Cat', 'Horse'. Under 'Dog' is 'Puppy' etc.
If I'm on 'Dog' the navigation should display the parent 'Animals' underneath 'Dog' (which is unlinked because I'm on that page) and not display 'Horse' and 'Cat'.
If I'm on 'Puppy' the navigation should display 'Animals' and 'Dog' and 'Puppy' should be unlinked.
I hope that's clear?!
The current code is:

<?php
global $post;

if ( $post->post_parent ) {
$ancestors = get_post_ancestors( $post->ID );
$parent = array_pop( $ancestors );
} else {
$parent = $post->ID;
}
?>
<ul class="g1-menu">
<?php wp_list_pages( 'title_li=&link_before=<span>&link_after=</span>&include=' . $parent ); ?>
<?php wp_list_pages( 'title_li=&link_before=<span>&link_after=</span>&child_of='. $parent ); ?>
</ul>

Answers (4)

2015-03-28

timDesain Nanang answers:

Hi, you can try this code:

<?php
$current = get_the_ID();
$ancestors = get_ancestors( $current, 'page' );
array_push($ancestors, $current);

$args_pages = array(
'title_li' => '',
'include' => $ancestors ,
'link_before' => '<span>',
'link_after' => '</span>',
'echo' => 0 ,
);
$list_pages = wp_list_pages( $args_pages );
$list_pages = preg_replace('/<li class="((.*)?current_page_item)"><a(.*)><span>(.*)<\/span><\/a><\/li>/i', '<li class="$1">$4</li>', $list_pages);
?>

<ul class="g1-menu">
<?php echo ($list_pages);?>
</ul>


If you want to show the children of the current page:

<?php
$current = get_the_ID();
$ancestors = get_ancestors( $current, 'page' );
array_push($ancestors, $current);

$args_pages = array(
'title_li' => '',
'include' => $ancestors ,
'link_before' => '<span>',
'link_after' => '</span>',
'echo' => 0 ,
);
$list_pages = wp_list_pages( $args_pages );
$list_pages = preg_replace('/<li class="((.*)?current_page_item)"><a(.*)><span>(.*)<\/span><\/a><\/li>/i', '<li class="$1">$4###</li>', $list_pages);


$args_childs = array(
'title_li' => '',
'child_of' => $current ,
'link_before' => '<span>',
'link_after' => '</span>',
'depth' => 1 ,
'echo' => 0 ,
);
$list_childs = wp_list_pages( $args_childs );

if(!empty($list_childs))
$list_childs = '<ul class="children">'.$list_childs.'</ul>';

$list_pages = str_replace('###', $list_childs, $list_pages);
?>

<ul class="g1-menu">
<?php echo ($list_pages);?>
</ul>


Ailsa Craig comments:

Hi timDesain,
That's it exactly! Thanks so much.

2015-03-26

Rowela Alzona answers:

Can you try this one:


<?php $cats = get_categories('child_of='.get_query_var('cat'));

foreach ($cats as $cat) :

$args = array(
'posts_per_page' => 3, // max number of post per category
'category__in' => array($cat->term_id)
);
$my_query = new WP_Query($args);

if ($my_query->have_posts()) :
echo '<h3>'.$cat->name.'</h3>';

while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php /*general loop output; for instance: */ ?>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a> <br />

<?php endwhile; ?>

<?php else :
echo 'No Posts for '.$cat->name;
endif;

endforeach; ?>


Let me know if this worked.


Ailsa Craig comments:

Hi Ellah,
That doesn't work. This just lists my posts and categories. I have a listing of pages. Don't know if that makes a difference?

2015-03-26

Romel Apuya answers:

try this
<?php
global $post;
$page_title = get_the_title($post->ID);
if($post->post_parent) {
$parent_id = $post->post_parent;
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach($breadcrumbs as $crumb){
echo $crumb;
}
}

?>


Romel Apuya comments:

here tested..

<?php
global $post;
$page_title = get_the_title($post->ID);
$current = '<a href="#">' .$page_title . '</a>';
if($post->post_parent) {
$parent_id = $post->post_parent;
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
$breadcrumbs[] = $current;
foreach($breadcrumbs as $crumb){
echo $crumb.'<br/>';
}
}
?>


Ailsa Craig comments:

Hi Romel,
Thanks. I think we're getting there but not everything is showing up. If I click on say 'Animals' the children (horse, cat, dog) aren't showing up at all in the menu. The current page (unlinked) also isn't showing up in the menu. Would there be a way to display this?
Thanks.


Romel Apuya comments:

here

<?php
global $post;
$page_title = get_the_title($post->ID);
$current = '<a href="#">' .$page_title . '</a>';
if($post->post_parent) {
$parent_id = $post->post_parent;
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
$breadcrumbs[] = $current;
foreach($breadcrumbs as $crumb){
echo $crumb.'<br/>';
}
}else{ ?>
<ul class="g1-menu">
<?php wp_list_pages( 'title_li=&link_before=<span>&link_after=</span>&include=' . $post->ID ); ?>
<?php wp_list_pages( 'title_li=&link_before=<span>&link_after=</span>&child_of='. $post->ID ); ?>
</ul>
<?php }
?>


Ailsa Craig comments:

Hi Romel,
It's definitely getting better! The children of a particular section still aren't appearing but the parents and grandparents are now. I wanted to disable the link completely if that's possible? not have the text look like a link (#).
Probably best if you see the website.
http://www.new.customercarewords.com/about
The sections I'm working on are the About section and Services.
Thanks.


Romel Apuya comments:

maybe you can replace this part

$current = '<a href="#">' .$page_title . '</a>';

to

$current = '<span">' .$page_title . '</span>';

"The children of a particular section still aren't appearing but the parents and grandparents are now."
ok ill work on it

2015-03-27

Jayaram Y answers:


Jayaram Y comments:

Hi. Please check this like. I have done a slimilr thing as your needs...

http://webboxtech.com/teachertraining/teacher-training-english/

please check the navigation on the lfet sidebar... click on the 2nd menu item in sidebar.


Jayaram Y comments:

Hi. Please check this link. I have done a similar thing as your needs...

http://webboxtech.com/teachertraining/teacher-training-english/

please check the navigation on the left sidebar... click on the 2nd menu item in sidebar.


Ailsa Craig comments:

Hi Jayaram,
It's not what I was looking for. In my scenario all the other links would disappear. But thanks anyway.