Make dynamic child page link menu in Wordpress
So go to page.php and uncomment page links
In this feature we will use wp_list_pages() function to get done this. this function return all pages from wordpress.
First remove hardcoded li from menu and use function like below ->
<ul class="min-list">
<?php wp_list_pages() ?>
<!-- <li class="current_page_item"><a href="#">Our History</a></li>
<li><a href="#">Our Goals</a></li> -->
</ul>
and save check the page.
Now we will customize with argument like below
<ul class="min-list">
<?php
if($theParent) {
$findChildrenOf = $theParent;
}else {
$findChildrenOf = get_the_ID();
}
wp_list_pages(array(
'title_li' => NULL,
'child_of' => $findChildrenOf
)) ?>
</ul>
and save. and check.
Now make this menu title dynamic ->
<h2 class="page-links__title"><a
href="<?php echo get_the_permalink($theParent) ?>"><?php echo get_the_title($theParent) ?></a>
</h2>
and save.
now i want display this page menu only have child page and not display which have no children pages so do like below ->
<?php
// Check if there are child pages
$testArray = get_pages(array(
'child_of' => get_the_ID(),
));
if($theParent || $testArray) { ?>
<div class="page-links">
<h2 class="page-links__title"><a
href="<?php echo get_the_permalink($theParent) ?>"><?php echo get_the_title($theParent) ?></a>
</h2>
<ul class="min-list">
<?php
if($theParent) {
$findChildrenOf = $theParent;
}else {
$findChildrenOf = get_the_ID();
}
wp_list_pages(array(
'title_li' => NULL,
'child_of' => $findChildrenOf
)) ?>
</ul>
</div>
<?php } ?>
and save
Also if you want set order of page in menu from dashboard do this first add below line in
'sort_column' => 'menu_order'
wp_list_pages(array(
'title_li' => NULL,
'child_of' => $findChildrenOf
))
and save now go to that page and change order from right side page option.
Comments
Post a Comment