A
A
A1chemist2018-07-10 23:04:14
WordPress
A1chemist, 2018-07-10 23:04:14

How to set a separate template for a taxonomy category?

Created custom post type = company. It created a taxonomy = category. Taxonomy categories can be several: 1,2,3. How to display separate templates for posts of these three categories?
Now there is a single-company.php file in which the template is displayed:

while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'single-category' );

How to write multiple conditions? Tried like this:
if( wp_get_post_terms() === '1' ){
get_template_part( 'template-parts/content', '1' );
}
else {
get_template_part( 'template-parts/content', '2' );
}

does not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zorca, 2018-07-11
@zorca Куратор тега WordPress

Если почитать про wp_get_post_terms, то можно заметить, что эта функция возвращает массив. Причем массив объектов. Вам нужно задать нужные аргументы, чтобы получить только айдишники таксономий, после чего забрать нулевой элемент массива и вот тогда уже можно проверять его на соответствие.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question