Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Kevin Cooper
Kevin Cooper
21,294 Points

Advanced Custom Fields, Displaying certain fields from specific categories

Using the <?php the_field ( 'image' ); ?> as an example, how would I get that field where ever the code is places to only show certain images from specific categories. Can I pass a category to it, like <?php the_field ( 'image', 'windows' ); ?> if so how is that set up in the ACF plugin when creating the field?

3 Answers

Kevin Korte
Kevin Korte
28,148 Points

I'm not sure I understand 100% of what you're trying to accomplish, but according to the ACF documentation, you can pass their function the_field a post id, option, taxonomy, user, etc.

A category is a type of taxonomy, so in theory, you should be able to pass the_field a category.

Maybe a use case example might help me more. Or you can play around with it and try it out.

http://www.advancedcustomfields.com/resources/the_field/

Kevin Cooper
Kevin Cooper
21,294 Points

Thanks for the reply. I'm setting up an image bank that will display fields (one of the ACF fields being an image) but only display the image if a certain category is checked. I have one of the fields setup as a taxonomy field type with the taxonomy as category. So when I add an image and check the category, say Awning, I'm not sure how I get it to display on the page.

If the image field name is 'image' and I add it to the template file as <?php the_field ( 'image', 46 ); ?> nothing shows. 46 is the Awning category id number that was set when I created the category.

So I only want the field to display that image if it matches that category. What am I missing? Thanks again for the help.

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Are you looking for a field that only lets you select images that have been uploaded and tagged with a certain category?

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

You might have to pull in the image ID and then use the wp_get_attachment_metadata() function to get the meta data, including the category for the image. You can then run a conditional statement testing if the category matches.

Kevin Cooper
Kevin Cooper
21,294 Points

Seems more of process than it's worth. Any other suggestions on how to accomplish this? The second part of the goal is to make it easier for the client to upload and place their own photos in the image bank. I like ACF because it allows to create nice clean interface for the client. If there's something else out there that you recommend, I'd be interested in seeing it. Thanks!

Kevin Cooper
Kevin Cooper
21,294 Points

I actually end up using the the WP_Query and passing the 'category_name' => in the $args = array Thanks for the help. It sometimes works out just to be able to talk it through with other devs Zac Gordon Kevin Korte