@Legal2019
Всё в имени моём... и радость и печаль...

Как сделать вывод на конкретный шаблон?

Есть тип записи "ААА" с таксономией "БББ", в которой есть тремы "Ввв", "Ггг" и т.п. (включая дочерние термы "Ввв1" и т.п.)
Есть файл single-AAA.php в котором есть такой стандартный код:
<article class="article">
<?php

    // Start the loop.
    while ( have_posts() ) : the_post();
    
if( is_single(1086) ){
    get_template_part( 'taxonomy/content-taxonomy/content-taxonomy-Ввв', 'content-taxonomy-Ввв' );
} elseif ( is_single(1074) ){
    get_template_part( 'taxonomy/content-taxonomy/content-taxonomy-Ггг', 'content-taxonomy-Ггг' );
}
    
        // If comments are open or we have at least one comment, load up the comment template.
    if ( comments_open() || get_comments_number() ) {
    comments_template();
    }
    if ( is_singular( 'attachment' ) ) {
    // Parent post navigation.
    the_post_navigation( array(
        'prev_text' => _x( '<span class="meta-nav">Published in</span><span class="post-title">%title</span>', 'Parent post link', 'mylbo' ),
	) );
    } elseif ( is_singular( 'post' ) ) {
        // Previous/next post navigation.
	the_post_navigation( array(
            'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next', 'mylbo' ) . '</span> ' .
            '<span class="screen-reader-text">' . __( 'Next post:', 'mylbo' ) . '</span> ' .
            '<span class="post-title">%title</span>',
            'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous', 'mylbo' ) . '</span> ' .
            '<span class="screen-reader-text">' . __( 'Previous post:', 'mylbo' ) . '</span> ' .
            '<span class="post-title">%title</span>',
            ) );
	}
    // End of the loop.
    endwhile;
?>

</article>

Я знаю, что это не верно, и прошу помочь в этом.
if( is_single(1086) ){
get_template_part( 'taxonomy/content-taxonomy/content-taxonomy-Ввв', 'content-taxonomy-Ввв' );
} elseif ( is_single(1074) ){
get_template_part( 'taxonomy/content-taxonomy/content-taxonomy-Ггг', 'content-taxonomy-Ггг' );
}

Смысл такой, что при открытии записи принадлежащей к терме "Ввв" или её дочки должна открываться страница с шаблоном:
get_template_part( 'taxonomy/content-taxonomy/content-taxonomy-Ввв', 'content-taxonomy-Ввв' );

Если запись принадлежит к терме "Ггг" или её дочки должна открываться страница с шаблоном:
get_template_part( 'taxonomy/content-taxonomy/content-taxonomy-Ггг', 'content-taxonomy-Ггг' );


Уже туплю сильно и прошу помочь, или направить в нужное направление.
  • Вопрос задан
  • 130 просмотров
Решения вопроса 1
wppanda5
@wppanda5 Куратор тега WordPress
WordPress Mедведь
Примерно так
// добавить в functions.php
	/**
	 * Получение термина верхнего уровня
	 *
	 * @param $term_id  int    ID термина
	 * @param $taxonomy string таксономия
	 *
	 * @return array|int|null|WP_Error|WP_Term
	 */
	function wpp_get_term_top_level_id( $term_id, $taxonomy ) {

		$term_parent = 0;

		while ( $term_id ) {
			$term        = get_term( $term_id, $taxonomy );
			$term_id     = $term->parent;
			$term_parent = $term;
		}

		return $term_parent;
	}


использование в вашем случае

// Start the loop.
    while ( have_posts() ) : the_post();
    
$terms = get_the_terms( get_the_ID(), 'БББ' );
if(!empty($terms)) {
$need_term = wpp_get_term_top_level_id( $terms[0]->term_id, 'БББ' );
 get_template_part( 'taxonomy/content-taxonomy/content-taxonomy',  $need_term->slug);
} else {
 echo 'не установлены термины таксономии БББ';
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы