How to hook wp_list_pages?

How can I hook the wp_list_pages function so that it reads a value of a custom field and displays it instead of displaying the page title?

Related posts

Leave a Reply

3 comments

  1. A quick Google Search came up with this

    Source

    Try the following:

    function wp_list_pages_filter($output) {
        // modify $output here, it's a string of <li>'s by the looks of source
        return $output;
    }
    add_filter('wp_list_pages', 'wp_list_pages_filter');
    
  2. A Walker Class extension would be necessary in this case:

    class My_Custom_Walker extends Walker_page {
    function start_el(&$output, $page, $depth, $args, $current_page) {
        if ( $depth )
            $indent = str_repeat("t", $depth);
        else
            $indent = '';
    
        extract($args, EXTR_SKIP);
        $output .= $indent . 
                '<li><div>' . get_post_meta($post_id, $key, $single) . '</div></li>';
    
    } // End start_el
    } // End Walker Class
    

    Then, when you use your wp_list_pages function, you would call the class:

    // Call class:
    $My_Walker = new My_Custom_Walker();
    
    $args = array(
        'walker'      => $My_Walker
    );
    
    wp_list_pages( $args );
    

    Documentation on this subject is a bit scarce so let me know if you need more help.

  3. You are able to pass in your own custom walker as one of the args, use this to pass in a walker which extends Walker_Page and uses your own custom post title.