WordPress.org

Ready to get started?Download WordPress

Forums

how to write get_meta_post in the style of wp_list_pages (2 posts)

  1. micasuh
    Member
    Posted 2 years ago #

    I'd like to replicate wp_list_pages exactly but using get_meta_post. Why?

    I have a huge list of hierarchical pages (parent/child). I'd like to list these pages out as wp_list_pages lists them.

    This is the source code I'm hoping to replicate:

    <ul class="items">
         <li class="page_item page-item-123 parent"><a href="/wordpress/region/city/" title="city"><img src="city-name.jpg" alt="City" /></a>
            <ul class='children'>
               <li class="page_item page-item-124 parent"><a href="/wordpress/region/city/suburb/" title="Suburb">Suburb</a>
                  <ul class='children'>
                     <li class="page_item page-item-125"><a href="/wordpress/region/city/suburb/complex/" title="Complex">Complex</a></li>
                  </ul>
               </li>
            </ul>
         </li>
     </ul>

    where city-name.jpg is defined in the custom fields section. All pages should show up like normal but the pages with custom fields should transfer the title name out for an image.

    How do I do this? I am starting to think that it's not possible with wp_list_pages but I'm not strong enough with PHP to do this myself.

  2. micasuh
    Member
    Posted 2 years ago #

    Had to ask the WordPress IRC for help on this one, but there's the answer. Add the following code to your functions.php file.

    /**
     * Extend the default page walker class to append class names for pages that
     * are parents.
     * @uses Walker_Page
     *
     * http://wordpress.stackexchange.com/questions/11821/class-parent-for-wp-list-pages
     *
     */
    class My_Page_Walker extends Walker_Page
    {
    /**
     * Filter in the classes for parents.
     */
     function _filterClass( $class )
     {
         $class[] = 'parent'; // change this to whatever classe(s) you require
         return $class;
     }
    
     /**
      * This is effectively a wrapper for the default method, dynamically adding
      * and removing the class filter when the current item has children.
      */
     function start_el( &$output, $page, $depth, $args, $current_page )
     {
         if ( !empty($args['has_children']) )
             add_filter( 'page_css_class', array( &$this, '_filterClass') );
    
        if ( $depth )
            $indent = str_repeat("\t", $depth);
        else
            $indent = '';
    
        extract($args, EXTR_SKIP);
        $css_class = array('page_item', 'page-item-'.$page->ID);
        if ( !empty($current_page) ) {
            $_current_page = get_page( $current_page );
            _get_post_ancestors($_current_page);
            if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) )
                $css_class[] = 'current_page_ancestor';
            if ( $page->ID == $current_page )
                $css_class[] = 'current_page_item';
            elseif ( $_current_page && $page->ID == $_current_page->post_parent )
                $css_class[] = 'current_page_parent';
        } elseif ( $page->ID == get_option('page_for_posts') ) {
            $css_class[] = 'current_page_parent';
        }
    
        $css_class = implode(' ', apply_filters('page_css_class', $css_class, $page));
    
        $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '">' . $link_before;
        $image = get_post_meta($page->ID, 'title_image', true);
        if ( isset($image) && !empty($image) ) {
            $output .= '<img src="'. $image .'" alt="'. apply_filters( 'the_title', $page->post_title, $page->ID ) .'" />';
        }
        else {
            $output .= apply_filters( 'the_title', $page->post_title, $page->ID );
        }
        $output .=  $link_after . '</a>';
    
        if ( !empty($show_date) ) {
            if ( 'modified' == $show_date )
                $time = $page->post_modified;
            else
                $time = $page->post_date;
    
            $output .= " " . mysql2date($date_format, $time);
        }
    
         if ( !empty($args['has_children']) )
             remove_filter( 'page_css_class', array( &$this, '_filterClass') );
     }
    }

    Change title_image to the custom fields value of your choice.

Topic Closed

This topic has been closed to new replies.

About this Topic