获取woocommerce子类别产品

时间:2014-05-09 09:45:32

标签: php wordpress woocommerce categories

我正在尝试将产品的woocommerce子类别显示在主要类别下。

<ul class="wsubcategs">
<?php
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $category->term_id,
'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li><a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a>

</li>
<?php
endforeach;
?>

</ul>

到目前为止,这已经让我在正确的主要类别下获得了相应的子类别,但是没有产品显示在它们之下。 任何建议将不胜感激。

0

2 个答案:

答案 0 :(得分:7)

在Wordpress中有很多方法可以做到这一点。您可以使用WP_Query对象执行自定义查询以获取该类别中的所有产品,这将是最灵活的选项,但有一种更简单的方法。

Woocommerce提供专门用于显示特定类别产品的短代码。输出将使用已经内置到Woocommerce中的模板。

<?php echo do_shortcode('[product_category category="appliances"]');?>

这将为您提供特定类别的产品。

在您的代码中,您可能会执行以下操作:

<ul class="wsubcategs">
<?php
$wsubargs = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' => $category->term_id,
    'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
    <li>
        <a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a>
        <div class="products">
            <?php echo do_shortcode('[product_category category="'.$wsc->slug.'"]');?>
        </div>
    </li>
<?php endforeach;?>
</ul>

我在你的问题中注意到你正在输出一个列表。在我看来,您可能不想在每个类别下面实际输出产品(详细模板),但您可能更愿意在子列表中显示产品或产品标题的数量。

以下是您展示产品数量的方法:

计数;?&GT;

你可以在上面的foreach循环中的任何地方使用它。

以下是如何在每个类别的列表元素中显示产品标题的子列表:

<?php $subcategory_products = new WP_Query( array( 'post_type' => 'product', 'product_cat' => $wsc->slug ) );
    if($subcategory_products->have_posts()):?>
    <ul class="subcat-products">
        <?php while ( $subcategory_products->have_posts() ) : $subcategory_products->the_post(); ?>
        <li>
            <a href="<?php echo get_permalink( $subcategory_products->post->ID ) ?>">
                <?php the_title(); ?>
            </a>
        </li>
        <?php endwhile;?>
    </ul>
<?php endif; wp_reset_query(); // Remember to reset ?>

以上代码中的内容如下:

<ul class="wsubcategs">
<?php
$wsubargs = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' => $category->term_id,
    'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
    <li>
        <a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a>
        <?php $subcategory_products = new WP_Query( array( 'post_type' => 'product', 'product_cat' => $wsc->slug ) );
            if($subcategory_products->have_posts()):?>
        <ul class="subcat-products">
            <?php while ( $subcategory_products->have_posts() ) : $subcategory_products->the_post(); ?>
            <li>
                <a href="<?php echo get_permalink( $subcategory_products->post->ID ) ?>">
                    <?php the_title(); ?>
                </a>
            </li>
            <?php endwhile;?>
        </ul>
    <?php endif; wp_reset_query(); // Remember to reset ?>
    </li>
<?php endforeach;?>
</ul>

答案 1 :(得分:0)

**Show Category and Sub Category in search Box**


----------


<?php
//template name:house

?>
<style>
.abc {
    margin-left: 10px;
}
</style>

<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<select>
<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{

if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );

$abc=$cat->name;
if($abc=="") {
?>
<option value="<?php echo $cat->slug;?>"><?php echo $abc; ?></option>

<?php }
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty

);

$sub_cats = get_categories( $args2 );
if($sub_cats)
{

foreach($sub_cats as $sub_category)
{
?>

<?php
if($sub_cats->$sub_category == 0)
{
$suv= $sub_category->cat_name;?>
<option class="abc" value="<?php echo $sub_category->slug; ?>"><span class="abc"><?php echo $suv; ?></span></option>




<?php } } } } } ?>

</select>