• I’m using wp_page_menu, and it outputs as:

    <div class="menu"><ul><li class="current_page_item"><a href="http://localhost/wordpress">Home</a></li><li class="page_item page-item-2"><a href="http://localhost/wordpress/about/" title="About">About</a><ul><li class="page_item page-item-4"><a href="http://localhost/wordpress/about/test/" title="test">test</a></li></ul></li></ul></div>

    I’m looking to write a function that will do the following:

    <ul id="navigation">
      <li class="home current"><a href="/" title="Link to the Home page">Home</a></li>
      <li class="about"><a href="/about/" title="Link to the About page">About</a>
        <ul>
          <li><a href="/about/test/" title="Link to the Test page">Test</a></li>
        </ul>
      </li>
    </ul>

    I have managed to remove the surrounding div and add the id to the ul with the following function:

    function add_menuid ($page_markup) {
    preg_match('/^<div class=\"([a-z0-9-_]+)\">/i', $page_markup, $matches);
    $divclass = $matches[1];
    $toreplace = array('<div class="'.$divclass.'">', '</div>');
    $new_markup = str_replace($toreplace, '', $page_markup);
    $new_markup = preg_replace('/^<ul>/i', '<ul id="navigation">', $new_markup);
    return $new_markup; }
    
    add_filter('wp_page_menu', 'add_menuid');

    However, I still need to add to this to enable title’s and also remove the ‘current_page_item’ to just ‘current’ and remove all instances of ‘page_item’ and instead of showing ‘page-item-2’, show the slug instead (eg. ‘about’)

    Thanks for help in advance.

Viewing 1 replies (of 1 total)
  • Good Hack. Stupid that wp_page_menu wraps with <div>

    I added your variable $divclass to the preg_replace line to account for when people use the menu_class option in wp_page_menu to change the class to whatever they want, so their choice of class gets put into the <ul class=”$divclass”>.

    Here the code with the fix:

    function add_menuid ($page_markup) {
    preg_match('/^<div class=\"([a-z0-9-_]+)\">/i', $page_markup, $matches);
    $divclass = $matches[1];
    $toreplace = array('<div class="'.$divclass.'">', '</div>');
    $new_markup = str_replace($toreplace, '', $page_markup);
    $new_markup = preg_replace('/^<ul>/i', '<ul id="'.$divclass.'">', $new_markup);
    return $new_markup; }
    
    add_filter('wp_page_menu', 'add_menuid');

    cheers

Viewing 1 replies (of 1 total)
  • The topic ‘wp_page_menu structure change for functions.php’ is closed to new replies.