Php 如何从当前WooCommerce产品类别中获取所有产品?
自定义我的Php 如何从当前WooCommerce产品类别中获取所有产品?,php,wordpress,loops,woocommerce,taxonomy-terms,Php,Wordpress,Loops,Woocommerce,Taxonomy Terms,自定义我的存档产品.php。如何在自定义循环中仅显示特定类别内的产品,没有解决我的问题。我试图single\u cat\u title()根据问题获取当前类别,但出现错误。基于此,我想我需要使用get\u queryed\u object(),但我不断地遇到错误 我试过这个: <?php $category = single_cat_title('', false); // this returns your current category ?> <?php // Set
存档产品.php
。如何在自定义循环中仅显示特定类别内的产品,没有解决我的问题。我试图single\u cat\u title()
根据问题获取当前类别,但出现错误。基于此,我想我需要使用get\u queryed\u object()
,但我不断地遇到错误
我试过这个:
<?php
$category = single_cat_title('', false); // this returns your current category ?>
<?php
// Setup your custom query
$args = array(
'post_type' => 'product',
'product_cat' => $category,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_post_thumbnail(); ?>
<br>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
已更新 在“产品类别存档”页面上,要获取您将使用的当前产品类别术语,请执行以下操作:
- (以获取WP_术语项目)
- 或者(获取术语Id)
<?php
// Get The queried object ( a WP_Term or a WP_Post Object)
$term = get_queried_object();
// To be sure that is a WP_Term Object to avoid errors
if( is_a($term, 'WP_Term') ) :
// Setup your custom query
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'product_cat', // The taxonomy name
'field' => 'term_id', // Type of field ('term_id', 'slug', 'name' or 'term_taxonomy_id')
'terms' => $term->term_id, // can be an integer, a string or an array
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div style="margin:8px; text-align:center;">
<a href="'.get_the_permalink().'">';
the_post_thumbnail();
the_title();
echo '</a></div>';
endwhile;
wp_reset_postdata(); // Remember to reset
endif; endif;
?>
测试和工作
文档:谢谢。我添加了你的代码,但是我现在得到了“你的网站上有一个严重错误”?是否可以获得
发布的缩略图(“缩略图”)代码>仅包装在\u permalink中?我无法使用echo`''将缩略图缩小为“缩略图”@newbieedshelp只需移动echo'代码>在标题()之前代码>我已经试过了,但它不会将图像显示为“缩略图”,而是图像变得超大。这仅在使用permalink包装时发生。这就是我尝试的:echo“”你试过了吗:;_title();回声'代码>
<?php
// Get The queried object ( a WP_Term or a WP_Post Object)
$term = get_queried_object();
// To be sure that is a WP_Term Object to avoid errors
if( is_a($term, 'WP_Term') ) :
// Setup your custom query
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'product_cat', // The taxonomy name
'field' => 'term_id', // Type of field ('term_id', 'slug', 'name' or 'term_taxonomy_id')
'terms' => $term->term_id, // can be an integer, a string or an array
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div style="margin:8px; text-align:center;">
<a href="'.get_the_permalink().'">';
the_post_thumbnail();
the_title();
echo '</a></div>';
endwhile;
wp_reset_postdata(); // Remember to reset
endif; endif;
?>