Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to style my portfolio posts with different templates. My thought is that it would be done by category. I have tried using in_category and is_category and a lot of other work around's but to no avail. The code in my single.php file currently is:

<?php if ( '' != locate_template( 'content-'.get_post_type().'.php' ) ) // Check if themplate exists esle get default post content
  {
     get_template_part( 'content', get_post_type() );
  } else {
      get_template_part( 'content', 'post' );
  }
?>

And though I seem to understand what its doing can't seem to come up with a conditional allowing it to check the category and returning the template accordingly.

What should I do?

share|improve this question

2 Answers 2

Using single.php with conditional template part inclusion for displaying a specific layout for your custom post type is not the right way to do things.

WordPress has something called the Template Hierarchy. The right way would be to create a single-name-of-your-cpt.php to display single portofolio items. The same applies to custom post type archives.

The second problem is that categories are a build-in taxonomy. Category archive pages use the archive.php template, but you won't see your custom post type show up there, because only posts are included. You could always modify the query via the pre_get_posts action, but I don't think that this is what you want.

For your case the best thing might be to use a Custom Taxonomy.

Let's say that you have a custom post type called portfolio and a custom taxonomy called portfolio-category.

With this set up you could use an archive-portfolio.php template to display all your portfolio posts and an taxonomy-portfolio-category.php to display the portfolio posts in a certain portfolio category. You can even create archive templates for certain portfolio categories this way.

For the single views, use single-portfolio.php and use has_term() to check in what portfolio category the post is and include the corresponding template part this way, making sure to have a default layout as fallback.

share|improve this answer

You can use template_include to set a specific template to a specific types of content using query_vars. Here is a example of how to set a specific single.php called single-information.php to a custom post type called information

function pietergoosen_info_template_include( $original_template ) {
    if ( isset( $wp->query_vars['information'] ) && false == $wp->query_vars['information']  ) {
        return get_template_directory() . '/single-information.php';
    } else {
        return $original_template;
    }
}

add_filter( 'template_include', 'pietergoosen_info_template_include' );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.