Php 从其ID获取菜单项标题
我需要得到我的顶级导航的标题。我已经知道顶级菜单项的ID,但我需要得到它的标题。怎么能这样呢 我现在有这样的菜单:Php 从其ID获取菜单项标题,php,wordpress,Php,Wordpress,我需要得到我的顶级导航的标题。我已经知道顶级菜单项的ID,但我需要得到它的标题。怎么能这样呢 我现在有这样的菜单: 一级 二级#一级 二级#二级 二级#三级 如果我在任何级别2页面上,我有级别1菜单项ID。我需要在当前主题位置实例中获取标题 <?php if(getTopSelectedMenu('primary')): $topParent = intval(getTopSelectedMenu('primary')); $t
- 一级
- 二级#一级
- 二级#二级
- 二级#三级
<?php if(getTopSelectedMenu('primary')):
$topParent = intval(getTopSelectedMenu('primary'));
$topParentTitle = "";
?>
<div class="sidebar-menu-headline"><?=$topParentTitle?><i class="fa fa-arrow-down" aria-hidden="true"></i></div>
<?php
$args = array(
'menu' => '',
'menu_class' => 'sidebar-menu',
'container' => 'ul',
'theme_location' => 'primary',
'level' => 2,
'child_of' => $topParent,
'echo' => FALSE,
'fallback_cb' => '__return_false'
);
$submenu = wp_nav_menu( $args );
if ( ! empty ($submenu) )echo $submenu;
?>
通过传递菜单id,使用以下功能获取菜单标题
Function: wp_get_nav_menu_object($menu) // $menu can be 'id','name' or 'slug'
Returns : Object (
term_id => 4
name => My Menu Name
slug => my-menu-name
term_group => 0
term_taxonomy_id => 4
taxonomy => nav_menu
description =>
parent => 0
count => 6
)
// in your case.
$menu = wp_get_nav_menu_object($topParent );
$menu_title = $menu->name;
你可以在这里找到更多信息
要获取菜单标题,请使用以下代码
if(getTopSelectedMenu('primary')){
$topParentId = intval(getTopSelectedMenu('primary'));
$menuLocations = get_nav_menu_locations(); // Get our nav locations (set in our theme, usually functions.php)
// This returns an array of menu locations ([LOCATION_NAME] = MENU_ID);
$menu = wp_get_nav_menu_object($menuLocations['primary']) ; // Get the *primary* menu object
$primaryNav = wp_get_nav_menu_items($menu->term_id); // Get the array of wp objects, the nav items for our queried location.
foreach ( $primaryNav as $navItem ) {
if ($navItem->ID == $topParentId ) {
$topParentTitle = $navItem->title;
}
}
}
<div class="sidebar-menu-headline"><?=$topParentTitle?><i class="fa fa-arrow-down" aria-hidden="true"></i></div>
if(getTopSelectedMenu('primary')){
$topParentId=intval(getTopSelectedMenu('primary');
$menuLocations=get_nav_menu_locations();//获取我们的导航位置(在主题中设置,通常是functions.php)
//这将返回一个菜单位置数组([LOCATION\u NAME]=菜单ID);
$menu=wp\u get\u nav\u menu\u对象($menuLocations['primary']);//获取*primary*菜单对象
$primaryNav=wp_get_nav_menu_items($menu->term_id);//获取wp对象数组,即查询位置的导航项。
foreach($primaryNav作为$navItem){
如果($navItem->ID==$topParentId){
$topparentitle=$navItem->title;
}
}
}
通过传递菜单id,使用以下功能获取菜单标题
Function: wp_get_nav_menu_object($menu) // $menu can be 'id','name' or 'slug'
Returns : Object (
term_id => 4
name => My Menu Name
slug => my-menu-name
term_group => 0
term_taxonomy_id => 4
taxonomy => nav_menu
description =>
parent => 0
count => 6
)
// in your case.
$menu = wp_get_nav_menu_object($topParent );
$menu_title = $menu->name;
你可以在这里找到更多信息
要获取菜单标题,请使用以下代码
if(getTopSelectedMenu('primary')){
$topParentId = intval(getTopSelectedMenu('primary'));
$menuLocations = get_nav_menu_locations(); // Get our nav locations (set in our theme, usually functions.php)
// This returns an array of menu locations ([LOCATION_NAME] = MENU_ID);
$menu = wp_get_nav_menu_object($menuLocations['primary']) ; // Get the *primary* menu object
$primaryNav = wp_get_nav_menu_items($menu->term_id); // Get the array of wp objects, the nav items for our queried location.
foreach ( $primaryNav as $navItem ) {
if ($navItem->ID == $topParentId ) {
$topParentTitle = $navItem->title;
}
}
}
<div class="sidebar-menu-headline"><?=$topParentTitle?><i class="fa fa-arrow-down" aria-hidden="true"></i></div>
if(getTopSelectedMenu('primary')){
$topParentId=intval(getTopSelectedMenu('primary');
$menuLocations=get_nav_menu_locations();//获取我们的导航位置(在主题中设置,通常是functions.php)
//这将返回一个菜单位置数组([LOCATION\u NAME]=菜单ID);
$menu=wp\u get\u nav\u menu\u对象($menuLocations['primary']);//获取*primary*菜单对象
$primaryNav=wp_get_nav_menu_items($menu->term_id);//获取wp对象数组,即查询位置的导航项。
foreach($primaryNav作为$navItem){
如果($navItem->ID==$topParentId){
$topparentitle=$navItem->title;
}
}
}
对,你想要简单!我抓到你了
如果您知道URL,请使用URL\u to\u postid()
:
$actual\u id=url\u to\u posted($url);
$the_title=get_the_title($actual_id);
或者简单地说:
$the_title=获取_title(url到_posted($url));
还没有真正遇到过这样不准确的时刻 对,你想要简单!我抓到你了 如果您知道URL,请使用
URL\u to\u postid()
:
$actual\u id=url\u to\u posted($url);
$the_title=get_the_title($actual_id);
或者简单地说:
$the_title=获取_title(url到_posted($url));
还没有真正遇到过这样不准确的时刻 @AlivetoDie from id?@AlivetoDie from id?好的,让我澄清一下。我需要菜单项标题,而不是菜单名。例如,如果我有>级别1菜单(id:1),>>级别2菜单(id:2),>>级别2菜单2(id:3),我当前有顶级菜单id(本例中为1)。我需要从这个导航实例中获取标题(1级菜单)编辑我的答案。谢谢。工作很有魅力,只要批准我的小修补程序。还有,我喜欢你解释什么是什么:)好的,让我澄清一下。我需要菜单项标题,而不是菜单名。例如,如果我有>级别1菜单(id:1),>>级别2菜单(id:2),>>级别2菜单2(id:3),我当前有顶级菜单id(本例中为1)。我需要从这个导航实例中获取标题(1级菜单)编辑我的答案。谢谢。工作很有魅力,只要批准我的小修补程序。还有,我喜欢你解释什么做什么:)