• Resolved shahidbahader

    (@shahidbahader)


    here is my code

    
    <?php
    $ex_page = get_page_by_title( 'Privacy Policy' );
    		$ex_page_2 = get_page_by_title( 'Custom Quote' );
    		
    		if ($ex_page === NULL) {
    			$ex_page_id = '';
    			if($ex_page_2 === NULL){
    			$ex_page_id_2 = '';
    			}
    		} else {
    			$ex_page_id = $ex_page->ID;
    			$ex_page_id_2 = $ex_page_2->ID;
    		}
    		wp_list_pages( array(
    			'depth'    => 0,
    			'title_li' => '',
    			'exclude'  => $ex_page_id_2,$ex_page_id
    			)
    		);
    ?>
    

    This code is excluding only the 1st Page ID, also if I swap the page ids, it excludes the 1st ID provided.

    • This topic was modified 6 years, 11 months ago by shahidbahader.
Viewing 2 replies - 1 through 2 (of 2 total)
  • It’s because of the way you’re writing the argument. Because you haven’t grouped the 2 values in any way, the 2nd is being interpreted as another argument for the first array. So what you’ve written is the equivalent of:

    wp_list_pages( array(
    	'depth'    => 0,
    	'title_li' => '',
    	'exclude'  => $ex_page_id_2,
    	4          => $ex_page_id,
    ) );
    

    Looking at the documentation for wp_list_pages(), it says this about exclude:

    (string) Comma-separated list of page IDs to exclude.

    Without variables that would look like this:

    'exclude' => '1,2',
    

    So with variables you need to concatenate a String out of your variables, like this:

    'exclude' => $ex_page_id_2 . ',' . $ex_page_id
    

    Another option is to make an array of values and implode them:

    'exclude' => implode( ',', array( $ex_page_id_2, $ex_page_id ) ),
    
    Thread Starter shahidbahader

    (@shahidbahader)

    Thanks man, it solved my problem

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_list_pages() Function exclude only works for the 1st ID’ is closed to new replies.