Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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 如何让wordpress博客页面显示与缅因州页面不同的导航栏_Php_Wordpress - Fatal编程技术网

Php 如何让wordpress博客页面显示与缅因州页面不同的导航栏

Php 如何让wordpress博客页面显示与缅因州页面不同的导航栏,php,wordpress,Php,Wordpress,嗨,我是一个网络开发人员,我正在尝试让word press为我的博客部分使用第二个导航。我已经设置好了主站点,但我想要的是,当我在顶部导航中点击博客时,显示博客部分的不同导航。我尝试在header.php中使用以下代码 <?php if (is_single('blog')){ wp_nav_menu(array('menu'=>'secondary' )); } ?> 但是,即使主题支持2个导航,该代码似乎也不起作用 <nav class="Seo-na

嗨,我是一个网络开发人员,我正在尝试让word press为我的博客部分使用第二个导航。我已经设置好了主站点,但我想要的是,当我在顶部导航中点击博客时,显示博客部分的不同导航。我尝试在header.php中使用以下代码

<?php
 if (is_single('blog')){
 wp_nav_menu(array('menu'=>'secondary' ));
 }
 ?>

但是,即使主题支持2个导航,该代码似乎也不起作用

 <nav class="Seo-nav">
 <div class="Seo-nav-inner">
 <?php
echo theme_get_menu(array(
        'source' => theme_get_option('theme_menu_source'),
        'depth' => theme_get_option('theme_menu_depth'),
        'menu' => 'primary-menu',
        'class' => 'Seo-hmenu'
    )
);

get_sidebar('nav');
?>


上面的代码是我用来调用导航的代码。有没有办法获得一个或多个特定页面来显示一个菜单?这方面的任何帮助都会很好。以前从来没有让某些页面有不同的导航,所以这对我来说是一个新的导航。

我想你可以用两种方式

一个是在第二个页面上只显示当前页面的子项

function show_different_nav(){
  global $post;
  $sec_nav=wp_list_pages("child_of=".$post->ID);
 return $sec_nav;
 }
现在在模板中调用show_different_nav()

另一种方法是编写自己的过滤器。在这种情况下,我修改了

 add_filter( 'wp_nav_menu_items', 'get_different_custom_menu_item_according_to_page_slug', 10, 2 );

function get_different_custom_menu_item_according_to_page_slug ( $items, $args ) {
$current_id=(int)$GLOBALS['post']->ID;
    $current_slug=$GLOBALS['post']->slug;


$the_nmo = wp_get_nav_menu_object($current_slug); 

if ($the_nmo==""){}else{

$menu_id=$the_nmo->term_id;
$items_2 = wp_get_nav_menu_items($menu_id);
$items="";


if ($args->theme_location == 'secondary') {

    foreach ($items_2 as $item){

    $link_page_id = (int)($item->object_id);


if ($current_id == $link_page_id){$cur=" current-menu-item current_page_item";}else{$cur="";
} 

    $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page'.$cur.'"><a  href="'.$item->url.'">'.$item->title.'</a></li>';

    }
    }

}
return $items;
}
add_filter('wp_nav_menu_items'、'get_different_custom_menu_item_根据页面_slug',10,2);
函数获取不同的自定义菜单项根据页面的不同($items,$args){
$current_id=(int)$GLOBALS['post']->id;
$current_slug=$GLOBALS['post']->slug;
$thew\u nmo=wp\u get\u nav\u menu\u object($current\u slug);
如果($the_nmo==“”){}else{
$menu\u id=$the\u nmo->term\u id;
$items_2=wp_get_nav_menu_items($menu_id);
$items=“”;
如果($args->theme\u location=='secondary'){
foreach($items_2作为$item){
$link\u page\u id=(int)($item->object\u id);
如果($current_id==$link_page_id){$cur=“当前菜单项current_page_item”;}否则{$cur=”“;
} 
$items.='
  • ; } } } 退回$items; }

    在我看来,您需要一个与页面slug完全相同的菜单。

    正如前面的评论所说,您需要条件标记。也许对你来说,特别是


    读这篇文章:不幸的是,这不是我想要做的,问题是我希望我的主导航更改为其他页面上的不同导航
    
    <?php
    if (is_page('blog')) {
       // Echo this menu on the blog slug page
          wp_nav_menu(array(
              'theme_location' => '[name_of_menu]'
          ));
    
    } else {
      // Otherwise, echo this one
          wp_nav_menu(array(
              'theme_location' => '[name_of_main_menu]'
          ));
    }