【发布时间】:2015-08-03 16:24:20
【问题描述】:
我在 Wordpress 中设置了两种自定义帖子类型,一种称为产品,另一种称为供应商。这些的蛞蝓是'product',一个是'supplier'。
这两种帖子类型共享一个名为 Suppliers 的自定义分类法,其中 slug 为 'supplier-tax'。然后有很多子项,它们是不同的供应商。
基本上,我想要做的是当您在一个产品的单个帖子页面上时,我也需要为供应商拉一个帖子。我认为最好的方法是在产品页面上从供应商分类中选择合适的供应商。然后使用相同的选定分类从“供应商”帖子类型查询并获取帖子。
我写了这个查询,它从分类中引入了帖子,但是它需要我告诉它要引入哪个分类和哪个 slug 等,而且它不会查询不同的帖子类型,这使它无用,但这是一个开始:
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => 'supplier_1',
),
) );
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
我已尝试调整并包含我在以前来源中喜欢的查询的部分内容,但我似乎无法破解它。这是我的尝试:
<?php
$terms = wp_get_post_terms( $post->ID, 'supplier-tax' );
if($terms){
$supplier_terms = array();
foreach ($terms as $term){
$supplier_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => $supplier_terms, //the taxonomy terms I'd like to dynamically query
'posts_per_page' => '-1'
),
),
'orderby' => 'title',
'order' => 'ASC'
) );
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?>
<?php the_title(); ?>"><?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
}
?>
有没有人对我做错了什么或如何使这项工作有任何想法?
【问题讨论】:
-
在您的最后一个代码中,您将整个 php 代码放在分类学应该在的位置。这是您编写时出现的某种错误,还是真正的问题?
-
不太清楚那里发生了什么,一定是一个错误,编辑它
标签: php wordpress custom-post-type taxonomy