M
M
morgana_lf2016-02-22 14:22:46
PHP
morgana_lf, 2016-02-22 14:22:46

How to make multiple single.php templates on Wordpress?

Good afternoon.
I don't know much about php, please help.
Once there was a task to make a separate single.php template for one category. At that time, I came across the following solution on the net:

<?php
$test = array(3);
if ( have_posts() ) { the_post(); rewind_posts(); }
if ( in_category($test)) { include(TEMPLATEPATH . '/single-test.php'); }
else {
    include(TEMPLATEPATH . '/single-defolt.php');
}
?>

Thus, I managed to make a separate post template for the category with id 3, and as a result, the site now has two templates: sihgle-test.php and single-defolt.php.
But now there is a need to make a third kind of template. That is, there will be one template for the category with id 3, another template for the category with id 228, and a third template for all other categories. How can you change the existing code? Or maybe there are other solutions?
Thanks everyone, issue resolved!
Decision:
<?php
$test = array(3);
if ( have_posts() ) { the_post(); rewind_posts(); }
if ( in_category($test)) { include(TEMPLATEPATH . '/single-test.php'); }
else {
    $test = array(1,4,5,6,7,10);
    if ( in_category($test)) { include(TEMPLATEPATH . '/single-defolt.php'); }
    else {
        include(TEMPLATEPATH . '/single-defolt-2.php');
    }
}
?>

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey Khokhlov, 2016-02-22
@andrhohlov

single.php is not about category pages at all,
here is a detailed diagram to help you: wphierarchy.com

A
Alexander Mamurov, 2016-02-22
@EXaneR

There is a plugin that allows you to assign different templates to Posts. Custom Post Templates

N
Nikita Souvel, 2016-02-23
@Souvel1

Look at the idea, if you have the Custom Post Types plugin (a plugin, I personally have it, a must-have), or manually make the custom field "Post Template" a checkbox and use it later in a loop with an if construct, only ABOVE in the VI for this special function get_template_part();
And in general, it is possible to connect in the following, correct way:
category-3.php For a category with ID 3
category-228.php For a category with ID 228
category.php - for ALL remaining categories

M
Mir, 2018-02-14
@mir546

and not easier to write at the beginning of single-custom_1.php
:

<?php 
/*
Template Name: single-custom_1
Template Post Type: post
*/

and then when creating a record in the console, choose which template to use?
If that 's the official dock.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question