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.

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...

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

Thanx in advance!

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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).

share|improve this answer
    
+1 for using a custom walker. –  t31os Feb 21 '11 at 14:20
    
Thanx for that, will definitely look into it! –  Odyss3us Feb 21 '11 at 14:56

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!

share|improve this answer
    
@user2632: You don't need the output buffering if you use the echo argument of wp_list_pages(): if it is false the function will return the output instead of writing it itself. –  Jan Fabry Feb 21 '11 at 15:11
    
Awesome, will give it a go, thanx for pointing that out! –  Odyss3us Feb 21 '11 at 15:15
    
@Jan Fabry: Thanx, that did the trick. –  Odyss3us Feb 21 '11 at 15:30
    
@user2632: Remember that the list also contains <ul> elements. And if you just want to replace them with <div> elements for styling, you can also adapt the stylesheet to cancel the default list-style for <li> elements, so they behave like any block element (as if they were just <div>'s). The Twenty Ten menu is a <ul><li> list, just styled with CSS to look different. –  Jan Fabry Feb 21 '11 at 15:35
    
@Jan Fabry: I'm actually only trying to get pages with specific meta keys and values, and I've specified 'title_li' => __('') so right now there are no <ul> elements to remove, but thank you for pointing that out, it will definitely help later on if I add in multi level navigation. –  Odyss3us Feb 21 '11 at 15:43

if you don't want to create your own walker, simply use str_replace(array('<li>', '</li>', '<ul>', '</ul>'), '', wp_list_pages('echo=0'));

share|improve this answer
    
It probably should be a bit more advanced, because the opening <li> has a variable class attribute, and the opening <ul> has a fixed class='children' attribute. –  Jan Fabry Feb 21 '11 at 14:21
    
the problem with a simple str_replace is the wp_list_pages() adds other content inside the <li> tag, such as class content –  DTest Feb 21 '11 at 14:23
    
then remove them with regex: preg_replace('@\<li([^>]*)>(.*?)@i', '$2', wp_list... ) –  onetrickpony Feb 21 '11 at 14:27

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.