Display Posts from Custom Post Type with Category Filters

Thanks in advance for any assistance.

I have a simple 4 page portfolio site I’m trying to build for myself. The pages consist of: Home Page, Print Design, Digital, and Identity.

I have created a custom post type called “portfolio” with categories: “Print Design”, “Digital”, and “Identity”.

When first visiting the site a user will see a grid of ALL posts from the custom post type (5 columns, 4 rows). I am using the following code to pull the custom post type:

<?php 
$items = new WP_Query(array(
'post_type' => 'portfolio', 
'posts_per_page' => 1000
)); 
while ( $items->have_posts() ) : 
$items->the_post();
?>

Each post pulls the featured image into the grid with the following code:

<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
?>

So far, all is well. I have a grid layout of 20 items all displaying nicely.

Here is my issue and what I am trying to accomplish:

The home page will always display all 20 posts just as they do now.
I want the ‘Print Design’ page to display all 20 posts as-well BUT I want all posts outside of the “Print Design” category to be 50% opaque and not-clickable.

For example: A user clicks on “Print Design” and is taken to a new page. This page displays 20 posts of which only 3 stand out and are clickable. The other 17 are grey and can’t be clicked.

This same idea would be setup for each additional page listed above.

My thoughts are that I should be able to setup a condition that says something like:

“HEY! There are 20 posts on this page! Find all the posts that aren’t in category “Print Design” and use this CSS “property”!”

Thanks again!
This stuff is fun but also difficult… and also frustrating when I’ve come so far but can’t complete the idea I have.

4 Answers
4

Lifted straight from the codex:

<?php 
if ( in_category( 'pachyderms' )) {
    // They have long trunks...
} elseif ( in_category( array( 'Tropical Birds', 'small-mammals' ) )) {
    // They are warm-blooded...
} else {
    // & c.
}
?>

So, just use in_category to decide how to output each post – whether or not to make it a link, and what classes to use.

while ( $items->have_posts() ) :
    $items->the_post();

$in_category = in_category( 'identity' ); // this should be the slug for your category

Throughout the rest of your code you can then just check if $in_category is true or false, and if it is false skip the anchor tags, or set a specific class etc.

Leave a Comment