Vaibhav Bhanushali Vaibhav Bhanushali - 2 months ago 27
PHP Question

wp_nav_menu exclude pages from menu

I am trying to exclude pages from wp_nav_menu

Here is the function to exclude pages from menu
Works well when

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


Works properly

BUt when using

<?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 ) ); ?>


Doesn't work properly.

Answer

HEre is the correct soultuion I hope it helps some other people

     <?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 ) ); ?>