自定义文章类型分组循环中无法展示Yoast主分类(自定义分类法'party')的问题求助
自定义文章类型分组循环中无法展示Yoast主分类(自定义分类法'party')的问题求助
各位大佬好,我遇到一个头疼的问题:我在页面上做了两个循环,都基于同一个FacetWP查询,一个是普通循环,另一个是按自定义字段pm_century分组的foreach循环。现在我想在循环里显示Yoast设置的主分类(自定义分类法party),普通循环里显示完全正常,但分组循环里就是啥都不出来,试了好几种写法都没用,求帮忙看看哪里出问题了!
共用的FacetWP查询代码
这个查询是针对自定义文章类型prime-ministers的,配置如下:
<?php return [ "post_type" => [ "prime-ministers" ], "post_status" => [ "publish" ], "meta_query" => [ "sort_0" => [ "key" => "pm_century", "type" => "CHAR" ], "sort_1" => [ "key" => "serving_period_1_start", "type" => "CHAR" ] ], "orderby" => [ "sort_0" => "DESC", "sort_1" => "DESC" ], "posts_per_page" => -1 ]; ?>
正常工作的普通循环(能正确显示Yoast主分类)
这段代码放在普通循环里,能正常输出party分类的Yoast主分类名称:
<?php $post_type = get_post_type(); $id = get_the_ID(); $taxonomy = current( get_object_taxonomies( $post_type, 'objects' ) )->name ?? false; $primary_term_id = function_exists( 'tsf' ) ? tsf()->get_primary_term( $id, $taxonomy ) : 0; $primary_term_id = $primary_term_id ?: ( get_the_terms( $id, $taxonomy )[0]->term_id ?? null ); if ( $primary_term_id ) { $term = get_term( $primary_term_id, $taxonomy ); ?> <p class="main-listing-party"><?php echo $term->name ?? '';?> </p> <?php } ?>
无法显示主分类的分组循环
我先把文章按pm_century字段分组,然后在嵌套的foreach里输出内容,但这里的主分类就是不显示:
<?php $century_posts = array(); while ($query->have_posts()) { $query->the_post(); $century = get_field('pm_century', get_the_id()); $century_posts[$century][] = $post; } wp_reset_query(); foreach ($century_posts as $century_post => $century_title) { ?> <!-- 显示世纪分组标题 --> <div class="century-line-wrap"> <h2 class="century-name"><?php echo esc_html($century_post); ?></h2> <div class="century-line"></div> </div> <?php foreach ($century_title as $listing => $single_listing) { setup_postdata($single_listing); $post_id = $single_listing->ID; $title = get_the_title($post_id); $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'large' ); ?> <?php $nameclass = str_replace(" ", "", $title); ?> <div class="single-prime-minister <?php echo strip_tags(get_the_term_list( $post_id, 'party', ' ',', ')); ?> <?php echo $nameclass ?>"> <a href="<?php the_permalink($post_id); ?>" title="<?php echo $title ?>" aria-label="Find out more about <?php echo $title ?>"> <div class="pm-filter-profile-image"> <img src="<?php echo $image[0]; ?>" alt="" /> </div> </a> <div class="pm-filter-details-wrap"> <h4><?php echo get_the_title(); ?></h4> <!-- 这里就是无法显示主分类的代码,和普通循环里的写法一样但没用 --> <?php $post_type = get_post_type(); $id = get_the_ID(); $taxonomy = current( get_object_taxonomies( $post_type, 'objects' ) )->name ?? false; $primary_term_id = function_exists( 'tsf' ) ? tsf()->get_primary_term( $id, $taxonomy ) : 0; $primary_term_id = $primary_term_id ?: ( get_the_terms( $id, $taxonomy )[0]->term_id ?? null ); if ( $primary_term_id ) { $term = get_term( $primary_term_id, $taxonomy ); ?> <p class="main-listing-party"><?php echo $term->name ?? '';?> </p> <?php } ?> </div> </div> <?php } wp_reset_postdata(); } ?>
我试过的其他方法,但都没用
- 直接用Yoast的函数获取主分类ID,但输出为空:
<?php $primary_term_id = yoast_get_primary_term_id( $post_id, 'party' ); ?> <p><?php echo $primary_term_id ?></p>
- 把普通循环里的代码改成用
$post_id代替get_the_ID(),还是没效果:
<?php $post_type = get_post_type(); $taxonomy = current( get_object_taxonomies( $post_type, 'objects' ) )->name ?? false; $primary_term_id = function_exists( 'tsf' ) ? tsf()->get_primary_term( $post_id, $taxonomy ) : 0; $primary_term_id = $primary_term_id ?: ( get_the_terms( $post_id, $taxonomy )[0]->term_id ?? null ); if ( $primary_term_id ) { $term = get_term( $primary_term_id, $taxonomy ); ?> <p class="main-listing-party"><?php echo $term->name ?? '';?> </p> <?php } ?>
实在搞不懂为啥分组循环里就是获取不到主分类,有没有大佬能指点一下?
备注:内容来源于stack exchange,提问作者Mr Toad
相关产品推荐
相关产品推荐

