heretic_man
@heretic_man

Как вывести все элементы определенного раздела в меню WP?

<?php
	$menu_name = 'menu-1';
	if ( ( $locations = get_nav_menu_locations() ) ) {

		$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
		$menu_items = wp_get_nav_menu_items($menu->term_id);
		$menu_list = '<ul id="menu-' . $menu_name . '">';
		$object_id = '';
		
		foreach ( (array) $menu_items as $key => $menu_item ) {
			$title = $menu_item->title;
			$url = $menu_item->url;
			if( $menu_item->title === 'Услуги'){  // Тот раздел меню где хочу найти все  подменю
				$object_id = $menu_item->object_id; // Копирую его id
			} elseif($object_id != '') { // Сравниваю id родителя всеx итемов на соответствие с значением в переменой 
				echo $title.'  ';
			}
		} // END foreach
		$menu_list .= '</ul>';
		} // END if isset 
?>


Минус тут в том что глубина погружения = 0;

<ul>
.....
	<li>Услуги</li>
	<ul>
		<li>lorem</li>
		<li>lorem</li>
		<li>lorem</li>
		<li>lorem
			<ul> <!-- Вот эта вложенность не выведется уже -->
				<li>lorem</li>
				<li>lorem</li>
			</ul>
		</li>
	</ul>
.....
</ul>


