Forums

help with category (4 posts)

  1. grishart
    Member
    Posted 1 year ago #

    Hi all
    I want to set up a conditional statement into your single.php script that detects what the requested category is and displays a different template.

    Does anyone know how to do this.

    thanks

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    single posts can have multiple categories;

    you can check if the single post is in a certain category with the conditional tag in_category()

    http://codex.wordpress.org/Function_Reference/in_category
    http://codex.wordpress.org/Conditional_Tags#A_Category_Page

  3. grishart
    Member
    Posted 1 year ago #

    Hi thanks for this but when I put the following code into my single.php outside the loop the post don't appear when you click through the link to view them.

    <?php
    if ( in_category('create') ) {
    	include 'single-create-2.php';
    } elseif ( in_category('St Martins news') ) {
    	include 'single.php';
    } else {
    	// Continue with normal Loop
    	if ( have_posts() ) : while ( have_posts() ) : the_post();
    	// ...
    }
    ?>

    http://stmartinoftours-org-uk.greenstrata.com/category/create-2/

    Have I set this up wrong or am I missing something?

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    in_category() might not work outside the loop.
    also the permalink looks a bit strange with the .../2011/01/400/

    i receive a '500 internal server' error when clicking on the post title.

    you could paste the whole code of single.php into a http://wordpress.pastebin.com/ and post the link to it here - so the code can be seen in its context.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.