WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_list_pages/wp_nav_menu with a variable id count for each item (4 posts)

  1. leapin_leprechaun
    Member
    Posted 4 years ago #

    Hey

    I'm trying to add a class/id to each item that comes out of wp_list_pages and wp_nav_menu. I got as far as doing a preg_replace on current classes on it and thats fine I can get a class in.

    My problem is that I need the new class to be a variable that gets added to after each item is outputted. So a count++ after each page item. Using a variable is fine but its a count after each one thats causing me hastle.

    Essentially what I'd like to end up with is something along these lines..

    <ul>
    <li class="item1">List item</li>
    <li class="item2">List item</li>
    <li class="item3">List item</li>
    <li class="item4">List item</li>
    </ul>

    What I've got so far in the functions file is

    $idcounter = 0;
    function add_markup_cat($output) { // add a class to menu-item
    	global $idcounter;
        return preg_replace('/menu-item /', $idcounter, $output, 20);
    }
    add_filter('wp_nav_menu', 'add_markup_cat');

    I'm thinking its something along the lines of a for each but I just can't figure it out.

    The navigation code I'm using is this that I found online..

    <?php
    
    $parent_id = $post->post_parent;
    $parent = get_post($parent_id);
    if($parent->post_parent){
    $top_level_id = $parent->post_parent;
    }else if($post->post_parent){
    $top_level_id = $post->post_parent;
    }else{
    $top_level_id = $post->ID;
    }
    if($parent->post_parent){
    $children = wp_list_pages("title_li=&child_of=19&echo=0");
    $my_id = $parent->post_parent;
    }else if($post->post_parent){
    $children = wp_list_pages("title_li=&child_of=19&echo=0");
    $my_id = $post->post_parent;
    }else{
    $children = wp_list_pages("title_li=&child_of=19&echo=0");
    $my_id = $post->post_parent;
    }
    if ($children) {
    
    $post_id_7 = get_post($my_id);
    $title = $post_id_7->ID;
    
    ?>
    
    <ul class="twocol">
    <?php echo $children; ?>
    </ul>

    If anyone can help at all I'd really really appreciate it! Bangin my head off the wall here!

    Thanks a million for your time

    leapin

  2. leapin_leprechaun
    Member
    Posted 4 years ago #

    oh i meant to say i had the count++ in here

    $idcounter = 0;
    function add_markup_cat($output) { // add a class to menu-item
    	global $idcounter;
        return preg_replace('/menu-item /', $idcounter, $output, 20);
    	$idcounter ++;
    }
    add_filter('wp_nav_menu', 'add_markup_cat');

    but it just gives one value back rather than one for each item.

    Any help, greatly appreciated!

  3. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    in place of:
    <?php echo $children; ?>

    you could try and use:

    <?php
    $kiddy_bits = explode('<li ', $children);
    $children = ''; $i = 1;
    foreach($kiddy_bits as $bits) :
    if($bits) { $children = $children.'<li class="item'.$i.'" '.$bits; $i++; }
    endforeach;
    echo $children;
    ?>
  4. leapin_leprechaun
    Member
    Posted 4 years ago #

    aww alchymyth thank u so so much! it's perfect, ur a superstar!

    thanks a million!

Topic Closed

This topic has been closed to new replies.

About this Topic