我怎样才能访问菜单项的“描述”?

时间:2013-01-06 作者:Claire

您可以在“外观”>“菜单”下添加菜单,我在中有描述。在我的页面中,我希望能够呼应出该描述。不是在菜单中,而是在我的页面中。如何访问此信息?

编辑:

@托肖

如何编辑自定义walker?对象$项是否有权访问页面摘录?

class description_walker extends Walker_Nav_Menu
{
        function start_el(&$output, $item, $depth, $args)
  {
    global $language;
       global $wp_query;
     global $post;
     global $polish_name;
       $indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
       $class_names = $value = \'\';

       $classes = empty( $item->classes ) ? array() : (array) $item->classes;

       $class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) );
       $class_names = \' class="\'. esc_attr( $class_names ) . \'"\';

       $output .= $indent . \'<li id="menu-item-\'. $item->ID . \'"\' . $value . $class_names .\'>\';

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

       $prepend = \'<strong>\';
       $append = \'</strong>\';


        $item_output = $args->before;
        $item_output .= \'<a\'. $attributes .\'>\';
      if($language=="polish"){
           $item_output .= $args->link_before .$prepend.apply_filters( \'the_title\', $item->description, $item->ID ).$append;

       }else{
        $item_output .= $args->link_before .$prepend.apply_filters( \'the_title\', $item->title, $item->ID ).$append;
       }


        $item_output .= \'</a>\';
        $item_output .= $args->after;


        $output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
        }
}

2 个回复
最合适的回答,由SO网友:fuxia 整理而成

我不喜欢再次解析菜单项的想法。作为替代解决方案,我建议在第一次运行时存储描述:

add_filter( \'walker_nav_menu_start_el\', \'wpse_78483_get_current_items_description\', 10, 2 );

/**
 * Get nav items description.
 *
 * @wp-hook walker_nav_menu_start_el
 * @param   string $item_output
 * @param   object $item
 * @return  string
 */
function wpse_78483_get_current_items_description( $item_output = NULL, $item = NULL )
{
    static $desc = \'\';

    // The function is NOT called during nav menu rendering, but later.
    if ( \'walker_nav_menu_start_el\' !== current_filter() )
        return $desc;

    // The function is called during wp_nav_menu().

        // description is set
    if ( ! empty ( $item->description )
        // and an URL is available
        and ! empty ( $item->url )
        // and it is the current page
        and parse_url( $item->url, PHP_URL_PATH ) === $_SERVER[\'REQUEST_URI\']
        )
    {
        // copy the description into our static internal variable
        $desc = $item->description;
        // remove this filter, it is not needed anymore
        remove_filter( \'walker_nav_menu_start_el\', __FUNCTION__ );
    }

    // return unchanged item markup
    return $item_output;
}
解释该函数有两个功能:

它作为一个过滤器,在wp_nav_menu(). 在这里,它被调用,直到它到达当前页面。然后将描述存储在$desc.after 导航菜单已呈现,如果有描述,您将获得描述的值

您可以在以后的任何时候通过调用不带参数的函数来获取描述:

print wpse_78483_get_current_items_description();
下面是第二种使用方法:

$desc = wpse_78483_get_current_items_description();

if ( empty ( $desc ) )
{
    the_excerpt();
}
else
{
    print wpautop( $desc );
}
额外提示:您可以为以下页面启用摘录编辑器框:

add_action( \'wp_loaded\', \'wpse_78483_page_excerpt\' );

function wpse_78483_page_excerpt()
{
    add_post_type_support( \'page\', \'excerpt\' );
}

SO网友:Claire

在谷歌缓存的网站上找到了答案。

因此,要访问当前页面的导航项描述,只需调用函数echo wps_get_menu_description()

function wps_get_menu_description( ) {
global $post;

// Default
$defaults = array(
    \'echo\' => false,
    \'format\' => \'\',
    \'description\' => \'\',
    \'location\' => \'primary\',
    \'classes\' => \'post-description\'
);

$args = wp_parse_args( $args, $defaults );
extract( $args , EXTR_SKIP );

// Get menu
$menu_locations = get_nav_menu_locations();
$nav_items = wp_get_nav_menu_items( $menu_locations[ $location ] );

// Cycle through nav items
foreach ( $nav_items as $nav_item ) {

    if ( ( is_page() || is_single() || is_archive() ) && ( $nav_item->object_id == $post->ID ) ) {
        $description = $nav_item->description;
    }

}


    $output = $description;

return $output;
}

结束

相关推荐

Wp_admin edit.php速度慢,有很多查询

我有大约4000个帖子。目前,当我点击所有帖子(edit.php)时,加载大约需要10秒,我显示了大约1000个查询!大多数看起来像。。。SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy