J
J
jebusSus2021-10-21 19:01:26
WordPress
jebusSus, 2021-10-21 19:01:26

3rd personal template for single.php?

Good afternoon! I display an individual post template for the news category, for all other categories, single-default is displayed. But now there is a need to add another custom template for a new category? How to implement it in the code below. Tell me please.

<?php
  $post = $wp_query->post;

  if (in_category('news')) { //slug  категории
      include(TEMPLATEPATH.'/single-news.php');
  } 
else {
      include(TEMPLATEPATH.'/single-default.php');
  }

?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2021-10-21
@jebusSus

Like this

<?php
  $post = $wp_query->post;

  if (in_category('news')) { //slug  категории
      include(TEMPLATEPATH.'/single-news.php');
  } 
elseif (in_category('individualnyj')) { //slug  категории
      include(TEMPLATEPATH.'/single-individualnyj.php');
  } 
else {
      include(TEMPLATEPATH.'/single-default.php');
  }

?>

A
Artem Zolin, 2021-10-21
@artzolin

The $post variable is already defined for you, it is not necessary to define it a second time, there is a function to connect templatesget_template_part()

if ( in_category( 'news' ) ) {
  get_template_part( 'templates/single', 'news' );
} elseif( in_category( 'articles' ) ) {
  get_template_part( 'templates/single', 'articles' );
} else {
  get_template_part( 'templates/single', 'default' );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question