1

We know that in WordPress the default post template file is single.php. Now, I have one category name "members" and want members posts to show a custom template, like content-members.php.

I can do that via custom post template plugins. but is there a way to make that without plugins?

Maybe we can do that like this:

<?php
    if ( is_category( 'members' ) ) {

         get_template_part( 'content', 'members' );

    } else {

        get_template_part( 'content', 'common' );

    }
 ?>

I tried this already, but it's not working and I think category is not triggering here.

2
  • Where are you adding the above code? Can you show me the whole code? Commented Aug 18, 2013 at 19:08
  • this is in single.php file... <?php get_header(); ?> <div class="contentarea cf"> <?php while ( have_posts() ) : the_post(); ?> <h3><?php the_title(); ?></h3> <?php if ( is_category( 'members' ) ) { get_template_part( 'content', 'members' ); } else { get_template_part( 'content', 'common' ); } ?> <?php endwhile; ?> </div> <?php get_footer(); ?>
    – pagol
    Commented Aug 18, 2013 at 19:12

2 Answers 2

4

I believe you might need to use in_category() instead of is_category().

in_category() checks if the current post is in a category, is_category() checks if it's a category archive page.

3
  • wow... cool.. its work nicely :) thanks @Michelle .... now i am playing with it is that correct way now.. how i am doing for multipal categor post.. its work but just for am i in write way or not.. <?php if ( in_category( 'salon' ) ) { get_template_part( 'content', 'salon' ); } if ( in_category( 'staff' ) ) { get_template_part( 'content', 'staff' ); } else { get_template_part( 'content', 'test' ); } ?>
    – pagol
    Commented Aug 18, 2013 at 19:21
  • If this works perhaps you could mark it as correct? Your comment appears to be a brand new question regarding basic logic and PHP if else statements
    – Tom J Nowell
    Commented Aug 18, 2013 at 19:38
  • @michelle Yes! Your response was way more elegant than mine. I'm not sure why I din't think of `in_category'.
    – epilektric
    Commented Aug 18, 2013 at 19:47
0

You are correct, the single.php template does not have any categories associated with it. You need to first get the category for that individual post.

Single Category

$category = get_the_category();
$cat_name = $category[0]->cat_name;
if ( $cat_name == 'Members' ) {

     get_template_part( 'content', 'members' );

} else {

    get_template_part( 'content', 'common' );

}

One thing to note here: This grabs only the first category associated with a post. If the post is in more than one category and "members" is not the first category in the list the post will not use the correct template.

Multiple Categories

If you want this to work with multiple categories you need to loop through them and look for the correct one.

$categories = get_the_category();
$cat_exists = false;
foreach($categories as $category) {
    if( $category->name == 'Members' ) {
        $cat_exists = true;
        break;
    }
} 
if ( $cat_exists ) {

     get_template_part( 'content', 'members' );

} else {

    get_template_part( 'content', 'common' );

}
3
  • sorry guys... now i have one more request. now i want when member will open. i mean template come then show all members post. not only single post.. just all post. so for that i think need to write something content-members.php file. i am not sure... can your guys tell me how can i do that. i tried some way but not work...
    – pagol
    Commented Aug 19, 2013 at 15:41
  • That would be best answered by submitting it as a new question. It's not really related to this question and it would be difficult to answer in the comments.
    – epilektric
    Commented Aug 19, 2013 at 16:03
  • @epilektric... i already make seperate question wordpress.stackexchange.com/questions/110633/…
    – pagol
    Commented Aug 19, 2013 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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