• Resolved kennkelly

    (@lionchaser)


    I have a custom post type Portfolio and I want to display a different single post type layout based on the category. If its category “Superfoods” I want one layout if its any other category I want the default Portfolio post layout. Below is equivielant to what the code should be for different page layouts based on category, my question is there an exception or difference if its a custom post type? What am I missing?

    <?php if (is_portfolio_category('Superfoods')) : ?>
    	<h2>Super Food Elements</h2>
    	<p><?php the_field('superfood-elements'); ?></p>
    <?php else : ?>
            <h2>Recipe Cooking Instructions</h2>
    	<p><?php the_field('cooking-instructions'); ?></p>
    <?php endif; ?>

    I took the code out for now but here is an example of a custom post type: http://renewingallthings.com/portfolio/detox-green-smoothie/

Viewing 7 replies - 1 through 7 (of 7 total)
  • If you are inside the loop, which you seem to be, you should be able to use

    <?php in_category( 'Superfoods') ?>

    for your conditional check.

    http://codex.wordpress.org/Function_Reference/in_category

    Unless you have defined a custom taxonomy for your custom post type the category-based methods should work.

    Thread Starter kennkelly

    (@lionchaser)

    Im not in a loop, this is for a single post page template. If a unique category is checked (superfoods) I would like to slightly modify the page layout and outputs.

    Because the categories in my custom post type are unique to the categories in my post categories I believe they are unique and I think that is my problem its trying to pull regular categories, how do I pull the custom category. I originally tried the following below which has the in_category but it still didnt work.

    <div class="single-portfolio-element row-fluid" >
    	<?php if (in_category('Superfoods')) : ?>
    	<h2>Superfood Instructions</h2>
    	<p><?php the_field('superfood-instructions'); ?></p>
    	<?php else : ?>
    	<h2>Recipe Cooking Instructions</h2>
    	<p><?php the_field('cooking-instructions'); ?></p>
    	<?php endif; ?>
    </div>

    Even the single.php page template technically makes use of of the loop, unless you have done some unusual custom work to it. If you see this line, you are in the loop:

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

    The reason I ask is that the function I posted requires you to specify the post ID as a second argument if you are not in the loop. We can talk about how to find that programatically if needed.

    So, assuming you have the above line and are working within it, the next thing to try is calling your category by slug rather than the friendly name. Here’s a nice bit of diagnostic code to ensure you have that down:

    <?php print_r(get_the_category()); ?>

    That’s going to spew out all the information about the post’s category. It works easiest if the post in question is only assigned to one category, so for diagnostics I would make sure you have a test post only marked as superfoods.

    In the array that gets printed out you will find something like “[slug] => superfoods”. You are looking for the entry following “[slug] => “. Use that as your value for(in_category(‘superfoods’)). Odds are the whole error came down to case, but the print_r will help you be certain.

    If you are still concerned that this is a special category for your post type, look in the print_r output. If you see “[taxonomy] => category” this is still in the stock-standard category taxonomy. If not we’ll work from there.

    Thread Starter kennkelly

    (@lionchaser)

    So it does appear that I am inside the loop based on your first statement. Only difference in my loop there was a : between the ) and while. In any case I also ran the print_r function you posted but the only thing it spit out on the page was

    Array ( )

    Also before posting this thread I tried both sets of code I posted in this thread using the SuperFoods name “Superfoods”, slug “superfoods” and id “7” but none seem to pull it.

    I am thinking that if it was the standard category set my category superfoods and the rest of them in the portfolio section would also show up in my posts categories correct? I was hoping that print_r would would but it didnt for me. Maybe I placed it in the wrong file or place in the file, I just placed it inside the loop on the page template file.

    Also going back to your first recommendation

    <?php in_category( 'Superfoods') ?>

    How I placed it in the if statement in my second reply is correct right?

    If so I am thinking I am not calling the custom category correctly. Assuming I can find that what would be the correct full php if else statement for a custom category?

    You could try

    <?php var_dump(get_the_category()); ?>

    instead of print_r. If it’s dumping a multi-dimensional array var_dump should handle it more gracefully.

    Your use of the in_category looks to be exactly correct.

    If we do find that your posting is not in the stock category taxonomy, this function is probably where we would go next:
    http://codex.wordpress.org/Function_Reference/is_object_in_term

    Following their example code this may work anyway:

    <?php
    if ( is_object_in_term( $post->ID, 'category', 'superfood' ) ) :
    	echo 'YES';
    else :
    	echo 'NO';
    endif;
    ?>

    If diagnostics do show we are using a different taxonomy, just substitute it for ‘category’ in the second argument.

    Thread Starter kennkelly

    (@lionchaser)

    We were close but that wasn’t quite it. Per my comments I was fairly certain it was a a custom taxonomy for the custom post type.

    I needed to use

    is_object_in_term($post->ID,$taxonomy,$terms)

    Where the correct $taxonomy value was “portfolio_categories” and $terms value was the slug of the category. I was able to connect with a friend and ultimately the code below resolved the issue.

    <?php if ( is_object_in_term( $post->ID, 'portfolio_categories', 'superfoods') ) : ?>
    	<h2>Super Food Elements</h2>
    	<p><?php the_field('superfood-elements'); ?></p>
    <?php else : ?>
    	<h2>Recipe Cooking Instructions</h2>
    	<p><?php the_field('cooking-instructions'); ?></p>
    <?php endif; ?>

    Appreciate your help in the process.

    Thread Starter kennkelly

    (@lionchaser)

    Marking as resolved.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Different Page Layout based on Category of Custom Post Type’ is closed to new replies.