В ваши функции. php файл поместите эту функцию:
function display_related_menu_pages() {
// Get all menu items
// replace "Menu Name" by your menu name or id
$menu_items = wp_get_nav_menu_items('Menu Name');
// Get the current item (current page)
$current_item = false;
foreach($menu_items as $item) {
if((int)$item->object_id === get_the_id()) {
$current_item = $item;
}
}
// Get the parent ID
$parent_id = false;
if($current_item->menu_item_parent != 0) {
$parent_id = $current_item->menu_item_parent;
}else {
$parent_id = $current_item->ID;
}
// Prepare items to display
$items_to_display = [];
// Get only items that have the parent_id or the item that is the parent_id
foreach($menu_items as $item) {
if(($parent_id == $item->ID || $parent_id == $item->menu_item_parent) && $item->ID !== $current_item->ID ) {
$items_to_display[] = $item;
}
}
// Display items
foreach($items_to_display as $item) :
?>
<div class="p-5 bg-gray-300">
<h3 class="pt-5 pb-0 mb-0 capitalize"><a><?php echo $item->title; ?></h3></a>
<button class="btn btn-blue mt-5 mb-5"><a href="<?php echo $item->url; ?>"><?php _e('read more', 'your-theme-text-domain'); ?></a></button>
</div>
<?php
endforeach;
}
Затем в вашем шаблоне juste вызовите функцию, где вы хотите ее отобразить
display_related_menu_pages();
Вы также можете имейте функцию, чтобы только возвратить пункты меню как это:
function get_related_menu_pages() {
// Get all menu items
// replace "Menu Name" by your menu name or id
$menu_items = wp_get_nav_menu_items('Menu Name');
// Get the current item (current page)
$current_item = false;
foreach($menu_items as $item) {
if((int)$item->object_id === get_the_id()) {
$current_item = $item;
}
}
// Get the parent ID
$parent_id = false;
if($current_item->menu_item_parent != 0) {
$parent_id = $current_item->menu_item_parent;
}else {
$parent_id = $current_item->ID;
}
// Prepare items to display
$items_to_display = [];
// Get only items that have the parent_id or the item that is the parent_id
foreach($menu_items as $item) {
if(($parent_id == $item->ID || $parent_id == $item->menu_item_parent) && $item->ID !== $current_item->ID ) {
$items_to_display[] = $item;
}
}
return $items_to_display;
}
и обработать отображение в вашем шаблоне:
$related_pages = get_related_menu_pages();
// Display items
foreach($related_pages as $item) :
?>
<div class="p-5 bg-gray-300">
<h3 class="pt-5 pb-0 mb-0 capitalize"><a><?php echo $item->title; ?></h3></a>
<button class="btn btn-blue mt-5 mb-5"><a href="<?php echo $item->url; ?>"><?php _e('read more', 'your-theme-text-domain'); ?></a></button>
</div>
<?php
endforeach;