Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WP Nav菜单-自定义wordpress菜单HTML结构_Php_Html_Wordpress - Fatal编程技术网

Php WP Nav菜单-自定义wordpress菜单HTML结构

Php WP Nav菜单-自定义wordpress菜单HTML结构,php,html,wordpress,Php,Html,Wordpress,如何使用自定义HTML结构创建Wordpress菜单 <?php wp_nav_menu( array( 'theme_location' => 'global-menu' ) ); ?> 输出HTML应如下所示: <?php wp_nav_menu( array( 'theme_location' => 'global-menu' ) ); ?> <nav> <a href="" class="item-link item-d

如何使用自定义HTML结构创建Wordpress菜单

<?php wp_nav_menu( array( 'theme_location' => 'global-menu' ) ); ?>

输出HTML应如下所示:

<?php wp_nav_menu( array( 'theme_location' => 'global-menu' ) ); ?>
<nav>
   <a href="" class="item-link item-dynamic-class-1">Item 1</a>
   <a href="" class="item-link item-dynamic-class-2">Item 2</a>
   <a href="" class="item-link item-dynamic-class-3">Item 3</a>
   <a href="" class="item-link item-dynamic-class-4">Item 4</a>
</nav>

我想从Wordpress菜单编辑器(CSS类输入字段)动态设置item-dynamic-class-x。如果已填充,则将类添加到锚点。如果没有,则只显示静态项目链接类。

尝试以下代码

<?php wp_nav_menu( array( 'theme_location' => 'global-menu' ) ); ?>
wp_nav_menu(
 array (
    'menu'            => 'main-menu',
    'container'       => '',// or false
    'container_id'    => FALSE,
    'menu_class'      => '',
    'menu_id'         => FALSE,
    'items_wrap'    => '%3$s',
    'depth'           => 1,
    'walker'          => new Description_Walker
 )
);
将下面的代码放入活动functions.php中

<?php wp_nav_menu( array( 'theme_location' => 'global-menu' ) ); ?>
class Description_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|object $args    Additional strings. Actually always an 
     * @return void
     */
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
    {
        $classes     = empty ( $item->classes ) ? array () : (array) $item->classes;

        $class_names = join(
            ' '
        ,   apply_filters(
                'nav_menu_css_class'
            ,   array_filter( $classes ), $item
            )
        );

        ! empty ( $class_names )
            and $class_names = ' class="'. esc_attr( $class_names ) . '"';



        $attributes  = '';

        ! empty( $item->attr_title )
            and $attributes .= ' title="'  . esc_attr( $item->attr_title ) .'"';
        ! empty( $item->target )
            and $attributes .= ' target="' . esc_attr( $item->target     ) .'"';
        ! empty( $item->xfn )
            and $attributes .= ' rel="'    . esc_attr( $item->xfn        ) .'"';
        ! empty( $item->url )
            and $attributes .= ' href="'   . esc_attr( $item->url        ) .'"';

        // insert description for top level elements only
        // you may change this
        $description = ( ! empty ( $item->description ) and 0 == $depth )
            ? '<small class="nav_desc">' . esc_attr( $item->description ) . '</small>' : '';

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

        $item_output = $args->before
            . "<a $attributes>"
            . $args->link_before
            . $title
            . '</a> '
            . $args->link_after
            . $description
            . $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
        );
    }
}
class Description\u Walker扩展了Walker\u导航菜单
{
/**
*启动元素输出。
*
*@param string$通过引用传递的输出。用于附加其他内容。
*@param object$item菜单项数据对象。
*@param int$菜单项的深度。可用于填充。
*@param array | object$args附加字符串。实际上总是一个
*@返回无效
*/
函数start_el(&$output,$item,$depth=0,$args=array(),$id=0)
{
$classes=空($item->classes)?数组():(数组)$item->classes;
$class\u name=加入(
' '
,应用过滤器(
“导航菜单css类”
,数组过滤器($classes),$item
)
);
!空($class\u名称)
和$class_names='class=“”.esc_attr($class_names)。”;
$attributes='';
!空($item->attr\u title)
和$attributes.='title=“”.esc_attr($item->attr_title)。”;
!空($item->target)
和$attributes.='target=“”.esc_attr($item->target)。”;
!空($item->xfn)
和$attributes.='rel=“”.esc_attr($item->xfn)。”;
!空($item->url)
和$attributes.='href=“”.esc_attr($item->url)。”;
//仅插入顶层图元的说明
//你可以改变这个
$description=(!empty($item->description)和0=$depth)
?“”.esc_attr($item->description)。“”;
$title=apply_过滤器('the_title',$item->title,$item->ID);
$item_output=$args->before
. ""
.$args->link_之前
$title
. ' '
.$args->link\u之后
.$description
.$args->after;
//由于$output是通过引用调用的,所以我们不需要返回任何内容。
$output.=应用过滤器(
“步行者导航菜单开始”
,$item_输出
,$item
,$depth
,$args
);
}
}