@Arh1diablo
Web мастер

Как вывести произвольные поля в wp_nav_menu?

Подскажите, добавил произвольное поле для пунктов меню img_menu ( изображения для пунктов меню ) подскажите как вывести его в функции wp_nav_menu, в цикле записей знаю, что через :
echo get_field('img_menu');

Сама функция :
$args = array( // Выводим верхнее меню
            'theme_location'=>'top',
            'container'=>'',
            'depth'=> 0);
        wp_nav_menu($args);
  • Вопрос задан
  • 215 просмотров
Решения вопроса 1
@Aricus
Вам следует прописать сделать своё отображение меню (walker). Приведу пример из своего проекта, может, поможет разобраться. Мне в своё время было непросто.
В header.php:
<div class="wrapper">
	<?php wp_nav_menu( array(
		'sort_column' => 'menu_order',
		'theme_location' => 'primary-menu',
		'container' => false,
		'menu_class' => 'navigation_menu',
		'walker' => new My_Walker_Nav_Menu()
	) ); ?>
</div>

В functions.php:
// Главное меню
class My_Walker_Nav_Menu extends Walker_Nav_Menu {
  function start_lvl(&$output, $depth = 1, $args = Array()) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul class=\"navigation_submenu-".$depth."\">\n";
  }
	public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
			$t = '';
			$n = '';
		} else {
			$t = "\t";
			$n = "\n";
		}
		$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';

		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
		$classes[] = 'menu-item-' . $item->ID;

		$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );

		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

		$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

		$output .= $indent . '<li' . $id . $class_names .'>';

		$atts = array();
		$atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
		$atts['target'] = ! empty( $item->target )     ? $item->target     : '';
		$atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
		$atts['href']   = ! empty( $item->url )        ? $item->url        : '';

		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

		$attributes = '';
		foreach ( $atts as $attr => $value ) {
			if ( ! empty( $value ) ) {
				$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
				$attributes .= ' ' . $attr . '="' . $value . '"';
			}
		}

		$title = apply_filters( 'the_title', writeTitle($item->title), $item->ID );

		$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
		
		
		if ($depth == 1) {
			$writeIcon = '<div class="submenu__icon">
				<div class="submenu__icon__img" style="background-image: url('.get_illImg($item->object_id)['url'].');"></div>
			</div>&nbsp;';
		} else {
			$writeIcon = '';
		}
		$item_output = $args->before;
		if (($depth == 1) && (get_field('activity', 'category_' .$item->object_id) == 'inactive')) {
			$item_output .= '<div' .addTooltip($item->object_id). '>';
			$item_output .= $writeIcon;
			$item_output .= $title;
			$item_output .= '</div>';
		} else {
			$item_output .= '<a'. $attributes .'' .addTooltip($item->object_id). '>';
			$item_output .= $writeIcon;
			$item_output .= $args->link_before . $title . $args->link_after;
			$item_output .= '</a>';
		}
		$item_output .= $args->after;

		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}
	
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

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

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