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

steven persoons
steven persoons
2,922 Points

Filter a gallery

I use a template to list all images from a custom post type in a gallery with wp_query function, this works very well ;)

Now I want to add a subnav that functions as filter for the displayed images.

The subnav will show the different available categories for the Custom Post Type.

For example:

I have a Custom Post type of 'Cars' and this CPT has 4 categories: 'AUDI','BMW','FIAT' and 'VOLVO'

First of all if you click my View all cars template, you will see all the cars and now I want to add a subnav ('Filter gallery: ALL AUDI BMW FIAT VOLVO), that depending on the above button you click will update the gallery to show only the selected category!

Do I have to use jquery to achieve this? How do I trigger the button actions?

Thanks in advance,

Steven

1 Answer

Jacobus Hindson
Jacobus Hindson
14,429 Points

Hello Steven,

I would take a look at the in_category explanation on the Codex.

The snippet is an example of where I am pulling different widget areas depending on the category of the post when looking at single.php. You should be able to emulate that on your Gallery to use a variable to set the category based off the button pressed.

 <?php if (in_category('3')) {get_sidebar('entertainment');} elseif (in_category('5')) {get_sidebar('functions');} else {get_sidebar('blog');} ?>