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

Ben Taylor
Ben Taylor
902 Points

Conditionals in Custom Post Types

Hey guys,

My custom post type is for me to add examples of work.. so basically in "Project 1" i might have 3 images I wanna show.. but in "Project 2" I might only have 1 image I want to show...

So my question is, can you have conditionals? So in advanced fields I make say 5 image fields.. then the php says "if image 1 has an image, show image 1. if image 2 has an image show image 2" etc etc?

Many thanks!

2 Answers

Ben Taylor
Ben Taylor
902 Points

SOLVED! For anyone else looking to hide empty fields, simply goto: http://www.advancedcustomfields.com/resources/how-to/hiding-empty-fields/

Matt Campbell
Matt Campbell
9,767 Points

Yes you can but I'm not quite sure what you're getting at.

Are you asking that you want to check if a post has the fields that relate to images set? If so, you can just use isset to see if the field has a value, if it does, do something.

Ben Taylor
Ben Taylor
902 Points

Hey Matthew, thanks for your reply.

Ok let me try and explain better.. I have a page called "Projects".. This displays links to my projects.. Within a project is a series of images.. So in advanced fields I make let's say 5 image fields.. So this means I can add a maximum of 5 images per project.. But if a project only has 2 images, then I don't want the remaining 3 to display broken..I want them not to display at all.. Does that make sense?