Home › Forums › Front-end Issues › Getting CF assigned to category
Hi,
I need help with the following scenario: I have a website with the following structure:
Country1 (Cat)
-Country1-Culture (Cat)
-Country1-Tourism (Cat)
-Country1-History (Page)
Country2 (Cat)
-Country2-Culture (Cat)
-Country2-Tourism (Cat)
-Country2-History (Page)
———–
CountryN (Cat)
-CountryN-Culture (Cat)
-CountryN-Tourism (Cat)
-CountryN-History (Page)
// no more countries 🙂
Food (Cat)
-Subfood1 (Cat)
-Subfood2 (Cat)
-Subfood3 (Cat)
———–
I’d like to have different nav menus for each country and another nav menu for the Food section and I’m using ACF to accomplish it. I’ve already created the nav menus, the positions in the theme and I’ve assigned them in WP. I got 2 CFs:
food_nav_menu – Type Select – with only 1 option – food_nav_menu – it is set as default and the CF is set to appear on all posts falling in the category Food;
country_nav_menu – Type Select – options for all the nav menus, no default, set to appear on all categories if not equal to Uncategorized and Food.
Using the following code to display the “correct” nav menu:
<?php
if ( get_post_meta($post->ID, "food_nav_menu", true))
wp_nav_menu ( array (
'menu' => 'alt_main_nav',
'menu_class' => 'nav',
'theme_location' => 'alt_main_nav',
'container' => 'false',
'fallback_cb' => 'bones_main_nav_fallback',
'depth' => '3',
'walker' => new Bootstrap_Walker()
));
elseif (get_post_meta ($post->ID, "country_nav_menu", true))
wp_nav_menu ( array (
'menu' => get_field('country_nav_menu'),
'menu_class' => 'nav',
'theme_location' => get_field('country_nav_menu'),
'container' => 'false',
'fallback_cb' => 'bones_main_nav_fallback',
'depth' => '3',
'walker' => new Bootstrap_Walker()
));
else
bones_main_nav();
?>
And it all works as expected, except when browsing empty categories (with no posts to get_post_meta from). After digging in the ACF options I found that I can assign CF to a category – I believe it is possible to skip the setting for each post and creation of dummy posts by using instead that “if post falls in category -Country2-Tourism we are already set, becase the CF country_nav_menu for Country2-Tourism has been already set”. However, I’m not able to properly get the corresponding field if I’m not using
get_post_meta ($post->ID, "country_nav_menu", true)
I’m a total beginner at PHP so any help would be highly appreciated.
Thanks!
Hi @DefJam
Why are you using get_post_meta and also get_field…?
Just use get_field, that is the better way to code.
As for you question, I’m completely lost as to what isn’t working. Can you please re-explain the issue?
Thanks
E
Thanks for your reply and sorry for that long post 🙂
I rewrote the code using only get_field
and it works… I used get_post_meta
because I found a snippet somewhere. My issue is that get_field('country_nav_menu')
doesn’t seem to “get” the field value when it is assigned to a category. It works fine when assigned to a post, but not to a category.
Thanks for you time!
The topic ‘Getting CF assigned to category’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.