Получение пунктов подменю и эхо - PullRequest
0 голосов
/ 30 марта 2020

Я пытаюсь получить включенные пункты подменю активной страницы и вывести их с помощью запроса 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>

1 Ответ

0 голосов
/ 30 марта 2020

В ваши функции. 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;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...