在我的网站上,我有两种不同的帖子类型。其中一个是publication
,其定制类别类型为publication-category
,另一个是service
,其定制类别类型为service-category
。我在发布页面上发布了一些手册,这些手册使用的服务不同。我想要做的是按相同类别类型在“服务”页面中显示这些小册子。就像,如果该小册子是由教育服务部门发布的,则此小册子应显示在教育服务页面上。
我目前正在使用ACF插件,但是每当有新的手册时,我都必须转到服务页面并在其中添加它。今天,我尝试使用以下代码,但它显示了来自不同类别类型而不是相同类别类型的所有手册。
也许你们可以为我提供帮助,让我以一种适合上述要求的方式来安排代码。
<?php
$custom_taxterms = wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'id',
'terms' => $custom_taxterms
)),
'post__not_in' => array( $post->ID ),
);
$related_items = new WP_Query( $args );
if ( $related_items->have_posts() ) :
echo '<ul>';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();
?>
答案 0 :(得分:0)
如果您在服务页面上,那么为什么要在其中使用“出版类别”
wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
似乎必须使用
$custom_taxterms = get_the_terms($post->ID, 'service-category');
$terms = [];
foreach ($custom_taxterms as $term) {
$terms[] = $term->slug
}
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $terms
)),
);
并为两个类别中的两个术语创建相同的子弹。据我了解。很难理解您的架构
答案 1 :(得分:0)
我找到了解决方案,方法是将服务类别分类法更改为与其他帖子类型相同的发布类别,并使用以下代码从https://wordpress.stackexchange.com/questions/139571/display-posts-with-same-taxonomy-term?rq=1
建立关系谢谢大家
<?php
//get the post's venues
$custom_terms = wp_get_post_terms($post->ID, 'publication-category');
if( $custom_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
// loop through venus and build a tax query
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// put all the WP_Query args together
$args = array( 'post_type' => 'publication',
'posts_per_page' => 20,
'tax_query' => $tax_query );
// finally run the query
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="listing-title"><?php the_title(); ?></div>
<div class="listing-image"><a href="<?php the_permalink() ?>" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID(), 'full') ?>')"></a>
</div>
<?php
endwhile;
}
wp_reset_query();
}?>