Site logo

Archived topic

Custom ACF Navigation Question...

3 replies · Started by Eric on August 14, 2021

Viewing posts 1–4 of 4

Hey GP Team! I hope everyone is staying healthy and safe...

I just had a quick question about this code you guys helped me out with for my Navigation bar a few months ago... it's been working great for the most part but I had a couple of problems installing it onto some of my sites and just wanted to clarify:

At the moment this code allows me to replace the nav label with my "ACF1" custom field. It also removes the item from the nav when ACF1 is not filled out:

add_filter( 'wp_nav_menu_objects', function( $items, $args ) { foreach ( $items as $key => $item ) { if ( 'POSTID' === $item->title ) { $custom_field = get_field('ACF1', POSTID); if ( $custom_field ) { $item->title = $custom_field; } else { unset( $items[ $key ] ); } } } return $items; }, 10, 2 );

What do I need to change for it ALSO remove the item if the post is still a draft? The current code below displays the nav label regardless of whether or not the post is published live on the site, so it creates a bunch of 404 links for some of my sites which have ACF1 filled out but are still drafts.

That's pretty much all I wanted to ask... thank you so much for your time and attention guys, and more power to the GP Team! GeneratePress is the BEST!

Hi there,

try this:

add_filter( 'wp_nav_menu_objects', function( $items, $args ) { 
    foreach ( $items as $key => $item ) { 
        if ( 'POSTID' === $item->title && 'draft' !== get_post_status ($item->object_id)  ) { 
            $custom_field = get_field('ACF1', POSTID); 
            if ( $custom_field ) { 
                $item->title = $custom_field; 
            } else { 
                unset( $items[ $key ] ); 
            } 
        } 
    } 
    return $items; 
}, 10, 2 );

Hi David! Thank you for your response but unfortunately it doesn't seem to work... This is how it looks on my site now with the code installed:

add_filter( 'wp_nav_menu_objects', function( $items, $args ) { foreach ( $items as $key => $item ) { if ( '9738' === $item->title && 'draft' !== get_post_status ($item->object_id) ) { $custom_field = get_field('acf1', 9738); if ( $custom_field ) { $item->title = $custom_field; } else { unset( $items[ $key ] ); } } } return $items; }, 10, 2 );

I have a ton of nav items so I've had to remove the spaces in the code to make things looks neater in functions.php, but I made sure everything was copied over correctly... did I mess something up? It is still displaying posts with ACF1 filled up even if they are drafts.

Not sure on this but my conditional logic is all wrong, as the else { unset( $items[ $key ] ) would never get fired if the 'draft' !== get_post_status ($item->object_id) is met. So you could try moving the unset condition:

add_filter( 'wp_nav_menu_objects', function( $items, $args ) { 
    foreach ( $items as $key => $item ) { 
        if ( 'POSTID' === $item->title && 'draft' !== get_post_status ($item->object_id)  ) { 
            $custom_field = get_field('ACF1', POSTID); 
            if ( $custom_field ) { 
                $item->title = $custom_field; 
            } 
        } else { 
            unset( $items[ $key ] ); 
        } 
    } 
    return $items; 
}, 10, 2 );

But i am not sure - its realy a Custom Development requirement

This archived topic is closed to new replies.