如何访问菜单项的“描述”?
-
-
您为什么不使用页面摘录?Why don’t you use the page excerpt?
- 0
- 2013-01-06
- fuxia
-
因为在调用wp_nav_menu时,我也需要在菜单中使用它.我有一个定制的助行器.because I need to use it in the menu as well, when wp_nav_menu is called. I have a custom walker.
- 0
- 2013-01-06
- Claire
-
@toscho事实证明,我现在需要使用摘录.您能看看[这个问题](http://wordpress.stackexchange.com/questions/78723/how-to-get-the-excerpt-in-nav-menu-walker)吗?@toscho As it turns out I need to use the excerpt this now. Could you take a look at [this question](http://wordpress.stackexchange.com/questions/78723/how-to-get-the-excerpt-in-nav-menu-walker) please?
- 0
- 2013-01-08
- Claire
-
2 个回答
- 投票数
-
- 2013-01-06
我不喜欢再次解析菜单项的想法.作为一种替代解决方案,我建议在第一次运行时存储描述:
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
中. - 它充当描述的获取者:如果在调用之后时不带参数调用此函数,则导航描述已呈现,如果有的话,您将获得描述的值.
缺点是:对于菜单调用来不及,例如在页脚中,它将不起作用.
优点:可以节省时间.您以后可以随时通过不带参数的函数来获取描述:
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' ); }
I don’t like the idea to parse the menu items again. As an alternative solution I suggest to store the description during the first run:
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; }
Explanation
The function does two things:
- It acts as a filter called inside of
wp_nav_menu()
. Here, it is called until it hits the current page. Then the description is stored internally in$desc
. - It acts as a getter for the description: If you call this function without parameter after the navigation menu has been rendered you get the value of the description, if there is one.
The downside is: it would not work for a menu call too late, in a footer for example.
The advantage: you save time.You can get the description later any time by calling the function without a parameter:
print wpse_78483_get_current_items_description();
As a follow-up, here is a second way to use it:
$desc = wpse_78483_get_current_items_description(); if ( empty ( $desc ) ) { the_excerpt(); } else { print wpautop( $desc ); }
Extra tip: You can enable the excerpt editor box for pages:
add_action( 'wp_loaded', 'wpse_78483_page_excerpt' ); function wpse_78483_page_excerpt() { add_post_type_support( 'page', 'excerpt' ); }
-
谢谢.我不太了解它是如何工作的,但是谢谢!thank you. I don't really understand how it works but it does so thanks!
- 0
- 2013-01-06
- Claire
-
@Nicola抱歉,您是对的.我进行了更新,提供了更好的解释和内联文档.@Nicola Sorry, you are right. I have made an update with a better explanation and inline docs.
- 1
- 2013-01-06
- fuxia
-
谢谢,我更新了问题以表明我希望所有页面都将alt文本存储在摘录中thanks, I updated my question to show that I want all pages to store the alt text in excerpt
- 0
- 2013-01-08
- Claire
-
- 2013-01-06
在Google缓存的网站上找到了答案.
因此要访问当前页面的导航项描述-只需调用
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; }
Found the answer on a google cached website.
So to access the current page's navigation item description - just call the function
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; }
您可以在外观>菜单下添加菜单的位置,这里有描述.在我的页面中,我希望能够回显该描述.不是在菜单中,而是在我的页面中.如何获取此信息?
编辑:
@toscho
如何编辑自定义步行者?对象$item是否可以访问页面摘录?