WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] remove li from wp_list_pages (8 posts)

  1. mastron
    Member
    Posted 7 years ago #

    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="http://www.oxi-design.com/website"><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="http://www.oxi-design.com/website"><h2>Oxi-design</h2></div>

    <li class="page_item"></a><a href="http://www.oxi-design.com/website/index.php/intro/" 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

    list=o

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

    Thanks in advance!!

    Niel

  2. Kafkaesqui

    Posted 7 years ago #

    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:

    <?php
    $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')); ?>

  3. mastron
    Member
    Posted 7 years ago #

    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?

    Thx

    Niel

  4. Kafkaesqui

    Posted 7 years ago #

    <?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('</li>', '<br />', $pages);
    echo $pages;
    ?>

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

  5. mastron
    Member
    Posted 7 years ago #

    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:

    Pages
    page 01
    page 02
    page 03
    etc.

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

    THX!!

    Niel

  6. Kafkaesqui

    Posted 7 years ago #

    "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:

    <?php
    $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).

  7. mastron
    Member
    Posted 7 years ago #

    Thx it all works great!!

    Niel

  8. fxbodin
    Member
    Posted 6 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic