Php 如何从不同的职位类型按类别显示相关职位
在我的网站上,我有两种不同的帖子类型。其中一个是Php 如何从不同的职位类型按类别显示相关职位,php,wordpress,Php,Wordpress,在我的网站上,我有两种不同的帖子类型。其中一个是出版物,自定义类别类型为出版物类别,另一个是服务,自定义类别类型为服务类别。我在出版物页面上发布了一些小册子,这些小册子属于不同的服务。我想做的是在服务页面中按相同的类别类型显示这些小册子。这就像,如果宣传册是由教育服务部门发布的,那么该宣传册应该显示在教育服务页面中 我目前正在使用ACF插件来实现这一点,但每当有新的宣传册时,我都必须转到服务页面并将其添加到其中。今天我尝试了下面的代码,但它显示了来自不同类别类型而不是相同类别类型的所有手册 也许
出版物
,自定义类别类型为出版物类别
,另一个是服务
,自定义类别类型为服务类别
。我在出版物页面上发布了一些小册子,这些小册子属于不同的服务。我想做的是在服务页面中按相同的类别类型显示这些小册子。这就像,如果宣传册是由教育服务部门发布的,那么该宣传册应该显示在教育服务页面中
我目前正在使用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();
?>
如果您在“服务”页面上,那么为什么要在中使用“出版物类别”
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
)),
);
并为两个类别中的两个术语创建相同的slug。据我了解。很难理解您的体系结构我通过将服务类别分类法更改为与其他帖子类型相同的发布类别,并从以下代码中创建关系,找到了解决方案: 谢谢大家
<?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();
}?>
我没有遵守你的要求。您是说您将在出版物类别
和服务类别
中都有一个“教育服务”术语。我也不明白你们用这些句子问什么:“我在出版物页面上发布了一些属于不同服务的小册子。”和“我试图做的是在服务页面上按相同的类别类型显示这些小册子。”以及你们试图创建的主题模板页面single service.php
?@MikeSchinkel,我已经为服务single services.php
和出版物single publication.php
页面创建了主题模板页面。此外,我还分别为这两个模板页面注册了分类法。在“服务”页面下,我们提供教育、招聘和咨询服务,并在“出版物”页面共享手册。当我们共享任何发布时,我们使用分类类型在发布中声明服务名称。所以,我想在这两个模板页面之间创建关系,以便按类别类型在各个服务页面中显示出版物。我希望这是清楚的。那么你将它们与ACF字段关系链接了吗?@Stender,是的,我目前正在使用ACF链接它们,但每次共享新出版物时,我都必须转到特定的服务页面,并将该出版物添加到ACF自定义字段中。所以我想通过创建类别类型之间的关系来实现自动化。我意识到你需要的是一个了解ACF的人,而我不知道。所以在这种情况下我无能为力。