Sandeep Rathore
Sandeep Rathore

Reputation: 55

How to create two different single.php for same post type?

I have a post type named 'Property'. I want to show single post in two different way. if anyone click on post then it will shows a simple layout with name of post and description.

Now i have also category for beds. now if anyone goes in category '2 Bed' then you can see all post with '2 Bed' categories('its done'). but now if anybody click on post then it have to show different single page.

my English is very bad so please excuse it.

Upvotes: 0

Views: 2155

Answers (4)

Sandeep Rathore
Sandeep Rathore

Reputation: 55

Thanks for you help. I am done with it myself. I create single.php for simple format and and in category result i didn't use the_permalink and call id of post and made a url like http://localhost/demo/page?id=$id

Upvotes: 0

Florian Rachor
Florian Rachor

Reputation: 1574

So according to the Wordpress template hierarchy there is only one single.php, that cannot be separated by category (like the archive page for example.)

https://developer.wordpress.org/themes/basics/template-hierarchy/

So in this case I suggest you read the current category id in your single.php file and then adjust the content to your needs. You can use get_the_category() do do this (reference: https://developer.wordpress.org/reference/functions/get_the_category/) which will return you an array with categories. In my simple example I just pick the first category to do something:

$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
if($category_id == 1) echo 'do something here';

Upvotes: 0

Tim Sheehan
Tim Sheehan

Reputation: 4014

You can set up individual templates for a single category by using the single_template hook.

Put this in your functions.php file:

function my_category_templates($single_template) {
    global $post;

    if ( in_category( 'property' )) {
        $single_template = dirname( __FILE__ ) . '/single-property.php';
    }

    // Copy the above for your other categories

    return $single_template;
}

add_filter( "single_template", "my_category_templates" );

You can then create individual single templates for each category, just add more conditions and point them to the template you create.

Upvotes: 2

Atif Tariq
Atif Tariq

Reputation: 2772

There is the concept of Category Templates as dictated by the Template Hierarchy but because you are asking how to display a "single" post based on category, you will want to use the in_category() Conditional Tag in the Template file you use to display singe posts. The Loop article has an example of using in_category.

Or look at this concept: http://www.nathanrice.net/blog/wordpress-single-post-templates/

Or this: http://justintadlock.com/archives/2008/12/06/creating-single-post-templates-in-wordpress

Or this plugin: http://wordpress.org/extend/plugins/custom-post-template/

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

Upvotes: 0

Related Questions