@mercmarcus

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

У меня есть малюсенький плагин-транслитератор. По умолчанию оно плавающая кнопка. Могу ли я фиксировать его в меню? Хотел сделать с шорткодом, правда в хедер. Но кнопка отображается в десктопе и в мобильном по разному. У меня меню стик, то есть фиксированный. Хочу добавить туда кнопку. Вот мой шорткод:

add_shortcode( 'head_bar' , 'мой скрипт' );
add_action('wp_enqueue_scripts', 'мой скрипт style');


<?php echo do_shortcode('[head_bar]'); ?>


Есть ли другие способы?
  • Вопрос задан
  • 42 просмотра
Пригласить эксперта
Ответы на вопрос 1
@Aricus
Проще всего вынести кнопку за пределы меню: прямо перед или после самого меню. Если же нужно, можно заморочиться с кастомным меню. WP позволяет сделать меню любого вида, но в нём непросто разобраться. Приведу пример из моего проекта.
Это - в вёрстке:
<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 );
	}
	
}
Ответ написан
Комментировать
Ваш ответ на вопрос

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

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