【问题标题】:Woocommerce list of categories, subcategories, and productsWoocommerce 类别、子类别和产品列表
【发布时间】:2017-06-04 21:59:06
【问题描述】:

我正在为 Wordpress Woocommerce 网站设置模板,我想显示产品类别、子类别和产品的列表以充当动态菜单。我希望它表现得像这样;

category 1
  -subcategory 1
    -product 1
    -product 2
  -subcategory 2
    -product 3 
category 2
  -product 4

我发现下面的代码和我想要的很接近

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

<?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 );
        echo "<ul class='category'><li>".$cat->name;
            $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) {
                    echo "<ul class='subcategory'>";
                        if($sub_cats->$sub_category == 0) {
                            echo "<li>".$sub_category->cat_name;
                            /*echo "<pre>";
                            print_r($sub_category);
                            echo "</pre>";*/

                            $args = array( 'post_type' => 'product','product_cat' => $sub_category->slug);
                            $loop = new WP_Query( $args );
                            echo "<ul class='products'>";
                                while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
                                    <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                                        <?php the_title(); ?>
                                    </a></li>
                                <?php endwhile; ?>
                            </ul>
                            <?php wp_reset_query(); ?>
                        <?php
                        } else {
                            echo "</li></ul></li>";
                        }
                        echo "</ul>";
                    }
                } else {
                    $args = array( 'post_type' => 'product', 'product_cat' => $cat->slug );
                    $loop = new WP_Query( $args );
                    echo "<ul class='products'>";
                        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
                            <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                                <?php the_title(); ?>
                            </a></li>
                        <?php endwhile; ?>
                    </ul></li></ul>
                    <?php wp_reset_query();
                }
            } else {
                echo "</li></ul>";
        }
}
?>

但这并没有正确关闭包含子类别的类别,而是看起来像这样:

category 1
  -subcategory 1
    -product 1
    -product 2
  -subcategory 2
    -product 3 
  category 2
    -product 4

注意类别 2 如何与子类别对齐。

感谢您对此问题的帮助。

【问题讨论】:

  • 不是我想要的。请耐心等待,因为我是编码新手。该链接似乎是一个自定义页面,我希望有一些东西可以替换网站的菜单。除了包含子类别的类别的结束标签问题之外,我在上面发布的代码有效。我怀疑检查类别是否有父级的检查有问题。

标签: php wordpress woocommerce


【解决方案1】:

经过一番大惊小怪,我把代码改成了这个;

<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$order = 'ASC';
$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 = 1;

$args = array(
    'taxonomy' => $taxonomy,
    'orderby' => $orderby,
    'order' => $order,  
    'show_count' => $show_count,
    'pad_counts' => $pad_counts,
    'hierarchical' => $hierarchical,
    'title_li' => $title,
    'hide_empty' => $empty,
    'parent' => 0
);

$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
    $category_id = $cat->term_id;
    $args2 = array('taxonomy' => $taxonomy,'parent' => $category_id,'hierarchical' => $hierarchical, 'orderby' => $orderby, 'order' => $order,'hide_empty' => $empty);
$categories = get_categories( $args2 );
$categories_cnt = count(get_categories( $args2 ));


if ($categories_cnt != 0){
echo "<ul class='category'><li>".$cat->name;
$sub_cats = get_categories( $args2 );
            if($sub_cats) {
echo "<ul>";
                foreach($sub_cats as $sub_category) {
                    echo "<li>".$sub_category->cat_name;

                    $args = array( 'post_type' => 'product','product_cat' => $sub_category->slug, 'orderby' => $orderby, 'order' => $order);
                            $loop = new WP_Query( $args );
                            echo "<ul class='products'>";
                                while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
                                    <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                                        <?php the_title(); ?>
                                    </a></li>
                                <?php endwhile; ?>
                            </ul>
                            <?php wp_reset_query(); ?>

                    <?php echo "</li>";

                    }
echo "</ul></ul>";
}
}
else {
    echo "<ul class='category'><li>".$cat->name;
    $args = array( 'post_type' => 'product', 'product_cat' => $cat->slug, 'orderby' => $orderby, 'order' => $order );
                    $loop = new WP_Query( $args );
                    echo "<ul class='products'>";
                        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
                            <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                                <?php the_title(); ?>
                            </a></li>
                        <?php endwhile; ?>
                    </ul></li></ul>
                    <?php wp_reset_query();
    echo "</li></ul>";

}

}
?>

我确信它很混乱,可能包含冗余代码,但它现在对我有用。

【讨论】:

  • 非常感谢您发布您的解决方案——我已经搜索了几个小时,这正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
相关资源
最近更新 更多