Support » Themes and Templates » remove li from wp_list_pages

  • Resolved mastron


    I’m new to wordpress and I’m trying to figure something out.

    In my sidebar.php I have the code:

    <div class="title"><?php _e('<a href=""><h2>Oxi-design</h2>'); ?></div>

    <?php wp_list_pages('sort_column=menu_order&exclude=79&title_li='); ?>

    this outputs in html:

    <div class="title"></a><a href=""><h2>Oxi-design</h2></div>

    <li class="page_item"></a><a href="" title="Intro">Intro</a>

    li is making it into a list and that is something I do not want. For the categories I fixed it with the


    attribute but that does not seem to work with the pages. How can I get it to work.

    Thanks in advance!!


Viewing 7 replies - 1 through 7 (of 7 total)
  • wp_list_pages() does not provide a way to turn off the list tags (not without editing WP’s source), but there is a parameter we can use to work around this: echo. Switching this off (i.e. setting to 0) will allow us to assign the output to a variable we can ‘clean’ the list tags from before displaying your Page links.

    Try this:

    $pages = wp_list_pages('sort_column=menu_order&exclude=79&title_li=&echo=0');
    $pages = str_replace(array('<li class="page_item">', '</li>'), '', $pages);
    echo $pages;

    And the one line (but more difficult to read) version:

    <?php echo str_replace(array('<li class="page_item">', '</li>'), '', wp_list_pages('sort_column=menu_order&exclude=79&title_li=&echo=0')); ?>

    Hi Kafkaesqui

    It works … but not perfect. The thing I am getting now is:

    Page 01 Page 02
    Page 03 Page 04

    Instead of

    Page 01
    Page 02
    Page 03
    Page 04

    Is there anyway to break it?



    $pages = wp_list_pages('sort_column=menu_order&exclude=79&title_li=&echo=0');
    $pages = str_replace('<li class="page_item">', '', $pages);
    $pages = str_replace('</li>', '<br />', $pages);
    echo $pages;

    This will replace the closing </li> tags with break tags.

    I’ve been staring at you code and came up with the following:

    <?php $pages = wp_list_pages(‘sort_column=menu_order&exclude=79&title_li=&echo=0’);
    $pages = str_replace(‘<li class=”page_item”>’, ”, $pages);
    $pages = str_replace(”,'<br>’, $pages); echo $pages; ?>

    I placed a <br> on the last line to brake it and it works good. But now once I select a page and the page is loaded the page text in the sidebar jumps in, like:

    page 01
    page 02
    page 03

    were page 01 is the selected page. I’ve been looking through my css if the error is in ther but if I look at the code I see this:

    <li class=”page_item current_page_item”>Intro<br>

    So the <li … is making it jump in. I’ve been playing arround with the code you gave me but I gave me know answer.

    As you have noticed, I love wordpress … but php is not my thing. I really appreciate you help on this!!



    I’ve been staring at you code

    Yeah, I got hit by an annoying element of the forum software, where editing a post here can whack HTML tags. I edited the code above to clear things up (i.e. put that break tag back in).

    But now once I select a page

    Real world testing… The following mod to the code above will take care of this:

    $pages = wp_list_pages('sort_column=menu_order&exclude=79&title_li=&echo=0');
    $pages = preg_replace('%<li class=".*">|</?ul>%U', '', $pages);
    $pages = str_replace('</li>', '<br />', $pages);
    echo $pages;

    The second $pages line now performs a regular expression match on <li class="ANYTHING">, <ul> and </ul>, and replaces them all with ” (that is, an empty value).

    Thx it all works great!!


    It’s extraordinary how the the community empoyer each of us!
    Thanks to that thread, I resolved an issue that seemed to me very complicated before.
    Hope that I contribute one day with such valuable advice.
    Thanks again.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘remove li from wp_list_pages’ is closed to new replies.