Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'd like to integrate get_post_meta into the wp_list_pages mix so that any top-level parent list items get replaced with images ONLY when a key exists in the post's custom fields section. I can't do it with the Featured Image b/c that's already being used for another purpose inside the post itself.

Here's my Walker_Page class in functions.php:

/**
 * 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') );

         parent::start_el( $output, $page, $depth, $args, $current_page );

         if ( !empty($args['has_children']) )
             remove_filter( 'page_css_class', array( &$this, '_filterClass') );
     }
 }

So, I'm currently using wp_list_pages to show Custom Post Type posts like this:

<ul class="items"><?php
$args = array(
    'walker'=>new My_Page_Walker,
    'post_type' => 'region',
    'menu_class' => 'nav-menus',
    'title_li' => ''
);
wp_list_pages($args);
?>
</ul>

It spits out the following code:

<ul class="items">
     <li class="page_item page-item-123 parent"><a href="/wordpress/region/city/" title="city">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>

and I'd like the following:

<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. It should only show up for the post title that has a custom field.

Is this a really complex amount of code where I can't use wp_list_pages? I honestly can't find an answer online.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I went to the #wordpress irc channel and was given the answer by Marko Heijnen. He grabbed the Walker Code from /wp-includes/post-template.php and added a few lines to it.

/**
 * 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') );
 }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.