removing <li> tags from wp_list_pages() using PHP

as the title states, I am trying to remove the <li></li> tags from the list that gets generated with wp_list_pages().

My thinking is to somehow run a for/foreach loop through the menu items and remove the <li></li> tags using str_replace(), but first I would need to parse the returned list into an array or something to traverse through the list items…

Read More

Any ideas on how I can accomplish that? or maybe a better way of going about it?

Thanx in advance!

Related posts

Leave a Reply

4 comments

  1. You could try to remove them, but maybe it’s easier to not generate them in the first place. The page list is displayed by a Walker. This is a class that “walks” over all the items in the tree, and displays them. wp_list_pages() by default (via walk_page_tree()) uses the Walker_Page class, which displays everything in <li> elements. However, you can duplicate this class, remove everything it in you don’t need, and pass that class to wp_list_pages() (with the walker argument).

  2. Thanx for the answers guys, I’ve edited my answer to reflect Jan Fabry’s comment about the echo=0 argument, also thanx to One Trick Pony for pointing that out initially.

    $lookfor = array('<li','</li>');
    $replacewith = array('<div', '</div>');
    
    $args = array(
        'echo'          => 0,
        'sort_column'   => 'menu_order',
        'title_li'      => __('')
    );
    
    $output = wp_list_pages( $args ); 
    
    echo str_replace($lookfor,$replacewith,$output);
    

    I will definitely look into creating a custom walker, as it seems to be the better way of achieving the result?

    Thanx again for all your help!

  3. I used the strip_tags function:

    $args = array('child_of' => $parent, 'echo' => false, 'title_li' => false, 'depth' => 0);
    
    $output .= strip_tags(wp_list_pages($args), "<a>");