0

i know how to define custom post template vai plugins very easily but i want without plugins how can i define custom post template for particular category post.

say like i have one category "members" under this category have ten post. so i want when show those member post that time show custom template.

see bellow code from twentytwelve theme category.php file

<?php
            /* Start the Loop */
            while ( have_posts() ) : the_post();

                /* Include the post format-specific template for the content. If you want to
                 * this in a child theme then include a file called called content-___.php
                 * (where ___ is the post format) and that will be used instead.
                 */
                get_template_part( 'content', get_post_format() );

            endwhile;

            twentytwelve_content_nav( 'nav-below' );
            ?>

        <?php else : ?>
            <?php get_template_part( 'content', 'none' ); ?>
        <?php endif; ?> 

see in above code say get_template_part( 'content', get_post_format() ); and see get_template_part( 'content', 'none' ) now i want when "members" category post will open it will one custom page.. like content-members.php file, so i think code will be like that get_template_part( 'content', 'members' )

so how can define in category page members category post will open content-members.php file

2 Answers 2

0

You can use is_category to check if the category is "members" and then load the appropriate template.

In get_template_part() use the template name for the second parameter instead of get_post_format().

In this example your template should be named "content-members.php."

while ( have_posts() ) : the_post();

    if ( is_category( 'members' ) ) {

         get_template_part( 'content', 'members' );

    } else {

        get_template_part( 'content', get_post_format() );

    }

endwhile;
3
  • thanks you so much @epilektric ... and how can i define single post too.. mean which members category post show in custome page. like single-members.php page
    – pagol
    Commented Aug 18, 2013 at 18:09
  • i already tried bellow code in single.php file but not work <?php if ( is_category( 'members' ) ) { get_template_part( 'content', 'members' ); } else { get_template_part( 'content', 'common' ); } ?> i think category not tigger here.
    – pagol
    Commented Aug 18, 2013 at 18:27
  • @pagol If my answer worked for you I'd appreciate the up vote. Thanks!
    – epilektric
    Commented Aug 18, 2013 at 20:02
0

You can use in_category to check if the category is "members" and then load the appropriate template.

In get_template_part() use the template name for the second parameter instead of get_post_format().

In this example your template should be named "content-members.php."

while ( have_posts() ) : 
    the_post();

    if ( in_category( 'members' ) ) {
         get_template_part( 'content', 'members' );
    } else {
        get_template_part( 'content', get_post_format() );
    }
endwhile;

I change is_category( 'members' ) to in_category( 'members' )

1
  • This does something slightly different, though: is_category('members') means 'are we showing the members archive page' whereas in_category() means 'is this post in the members category'.
    – Rup
    Commented Oct 28, 2020 at 8:40

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.