网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

如何获取当前页面菜单项祖先名称wordpress

GG网络技术分享 2025-03-18 16:12 2


问题描述:

I would like to know how to retrieve as variable the name of the current page.

For exemple, My menu is like :

Item 1

Item 2 => sub-item 1

Item 1 & 2 are custom links. sub-item1 is my page.

When I\'m on this page, I want to retrieve \"Item 2\" name (to build my custom breadcumbs)

Thanks

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我想知道如何检索当前页面的名称作为变量。

例如,我的菜单如下:</ p>

  Item 1 

Item 2 =&gt; 子项目1

</ code> </ pre>

项目1&amp; 2是自定义链接。 sub-item1是我的页面。</ p>

当我在这个页面上时,我想要检索“Item 2”名称(以构建我的自定义breadcumbs)</ p>

谢谢</ p>

</ div>

网友观点:

BREADCRUMBS:

Here is a breadcrumbs function that work with the hierarchy of your menu. Add this to your theme\'s \'functions.php\'.

function my_menu_breadcrumb($theme_location, $separator = \' &gt; \') {

$locations = get_nav_menu_locations();

if ( isset( $locations[ $theme_location ] ) ) {

$menu = wp_get_nav_menu_object( $locations[ $theme_location ] );

$menu_items = wp_get_nav_menu_items($menu->term_id);

_wp_menu_item_classes_by_context( $menu_items );

$breadcrumbs = array();

foreach ( $menu_items as $menu_item ) {

if ($menu_item->current) {

$breadcrumbs[] = \\\"<span title=\\\\\"{$menu_item->title}\\\\\">{$menu_item->title}</span>\\\";

}

else if ($menu_item->current_item_ancestor) {

$breadcrumbs[] = \\\"<a href=\\\\\"{$menu_item->url}\\\\\" title=\\\\\"{$menu_item->title}\\\\\">{$menu_item->title}</a>\\\";

}

}

echo implode($separator, $breadcrumbs);

}

}

You can then call as <?php my_menu_breadcrumb(\'header-menu\'); ?> where \'header-menu\' is the menu location name. In the loop, $menu_item->title will return page title and $menu_item->url it\'s URL.

PARENT MENU TITLE:

Here is the function to get parent menu item title(s) of current page - add this to your theme\'s \'functions.php\'.

function my_menu_parent($theme_location) {

$locations = get_nav_menu_locations();

if ( isset( $locations[ $theme_location ] ) ) {

$menu = wp_get_nav_menu_object( $locations[ $theme_location ] );

$menu_items = wp_get_nav_menu_items($menu->term_id);

_wp_menu_item_classes_by_context( $menu_items );

$breadcrumbs = array();

foreach ( $menu_items as $menu_item ) {

if ($menu_item->current_item_ancestor) {

$breadcrumbs[] = $menu_item->title;

}

}

return $breadcrumbs;

}

}

You can then call as below where \'header-menu\' is the menu location name.

$parentitems = my_menu_parent( \'header-menu\' );

foreach ( $parentitems as $parentitem ) {

echo $parentitem.\\\"<br>\\\";

}

###

You can user this

$pagename = get_query_var(\'pagename\');  

if ( !$pagename && $id > 0 ) {

// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object

$post = $wp_query->get_queried_object();

$pagename = $post->post_name;

}

###

There are plenty of tutorials and code snippets online describing how to show the active page’s children (or if on one of those children, the active page’s siblings) as a sub-menu in WordPress. Usually something like this:

<?php

$ref_post = empty($post->post_parent) ? $post->ID : $post->post_parent;

$children = wp_list_pages(\'title_li=&child_of=\'.$ref_post.\'&echo=0\');

if ($children) {

echo \\\"<ul>$children</ul>\\\";

}

?>

for ref:-https://www.minddevelopmentanddesign.com/blog/showing-current-pages-parents-sub-menu-items-custom-nav-menu-wordpress/

标签:

提交需求或反馈

Demand feedback