1

I'm having trouble getting child pages to use the parent li class whilst using wp_list_pages(). I'm using a walker to get the list classes to use page title, but no matter which way I try, I can't transfer the parent li class to its child. Here's what I'm trying to achieve:

<li class="ebooks"><a href="#">The First Ebook</a></li> /* Child of 'Ebooks' */
<li class="webinars"><a href="#">Webinars</a></li>
<li class="modules"><a href="#">Modules</a></li>
<li class="ebooks"><a href="#">Ebooks</a></li>   /* Parent of 'The First Ebook' */

The child li class on every method I've tried outputs 'The First Ebook' instead of its parent class 'Ebook'.

Here's my Walker:

class Walker_Child_Classes extends Walker_page
{

function start_el( &$output, $page, $depth, $args, $current_page )
{
    if ( $post->post_parent )
    {
        $css_class = apply_filters(
            'the_title',
            $parent_title = get_the_title( $page->post_parent ),
            $page->name
        );
    }
    else
    {
        $css_class = apply_filters(
            'the_title',
            $page->post_name,
            $page->ID
        );
    }

    $output .= $indent
        . '<li class="' 
            . $css_class 
        . '">'
        . '<a href="' 
            . get_page_link( $page->ID )
            . '" title="' 
            . esc_attr( 
                wp_strip_all_tags( 
                    apply_filters( 
                        'the_title', 
                        $page->post_title, $page->ID 
                    )
                )
            ) 
        . '">' 
            . $link_before 
            . apply_filters(
                'the_title',
                $page->post_title,
                $page->ID
            ) 
            . $link_after 
        . '</a>';
} // end function/method

} // end Walker class

And I'm calling it using this:

$sWalker = new Walker_Child_Classes();
wp_list_pages( array( 
    'title_li'    => null, 
    'walker'      => $sWalker, 
    'depth'       => -1, 
    'sort_column' => 'post_date', 
    'sort_order'  => 'DESC', 
    'child_of'    => $post->ID
) );
  • Welcome to WordPress Development. Why do you use $post->post_parent? Where is $post defined in your code? I think you meant to use the $page object instead. – birgire Jul 3 '14 at 20:35
  • Thanks, birgire..I should have noticed that! Made so many mods, it slipped me by. – user55100 Jul 4 '14 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.