首页 > 解决方案 > 获取子菜单项和回显

问题描述

我正在尝试打开活动页面的子菜单项,并使用 wordpress 查询将它们回显。我已经想出为菜单的帖子类型执行此操作,我有点挣扎。这是我正在使用的帖子类型版本。

  <div class="container">
<div class="grid mb-10 mt-10 w-full text-white sm:grid-cols-3 gap-4">

<?php
        // WP_Query arguments
        wp_nav_menu( array(
          'menu'        => 'Menu Name',
          'sub_menu'    => true,
          'show_parent' => true
        ) );
        // The Query
        $services = new WP_Query( $args );
        // The Loop
        if ( $services->have_posts() ) {
            while ( $services->have_posts() ) {
        $services->the_post();
    ?>

  <div class="p-5 bg-gray-300">

    <h3 class="pt-5 pb-0 mb-0 capitalize"><a>THIS IS WHERE I NEED TO ECHO THE TITLE</h3></a>

    <button class="btn btn-blue mt-5 mb-5"><a href="<?php the_permalink();?> ">read more</a></button>

  </div>
  <?php wp_reset_postdata(); ?>
            <?php }}?>

</div>

标签: phpwordpress

解决方案


在你的functions.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;
}

然后在你的模板中调用你希望它显示的函数

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;

推荐阅读