Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从当前WooCommerce产品类别中获取所有产品?_Php_Wordpress_Loops_Woocommerce_Taxonomy Terms - Fatal编程技术网

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)
自WordPress 3.1以来,不推荐在WP_查询中直接使用分类法参数。相反,您将使用税务查询,如下所示:

<?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;
?>