Answer the question
In order to leave comments, you need to log in
How to add conditional logic to menu items using ACF?
Hello!
There was a need to add conditional logic to menu items.
The If Menu plugin is deprecated because when it is used, it modifies the menu items and my edits with ACF to add icons stop working.
Exit - add extra. field using ACF to menu items. To do this, I use the True / False field.
I want to display some menu items only to authorized users ( is_user_logged_in() )
On the site, I use a custom walker
<?php # -*- coding: utf-8 -*-
/**
* Create a nav menu with very basic markup.
*
* @author Thomas Scholz http://toscho.de
* @version 1.0
*/
class menu_walker extends Walker_Nav_Menu
{
/**
* Start the element output.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. May be used for padding.
* @param array $args Additional strings.
* @return void
*/
//public function start_el( &$output, $item, $depth, $args )
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
{
$output .= '<li>';
$attributes = 'class="app-menu__item"';
// if ( $args->has_children ) {
// $attributes = 'class="app-menu__item" data-toggle="treeview"';
// } else {
// $attributes = 'class="app-menu__item"';
// }
! empty ( $item->attr_title )
// Avoid redundant titles
and $item->attr_title !== $item->title
and $attributes .= ' title="' . esc_attr( $item->attr_title ) .'"';
! empty ( $item->url )
and $attributes .= ' href="' . esc_attr( $item->url ) .'"';
$attributes = trim( $attributes );
$title = apply_filters( 'the_title', $item->title, $item->ID );
$font_awesome = get_field('font_awesome', $item);
// $item_output = "$args->before<a $attributes>$args->link_before$title</a>"
// . "$args->link_after$args->after";
$item_output = "$args->before"
. "<a $attributes>"
. "<i class='app-menu__icon " . $font_awesome ."'></i>"
. "$args->link_before"
. "$title"
. "$args->link_after"
. "</a>"
. "$args->after";
// Since $output is called by reference we don't need to return anything.
$output .= apply_filters(
'walker_nav_menu_start_el'
, $item_output
, $item
, $depth
, $args
);
}
/**
* @see Walker::start_lvl()
*
* @param string $output Passed by reference. Used to append additional content.
* @return void
*/
// public function start_lvl( &$output )
public function start_lvl( &$output, $depth = 0, $args = array() )
{
$output .= '<ul class="treeview-menu">';
}
/**
* @see Walker::end_lvl()
*
* @param string $output Passed by reference. Used to append additional content.
* @return void
*/
// public function end_lvl( &$output )
public function end_lvl( &$output, $depth = 0, $args = array() )
{
$output .= '</ul>';
}
/**
* @see Walker::end_el()
*
* @param string $output Passed by reference. Used to append additional content.
* @return void
*/
//function end_el( &$output )
function end_el( &$output, $item, $depth = 0, $args = array() )
{
$output .= '</li>';
}
}
if (is_user_logged_in() && get_field('display_for_logged_in_users', $item)) {
......
} else {
......
}
if (is_user_logged_in() && get_field('display_for_logged_in_users', $item)) {
......
} else if (!is_user_logged_in() && get_field('display_for_logged_in_users', $item)) {
......
}
Answer the question
In order to leave comments, you need to log in
Your walker works with each element in turn, and the element itself is available in $item
. Also check his name / slug / ID. It will turn out 3 checks:
- the required $item
- the user is authorized
- and only then we look at the value of the field
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question