Array
(
    [1] => WP_Post Object // Обший родитель который нуджно получить
        (
            [ID] => 59
            [post_author] => 1
            [post_date] => 2019-01-05 13:22:40
            [post_date_gmt] => 2019-01-05 13:22:40
            [post_content] =>  
            [post_title] => 
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => 59
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2019-01-17 02:00:23
            [post_modified_gmt] => 2019-01-16 20:00:23
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => https://astana-gc.kz/?p=59
            [menu_order] => 2
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 59
            [menu_item_parent] => 0
            [object_id] => 21
            [object] => page
            [type] => post_type
            [type_label] => Страница
            [url] => https://astana-gc.kz/uslugi/
            [title] => Услуги
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

    [5] => WP_Post Object
        (
            [ID] => 46
            [post_author] => 1
            [post_date] => 2019-01-04 21:05:04
            [post_date_gmt] => 2019-01-04 21:05:04
            [post_content] =>  
            [post_title] => 
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => 46
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2019-01-17 02:00:23
            [post_modified_gmt] => 2019-01-16 20:00:23
            [post_content_filtered] => 
            [post_parent] => 21
            [guid] => https://astana-gc.kz/46/
            [menu_order] => 6
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 46
            [menu_item_parent] => 59
            [object_id] => 45
            [object] => page
            [type] => post_type
            [type_label] => Страница
            [url] => https://astana-gc.kz/uslugi/stroitelnoe-proektirovanie/
            [title] => Строительное проектирование
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

    [6] => WP_Post Object
        (
            [ID] => 700
            [post_author] => 1
            [post_date] => 2019-01-14 22:17:58
            [post_date_gmt] => 2019-01-14 16:17:58
            [post_content] =>  
            [post_title] => 
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => 700
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2019-01-17 02:00:23
            [post_modified_gmt] => 2019-01-16 20:00:23
            [post_content_filtered] => 
            [post_parent] => 21
            [guid] => https://astana-gc.kz/?p=700
            [menu_order] => 7
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 700
            [menu_item_parent] => 59
            [object_id] => 94
            [object] => page
            [type] => post_type
            [type_label] => Страница
            [url] => https://astana-gc.kz/uslugi/razrabotka-pos-i-ppr/
            [title] => Разработка ПОС и ППР
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

    [7] => WP_Post Object // вложен в предыдущий элемент. вот этот не выведится 
        (
            [ID] => 845
            [post_author] => 1
            [post_date] => 2019-01-17 02:00:23
            [post_date_gmt] => 2019-01-16 20:00:23
            [post_content] =>  
            [post_title] => 
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => 845
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2019-01-17 02:00:23
            [post_modified_gmt] => 2019-01-16 20:00:23
            [post_content_filtered] => 
            [post_parent] => 94
            [guid] => https://astana-gc.kz/?p=845
            [menu_order] => 8
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 845
            [menu_item_parent] => 700
            [object_id] => 840
            [object] => page
            [type] => post_type
            [type_label] => Страница
            [url] => https://astana-gc.kz/uslugi/razrabotka-pos-i-ppr/32433/
            [title] => Проект организации строительства (ПОС)
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )


)
  • Вопрос задан
  • 198 просмотров
Решения вопроса 1
wppanda5
@wppanda5 Куратор тега WordPress
WordPress Mедведь
Про погружение есть такая штука называется рекурсия

Вот пример реализации такого как вам надо, под себя подпилите
function wpp_bt_shop_by_menu_items() {
	if ( false === ( $html = get_transient( 'wpp_bt_shop_by_nav' ) ) ) {

		$html      = '';
		$locations = get_nav_menu_locations();
		if ( $locations && isset( $locations['shop_by'] ) ) :
			$menu       = wp_get_nav_menu_object( $locations['shop_by'] );
			$menu_items = wp_get_nav_menu_items( $menu );
			foreach ( $menu_items as $menu_item ):
				$term = $menu_item->type === 'taxonomy' ? get_term( $menu_item->object_id ) : null;
				$html .= wpp_bt_crate_items_list( $term, $menu_item );
			endforeach;
		endif;

		set_transient( 'wpp_bt_shop_by_nav', $html, 24 * HOUR_IN_SECONDS );

	}

	return apply_filters( 'wpp_bt_shop_by_nav_items', $html );
}

/**
 * Product cat naw menu item
 *
 * @param $term
 * @param $menu_item
 *
 * @return string
 */
function wpp_bt_crate_items_list( $term, $menu_item) {
	$html = '';

	if ( $menu_item === null || ( $menu_item !== null && $menu_item->menu_item_parent == 0 ) ) {

		if ( $menu_item === null || ( $menu_item !== null && $menu_item->object === 'product_cat' ) ) {

			if ( wpp_instock_product_count_in_term( $term->term_id ) > 0 || ( $menu_item->object_id === BT_LATEST_PRODUCT_TERM ) ) {


					$children = get_categories(
						array(
							'hide_empty' => true,
							'taxonomy'   => 'product_cat',
							'parent'     => $term->term_id
						)
					);

					$class = count( $children ) > 0 ? 'has-child' : 'cat-no-child';
					$title = $menu_item ? $menu_item->title : $term->name;

					$html .= sprintf( '<li class="cat-item cat-item-%s %s">', $term->term_id, $class );
					$html .= sprintf( '<a href="%1$s" title="%2$s">%2$s</a>', get_term_link( $term->term_id ), $title );

					if ( count( $children ) > 0 ) {
						$html .= '<ul class="children">';

						foreach ( $children as $child ) {
							if ( $child->count > 0 ) {
								$html .= wpp_bt_crate_items_list( $child, null );
							}
						}

						$html .= '</ul>';
					}

					$html .= '</li>';

			}

		} else if ( $menu_item->object == 'custom' ) {

			$children = get_menu_item_children(
				array(
					'parent' => $menu_item->ID
				)
			);

			$class = count( $children ) > 0 ? 'has-child' : 'cat-no-child';

			$html .= sprintf( '<li class="cat-item cat-item-%s %s">', $menu_item->ID, $class );
			$html .= sprintf( '<a href="%1$s%2$s" title="%3$s">%3$s</a>', get_home_url(), $menu_item->url, $menu_item->title );

			if ( count( $children ) > 0 ) {
				$html .= '<ul class="children">';

				foreach ( $children as $child ) {

					$html .= sprintf( '<li class="cat-item cat-item-%s cat-no-child">', $child->ID );
					$html .= sprintf( '<a href="%1$s%2$s" title="%3$s">%3$s</a>', get_home_url(), $child->url, $child->title );
				}

				$html .= '</ul>';
			}

			$html .= '</li>';
		}
	}

	return $html;
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы