Wordpress 如何在wp_查询中添加分类作为参数
这是显示产品列表的程序的一部分。我将产品创建为自定义帖子,需要根据分类法显示帖子 谁可以在选择后参数中提供分类作为参数 我使用工具集插件创建了分类法和帖子Wordpress 如何在wp_查询中添加分类作为参数,wordpress,post,Wordpress,Post,这是显示产品列表的程序的一部分。我将产品创建为自定义帖子,需要根据分类法显示帖子 谁可以在选择后参数中提供分类作为参数 我使用工具集插件创建了分类法和帖子 <?php $taxonomies = get_terms([ 'taxonomy' => 'product_category', 'parent' => 0, 'orderby' => 'name', 'order' => get_query_var('Order', 'A
<?php $taxonomies = get_terms([
'taxonomy' => 'product_category',
'parent' => 0,
'orderby' => 'name',
'order' => get_query_var('Order', 'ASC'),
'hide_empty' => false,
]);
?><?php
$i=1;
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {; ?>
<?php
if(!empty(do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]'))){
$pro_cat_img = do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]');
}else{
$pro_cat_img = get_template_directory_uri()."/img/product-guitar.png";
} ?>
<h1>
<?php echo $taxonomy->name;
//$ir=$taxonomy->name;
?>
</h1>
<ul>
<?php
// args
$args = array(
'posts_per_page' => 5,
'post_type' => 'product',
'post_status' => 'publish',
//how can i add taxonomies asan argument here
);
// query
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if(has_post_thumbnail(get_the_ID())) {
$feature_image_full = simplexml_load_string(get_the_post_thumbnail(get_the_ID(), 'banner_img'));
$pr_img_source = $feature_image_full->attributes()->src;
?>
<li>
<span>
<?php echo the_title(); ?>
</span>
</li>
<?php } endwhile; endif; wp_reset_query(); ?>
</ul>
attributes()->src;
?>
这是工作代码
最好知道您查看了哪些资源,具体尝试了哪些资源,以及分类法的名称(键,而不是标签)。你看过这个吗:或者读过这个:?如果是,您是否尝试添加参数?您尝试添加了哪些参数?当你添加它们的时候发生了什么?谢谢你,它起了作用,这真的很重要,我也在添加结果,以供将来其他人参考
<?php $taxonomies = get_terms([
'taxonomy' => 'product_category',
'parent' => 0,
'orderby' => 'name',
'order' => get_query_var('Order', 'ASC'),
'hide_empty' => false,
]);
?><?php
$i=1;
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {; ?>
<?php
if(!empty(do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]'))){
$pro_cat_img = do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]');
}else{
$pro_cat_img = get_template_directory_uri()."/img/product-guitar.png";
} ?>
<div class="product-row clearfix">
<div class="bg-box">
<?php if($i==1){?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-guitar"/>
<?php }else if($i==2){ ?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-speaker"/>
<?php }else if($i==3){ ?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-headphone"/>
<?php } ?>
<a href="<?php echo esc_url( add_query_arg( array('cat'=>$taxonomy->term_id), home_url('/products') ) ); ?>" class="view-more-btn button button--moema ">
READ MORE
</a>
</div>
<div class="items-list-box">
<h1>
<?php echo $taxonomy->name;
$ir=$taxonomy->name;
?>
</h1>
<ul>
<?php
// args
$args = array(
'posts_per_page' => 5,
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'product_category',
'field' => 'name',
'terms' => $ir,
)
),
);
// query
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if(has_post_thumbnail(get_the_ID())) {
$feature_image_full = simplexml_load_string(get_the_post_thumbnail(get_the_ID(), 'banner_img'));
$pr_img_source = $feature_image_full->attributes()->src;
?>
<li>
<span>
<?php echo the_title(); ?>
</span>
</li>
<!-- <img src="<?php //echo esc_url($pr_img_source); ?>" alt="<?php the_title(); ?>">
-->
<?php } endwhile; endif; wp_reset_query(); ?>
</ul>