@viktorleg

Как добавить атрибут в пункт меню Wordpress?

Приветствую!
Подскажите темному, как добавить атрибут modal в пункт меню, чтобы получилось так:
<a href="link" modal>Ссылка</a>

Я добавляю с помощью этой функции:
add_filter( 'nav_menu_link_attributes', 'contact_menu_atts', 10, 3 );
function contact_menu_atts( $atts, $item, $args )
{
  // The ID of the target menu item
  $menu_target = 1;

  // inspect $item
  if ($item->ID == $menu_target) {
    $atts['data-toggle'] = 'modal';
  }
  return $atts;
}


и в итоге получается так:
<a href="link" data-toggle="modal">Ссылка</a>
  • Вопрос задан
  • 479 просмотров
Пригласить эксперта
Ответы на вопрос 1
xakplant
@xakplant
Автор сайта xakplant.ru
add_filter( 'nav_menu_link_attributes', 'contact_menu_atts', 10, 3 );
function contact_menu_atts( $atts, $item, $args )
{
  // The ID of the target menu item
  $menu_target = 1;

  // inspect $item
  if ($item->ID == $menu_target) {
    array_push($atts, "modal");
  }
  return $atts;
}


Но не факт, что это работает. Возможно лучше всего будет "пересобрать" строку с html-ем ссылки с помощью https://wp-kama.ru/hook/walker_nav_menu_start_el
Ответ написан
Ваш ответ на вопрос

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

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