WordPress.org

Ready to get started?Download WordPress

Forums

Category-based single post templates (7 posts)

  1. mindfeck
    Member
    Posted 6 years ago #

    Is it possible to have an entire template be different based on what category a post is? Maybe a php line saying if it's category 52, use this css code, or use this line of html, or something along those lines. My problem is that in one category, I want images to have a border, and in other categories, I do not.

    I was able to do this with different category page templates, but then I could not find a way for the single post view. Thanks.

  2. Jeremy Clark
    Moderator
    Posted 6 years ago #

    You can use a conditional tag.
    Heres a working example.

    <?php if ( in_category(5) ) {
    echo '<span class="my-cat-5">This is a nice category</span>';
    } else {
    echo '<span class="bad-cat">This is a BAD category</span>';
    }
    ?>

    Where 5 is the special category and then you can add speceifc style in the your stylesheet using the my-cat-5 style. You'll of course want to copy the contents of you old single post loop in between the span before and after the else. If you would like to copy the contents of your single post page here if you have any trouble then I can show you what you need.

  3. moshu
    Member
    Posted 6 years ago #

    Or use the post_templates_by_category plugin:
    http://guff.szub.net/2005/07/21/post-templates-by-category/

  4. joand
    Member
    Posted 6 years ago #

    I am trying to upgrade a blog from 2.0.4 to 2.3.3. I have used the post-templates-by-category plugin. While it works in displaying the posts, I am having a problem in 2.3.3 in editing or creating posts. When hitting the Publish button I receive the following error:

    Warning: Cannot modify header information - headers already sent by (output started at ../wp-content/plugins/post-templates-by-cat.php:96) in ../wp-includes/pluggable.php on line 390

    Also received the same error when trying to deactivate the plugin, so had to rename it.

    There is not any mention of this plugin in the list of those that work or don't work with 2.3 - so I don't know if it is still supported. I thought the problem might be with my template, but I tried it with the default template and received the same error.

    It does not seem like there is any other plugin that will do the same thing. Anyone know what could be wrong or if there is a new plugin that works?

  5. joand
    Member
    Posted 6 years ago #

    Did a little more searching here on pluggable.php and as a result edited the post-templates-by-category.php file to delete an extra space at the very end. And now it works.

  6. moshu
    Member
    Posted 6 years ago #

  7. brankoa
    Member
    Posted 5 years ago #

    I believe I have the solution you are looking for. I needed the same functionality so... here's a link to post on my site.

Topic Closed

This topic has been closed to new replies.

About this Topic