WordPress.org

Ready to get started?Download WordPress

Forums

Adding special characters between menu items using WP_LIST_PAGES (21 posts)

  1. bpetruzzo
    Member
    Posted 5 years ago #

    I've been searching around but I couldn't find an answer to this question, although I'm pretty confident that it's not an especially difficult problem.

    Currently, my menu displays like this:
    HOME ABOUT ME BLOG POLICY BOOK REVIEWS

    I would like to add "\\" characters between the pages, like this:
    HOME \\ ABOUT ME \\ BLOG POLICY \\ BOOK REVIEWS

    I've found a few topics for adding the pipe character using CSS, but that's really not what I'm looking to do.

    How can I tell WP_LIST_PAGES to add "\\" between each page?

  2. t31os
    Member
    Posted 5 years ago #

    This will work...

    <?php $new = ' \\\\ '; wp_list_pages('link_before=<li>&link_after='.$new.'</li>');?>

    You can then change $new to be whatever you like, you need 4 slashes to have 2 display due to how PHP reads them..

    Post back if you get stuck..

  3. bpetruzzo
    Member
    Posted 5 years ago #

    Thanks for the help.

    It works except now it adds "pages" before everything else, which is not a link. It seems that it's a title or something, which I'm not sure where it's coming from.

    How can I keep the title from showing up?

  4. clompers
    Member
    Posted 5 years ago #

    Add in a blank title_li= to get rid of the Pages heading. This will also prevent the surrounding ul tags from being created so you'll end up with something like this:

    <ul><?php $new = ' \\\\ '; wp_list_pages('title_li=&link_before=<li>&link_after='.$new.'</li>');?></ul>

  5. bpetruzzo
    Member
    Posted 5 years ago #

    That worked perfectly! Thank you very much!

  6. Capn Code
    Member
    Posted 5 years ago #

    The following snippet in your template will output the desired effect:

    <?php
    $links = get_pages();
    
    foreach($links as $i => $page)
    	$links[$i] = '<li class="page-' . (is_page($page->ID) ? 'active' : 'item') . '"><a href="' . get_page_link($page->ID) . '" title="' . attribute_escape(apply_filters('the_title', $page->post_title)) . '">' . apply_filters('the_title', $page->post_title) . '</a></li>';
    
    echo implode(' | ', $links);
    ?>
  7. fatsandrew7
    Member
    Posted 5 years ago #

    capn code-

    your solution works great for what i need! thanks!

    wondering if there is any way to control the order or visibility of any of the links -- similar to how wp_list_pages() does?

    example of how wp_list_pages() does it:
    wp_list_pages('sort_column=menu_order&include=2,4,34,37&title_li=');

    thank you very much!

    andrew

  8. fatsandrew7
    Member
    Posted 5 years ago #

    nevermind -- i figured it out!!

    great advice -- thanks again!

    andrew

  9. Capn Code
    Member
    Posted 5 years ago #

    Glad it worked out for you, Andrew.

  10. Capn Code
    Member
    Posted 5 years ago #

    You can add a link to the index by adding the following:

    array_unshift($links, '<li class="page-item"><a href="' . get_option('home') . '" title="Home"> Home </a>');

    before:

    implode(' | ', $links);

  11. eassae
    Member
    Posted 4 years ago #

    Hello Capn Code.

    I am using your script and it works great. I am just having one problem. My last menu item does not get assigned the class of page-active when pressed.

    If you could offer any solutions that would be great.

  12. Capn Code
    Member
    Posted 4 years ago #

    Cor blimey! Thanks for pointing out that oversight, eassae. This will do the trick:

    array_unshift($links, '<li class="page-'. (is_front_page() ? 'active' : 'item') .'"><a href="' . get_option('home') . '" title="Home">Home</a>');

  13. David Calhoun
    Member
    Posted 4 years ago #

    @Capn Code

    How could I edit this PHP to be used with the "wp_list_bookmarks" function? I'd like to list the links and throw a " | " between each one.

    This is what I've tried so far. It will list the bookmarks correctly, but for some reason doesn't insert the " | " between them:

    <?php $links = wp_list_bookmarks('title_li=&categorize=0&sort_column=menu_order');
    								  foreach($links as $i => $page)
    								  $links[$i] = '<li><a href="' . link_url($page->ID) . '" title="' . attribute_escape(apply_filters('link_name', $page->link_name)) . '">' . apply_filters('link_name', $page->link_name) . '</a></li>';
    
    								  echo implode('<span class="divider"> | </span>', $links); ?>

    Thanks for the help.

  14. Capn Code
    Member
    Posted 4 years ago #

    dpcalhoun,

    This should do the trick:

    $links = wp_list_bookmarks('title_li=&categorize=0&sort_column=menu_order&echo=0');
    $bookmarks = explode('</li>', $links); //create array from string returned by wp_list_bookmarks
    array_pop($bookmarks); //pop last element off array
    echo implode('<span class="divider"> | </span>', $bookmarks);
  15. leroc
    Member
    Posted 4 years ago #


  16. David Calhoun
    Member
    Posted 4 years ago #

    @Capn Code

    Thank you so much! I've been trying to figure this out from your first code for awhile. I'm still a PHP novice. Slowly learning. :)

  17. Capn Code
    Member
    Posted 4 years ago #

    dpcalhoun,

    Always happy to lend a hand :-). Good luck in your travels.

  18. David Calhoun
    Member
    Posted 4 years ago #


  19. petersunna
    Member
    Posted 4 years ago #

    I found this thread very useful. Thanks all!

    @Capn Code
    Is there a way I can edit the code to do the same thing for Categories?

    Many thanks!

  20. Capn Code
    Member
    Posted 4 years ago #

    Hi Peter,

    It's accomplished much the same way:

    $categories = wp_list_categories('title_li=&echo=0');
    $categories = explode('</li>', $categories); //create array from string returned by wp_list_categories
    array_pop($categories); //pop last element off array
    echo implode(' | ', $categories);
  21. jmorganson
    Member
    Posted 4 years ago #

    I've also used some CSS to accomplish this recently.

    li:after {
    	content: "|";
    }
    
    li:last-child:after {
    	content: "";
    }

Topic Closed

This topic has been closed to new replies.

About this Topic