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.

How can I display a post with a certain category in the page template instead of default category template.

For an example, I want to show all posts under category "Scott" with the page template ("page.php").

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

I'm pretty sure you could also do what you want by filtering template_include. This is super-untested, but maybe this can get you headed in the right direction:

function wpse53871( $template ) {
    global $post;
    // check if is a Post and in the 'scott' category
    if( is_single( $post->ID ) && has_category( 'scott', $post ) ) {
        return get_template_part( 'page' );
    } else {
        return $template;
    }
}
add_filter( 'locate_template', 'wpse52871' );

Here's another example of the filter in use.

Additional

You can also add a filter to single_template

add_filter( 'single_template', 'wpse53871' );

share|improve this answer
    
Thanks for the edit @Chris_O. You made me realized I forgot to add the filter to my snippet (which I will do now)! Take note, @daniel.tosaba. I didn't know about single_template. That's excellent. If you use that, I assume the function wouldn't need the is_single() check? –  mrwweb May 31 '12 at 21:41
    
+1. Just a note: you might want to use get_template_directory_uri() - or, better yet, locate_template() - instead of get_stylesheet_directory_uri(). Consider Child-Theme friendliness. :) –  Chip Bennett Jun 1 '12 at 0:12
    
@chip bennett Good thought on locate_template(). I need to get in the habit of using that. Isn't "stylesheet_directory" the child theme friendly function? –  mrwweb Jun 1 '12 at 1:32
    
"Isn't "stylesheet_directory" the child theme friendly function?" Only if the Child Theme includes the specified template file. stylesheet_directory refers to the Child Theme, and template_directory refers to the Parent Theme. So, it depends entirely on context/use-case. That's why I recommend using locate_template(). :) (Note: you could also use get_template_part(), which is basically a fancy wrapper for locate_template().) –  Chip Bennett Jun 1 '12 at 11:46
add comment

There's two different types of content: 1. Pages http://codex.wordpress.org/Pages 2. Posts

Categories only apply to Posts. http://codex.wordpress.org/Category_Templates

If you're using Pages, you want to create Parent and Child pages. http://codex.wordpress.org/Pages#Organizing_Your_Pages

If you have a page and you want to list categories in it, you can add the list categories function to the page template http://codex.wordpress.org/Template_Tags/wp_list_categories

Also, I wouldn't modify page.php unless you really want to, I'd create a custom page template http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

share|improve this answer
add comment

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.