How do I make a Pages menu without <ul> … <li> ?
-
I want to use wp_page_menu() without any
<ul>
or<li>
tags.Is there anyway I can customise the output of wp_page_menu() beyond the regular $args?
-
Or can I run a custom loop to pull Page details?
I just discovered the
get_pages()
function.However, my setup below does not display them in
menu_order
and it does notexclude
the specified Pages. Any tips?<?php $args = array( 'sort_column' => 'menu_order', 'sort_order' => 'asc', 'exclude' => '120,121', 'include' => '', 'child_of' => 0, 'parent' => 0, 'exclude_tree' => '', 'hierarchical' => 1, 'meta_key' => '', 'meta_value' => '', 'authors' => '', 'number' => '', 'offset' => 0 ); $pages = get_pages(); foreach ($pages as $page) { echo $page->post_title; } ?>
here is a way to ‘strip’ the ul/li/class from the wp_page_menu() –
probably not the most elegant/effective solution, but works:<?php $args = array( 'sort_column' => 'menu_order', 'sort_order' => 'asc', 'exclude' => '120,121', 'include' => '', 'child_of' => 0, 'parent' => 0, 'exclude_tree' => '', 'hierarchical' => 1, 'meta_key' => '', 'meta_value' => '', 'authors' => '', 'number' => '', 'offset' => 0, 'echo' => 0 ); $pag=explode('a href',wp_page_menu( $args )); $i=0; // counter to skip first fragment $pagelist=''; // output string $sep='<br />'; // separator between list elements foreach($pag as $pa) : if($i==0) {$i++; continue;}; $pac=explode('</li',$pa); // break off useless overhang $pagelist=$pagelist.'<a href'.$pac[0].$sep; //build output string with separators endforeach; echo substr($pagelist,0,-strlen($sep)); // output after removing exceess separator ?>
here is a less radical version – replacing the
<li>
with<span>
and removing any<ul> </ul>
.
this is keeping the css classes and any titles or enclosing divs that come from from wp_page_menu().<?php // list free page list with span instead $args = array( 'sort_column' => 'menu_order', 'sort_order' => 'asc', 'exclude' => '120,121', 'include' => '', 'child_of' => 0, 'parent' => 0, 'exclude_tree' => '', 'hierarchical' => 1, 'meta_key' => '', 'meta_value' => '', 'authors' => '', 'number' => '', 'offset' => 0, 'echo' => 0 ); $pgl=wp_page_menu( $args ); $sep=' - '; // optional separator $rep='</span>'.$sep; // adding separator to replacent for </li> $pgl=str_replace('</li>',$rep,$pgl); // replace all closing li with closing span $pgl=str_replace('<li ','<span ',$pgl); // replace all li with span $pgl=str_replace('</ul>','',$pgl); // replace closing ul, in this case with '' $pgl=str_replace('<ul>','',$pgl); // replace ul, in this case with '' if($sep!='') : $pgl=strrev($pgl); $sep=strrev($sep); // this is needed for removing the last separator $pgl=explode($sep,$pgl,2); // this is needed for removing the last separator $pgl=implode('',$pgl); $pgl=strrev($pgl); // this is needed or removing the last separator endif; echo $pgl; // echo pagelist ?>
Try this:
<?php $args = array( 'sort_column' => 'menu_order', 'exclude' => '120, 121' ); $pages = get_pages($args); foreach ($pages as $page){ echo $page->post_title; } ?>
A few notes about your code:
1. You don’t need to specify all values to get_pages().
2. Check the defaults, not specifying these will save you time.
3. !Most importantly! Make sure you actually pass $args into the function, not sure what I mean, check your original code. You specify all the arguments in $args, but dont them use them when calling get_pages() ??Thanks for those tips. I learnt a bit.
Here’s my working result.
I’m keeping all the $args because I intend to use a few more of them later.<?php $args = array( 'child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'menu_order', 'hierarchical' => 1, 'exclude' => '120,121', 'include' => '', 'meta_key' => '', 'meta_value' => '', 'authors' => '', 'parent' => 0, 'exclude_tree' => '', 'number' => '', 'offset' => 0 ); $pages = get_pages($args); foreach ($pages as $page) { $menu = '<span class="hmenu"><a href="'.get_page_link($page->ID).'" title="">'.$page->post_title.'</a></span>'; echo $menu."\n"; } ?>
I needed something similar for outputting the page list in a custom xml document, so I could read it with another program.
I used wp_list_pages() with a slight modification to two files: post-template.php and classes.php
first post-template.php (this is where the wp_list_pages function sits):
In the function, add a custom argument
'wrapper_tag' => 'li'
to the $defaults array. That’s it for that file. Then in classes.php, find the Walker_Page Class and find the line that reads:
$output .= $indent . '<li class="' . $css_class
and change it to:
$output .= $indent . "<$wrapper_tag class=\"" . $css_class
then, a little further on, change:function end_el(&$output, $page, $depth) { $output .= "</li>\n"; }
to:
function end_el(&$output, $page, $depth,$args) { extract($args, EXTR_SKIP); $output .= "</$wrapper_tag>\n"; }
You see I included all the $args and extracted them just to get the $wrapper_tag, so this may not be the most efficient solution, but it works. I probably should have created my own funtion/class, but I’m lazy.
Finally, to use it, just use:
wp_list_pages('title_li=&depth=1&wrapper_tag=whatever');
- The topic ‘How do I make a Pages menu without <ul> … <li> ?’ is closed to new replies.