【问题标题】:categories and sub categories wordpress类别和子类别 wordpress
【发布时间】:2016-04-01 08:51:18
【问题描述】:

我为产品创建了一些自定义类别,它有子类别,子类别还有更多的子类别。现在首先我显示主要类别。如果我在那里显示子类别,则显示与该类别及其子类别相关的所有子类别。我想逐步显示它们。也就是说,如果用户单击主类别,则它会转到其子类别页面。如果用户单击其子类别之一,则应转到子类别,如果没有子类别则显示产品。代码是这样的

 $products = get_term_children($term_id[0], 'product-cat');
if(count($products) > 0){
    $count = 0;
    $sorted_products = array();

    foreach ($products as $product) {

        $sorted_products = get_term($product, 'product-cat');
        $prod_meta = get_option("taxonomy_term_".$term->term_id);
    //echo "<pre>"; print_r($sorted_products);

    foreach ($sorted_products as $product) { ?>
            <div class="col-md-3 col-sm-4 col-xs-12">
                    <a href="<?php echo $product['link']; ?>">
                        <a href="<?php echo $product['link']; ?>" class="hvr-grow">
                            <img class="center-block img-responsive" src="<?php echo $product['img'] ? $product['img'] : '/wp-content/themes/ruskin/images/dummy-product.jpg'; ?>"  alt="<?php echo $product['name']; ?>">
                    <h3><a href="<?php echo $product['link']; ?>"><?php echo $product['name']; ?></a></h3>
else{

    # Define the WP Query post arguments.
    $args = array(
    'post_status' => 'publish',
    'post_type' => 'products',
    'posts_per_page' => -1,
    //'meta_query' => array('relation' => 'AND', array('key' => '_cus__featured', 'value' => '1', 'compare' => '='),),
    'meta_key' => '_cus__sort_order',
    //'meta_value'    => 'meta_value',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'tax_query' => array(
        array('taxonomy' => 'product-cat',
            'field' => 'slug',
            'terms' => $cats
        )));
$loop = new WP_Query($args);
$total = $loop->found_posts;
$sliders='';
// Generatet the slider conteents
while ($loop->have_posts()) {
    $loop->the_post();
    $listingimg = get_post_custom_values('_cus__listing_img');
        $listingimg = "/wp-content/themes/bodyo/images/no-slider-img.jpg";

    $img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'main_slide_img');
        $img = "/wp-content/themes/bodyo/images/no-slider-img.jpg";
    $sliders .= '<a href="'. get_the_permalink() .'" class="hvr-grow">';
    $sliders .= '<img src="'.$listingimg.'" class="center-block img-responsive" alt="'. get_the_title() .'" />';
    $sliders .= '</a>';
    $sliders .= '</div>';
    $sliders .= '<a href="'. get_the_permalink() .'">';
    $sliders .= '<h3>'. get_the_title() .'</h3>';
    $sliders .= '<p>'. get_the_excerpt() .'</p>';
    $sliders .= '<a href="'. get_the_permalink() .'">read more</a>';
    $counter++;

}

它会覆盖之前的排序顺序。也就是说,如果我们从仪表板按排序顺序给出 2 到三个类别,那么它只显示最后一个。前两个被覆盖。

【问题讨论】:

    标签: php wordpress sorting taxonomy custom-taxonomy


    【解决方案1】:

    应该这样做。

    $args = array(
        'child_of' => $term_id[0],
        'taxonomy' => 'product-cat',
        'hierarchical' => true,
        'depth'  => 1,
    );
    $categories = get_categories($args);
    

    请记住,get_categories()get_terms() 的包装器。

    您可以找到所有接受的值here,但您要寻找的是深度。

    【讨论】:

    • 你可以在哪一步详细说明一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2014-07-02
    • 2014-06-15
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多