wp_nav_menu从菜单中排除页面

时间:2013-04-19 19:51:42

标签: php wordpress

我正在尝试从wp_nav_menu中排除页面

以下是从菜单中排除页面的功能

时效果很好
  <?php $page = get_page_by_title('my title' );
   wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $page->ID  ) ); ?>

正常运作

使用时的BUT

   <?php $page = get_page_by_title('my title' );
         $pag1 = get_page_by_title('my title 1' );
         $pag2 = get_page_by_title('my title2' );
   wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $page->ID,$pag1->ID,$pag2->ID  ) ); ?>

无效。

5 个答案:

答案 0 :(得分:7)

试试这个

wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => '".$page->ID.",".$pag1->ID.",".$pag2->ID."'  ) ); 

结帐tutorial

答案 1 :(得分:3)

这是正确的解决方案

我希望它可以帮助其他人

<?php 
$page = get_page_by_title('my title' );
$pag1 = get_page_by_title('my title 1' );
$pag2 = get_page_by_title('my title2' );

$ids = "{$page->ID},{$pag1->ID},{$pag2->ID}";
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $ids ) ); 
?>    

答案 2 :(得分:1)

如果将要排除的页面放入数组中,它应该可以工作。

看看这个:http://ehsanis.me/2010/11/30/wordpress-wp_nav_menu-to-exclude-pages/

答案 3 :(得分:1)

使用exclude方法对其进行管理,但按页面ID排除:

wp_nav_menu( 
  array( 
   'theme_location' => 'menu-2', 
   'exclude' => '599, 601'  
  ) 
); 

(599&601是页面ID)

写在这里:

http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/wordpress-tip-how-to-exclude-pages-from-wp_nav_menu-function/

并手动查找页面ID:

http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-find-tag-page-post-link-category-id-in-wordpress/

答案 4 :(得分:0)

您可以使用Custom Walker Function跳过菜单项的渲染。

wp_nav_menu( array(
    'theme_location' => 'menu-2',
    'walker'         => new custom_navigation
) );

class custom_navigation extends Walker_Nav_Menu {

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

        parent::start_el($item_html, $item, $depth, $args);

        $exclude = array();
        $exclude[] = get_page_by_title( 'my title' );
        $exclude[] = get_page_by_title( 'my title 1' );
        $exclude[] = get_page_by_title( 'my title2' );

        if ( ! in_array( $item->object_id, $exclude ) ) {

            // add your menu item html to the $output variable

        }

    }

}