0

我正在尝试从 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  ) ); ?>

不能正常工作。

4

5 回答 5

7

尝试这个

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

结帐本教程

于 2013-04-19T20:03:51.180 回答
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 ) ); 
?>    
于 2013-04-19T20:34:32.197 回答
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/

于 2014-12-26T09:17:20.697 回答
0

您可以使用自定义 Walker 函数来跳过菜单项的呈现。

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

        }

    }

}
于 2014-11-27T09:20:40.940 回答
0

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

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

于 2013-04-19T19:54:38.403 回答