Home / functions.php / Display wp_list_pages with dynamicly created exclude list
  • How to Display wp_list_pages with dynamicly created exclude list

    display-wp_list_pages-with-dynamicly-created-exclude-list

    Adding the first snippet to the functions.php of your wordpress theme will use wp_list_pages but filter out any pages that contain no content creating an excluded_pages array. Then add the second snippet into your wordpress theme in the location you wish to display the pages.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    function wps_wp_list_pages(){
    $excluded_pages = array();
    $all_pages = get_pages();
    foreach ( $all_pages as $the_page ) {
        if ('' == $the_page->post_content) {
            $excluded_pages[] = $the_page->ID;
        }
    }
    wp_list_pages( array(
        'exclude' => implode(',',$excluded_pages)
    ));
    }
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
    <? wps_wp_list_pages(); ?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    get_pages, wp_list_pages, get_page, wp,