user1788364
user1788364

Reputation: 1151

WordPress change position of custom wp_nav_menu_items

I have a sidebar menu which is populated through the normal WordPress menu (Appearance > Menus).

I also have a custom menu created using 'wp_nav_menu_items' that gets added to the bottom of the WP menu. My issue is I need to be able to change the order of that custom menu. This is how it currently looks:

What I'd like to achieve:

This is how my code currently looks:

class My_Walker_Nav_Menu extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth = 0, $args = Array()) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"childnav\">\n";
    }
    function end_lvl(&$output, $depth = 0, $args = Array()) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
    }
}

add_filter( 'wp_nav_menu_items', 'custom_pineapple_navigation', 10, 2 );
function custom_buy_navigation ( $items, $args ) {
    $your_query = new WP_Query( 'pagename=pineapples' );
    while ( $your_query->have_posts() ) : $your_query->the_post();
        $items .= '<li class="haschild"><a href="#">Pineapple</a>                 
            <ul class="childnav">
                <li class="breadcrumb"><a href="#">Back to main menu</a></li>
                <li class="label"><a href="#">Pineapple</a></li><span class="scrollMenu scrollbar-outer">';
                    $customPosts = get_field('my_pineapple_custom_field');
                    if( $customPosts ): 
                        foreach( $customPosts as $customPost): 
                            setup_postdata($customPost);
                            $items .= '<li><a href="'.get_permalink($customPost->ID).'">'.get_the_title($customPost->ID).'</a></li>';                                                 
                        endforeach; 
                        wp_reset_postdata();
                    endif;
                $items .= '</span></ul></li>';   
    endwhile;
    wp_reset_postdata();

    // More Queries like above

    return $items;
}

Is there a way to achieve what I'm after?

Upvotes: 0

Views: 606

Answers (1)

Rafayel Tob
Rafayel Tob

Reputation: 11

if i get you right then you need to change your if statement like this

  $i = 0;
if( $customPosts ): 
foreach( $customPosts as $customPost): 
if($i == 2) {
    echo '<a href="#">Pineapple</a>';
} else {
    setup_postdata($customPost);
    $items .= '<li><a href="'.get_permalink($customPost->ID).'">'.get_the_title($customPost->ID).'</a></li>';                                                 
endforeach; }
$i++;

Upvotes: 1

Related Questions