在循环WordPress中显示产品类别静态滑块名称和图像

时间:2020-11-27 作者:Mehrdad Hosseini

我尝试在我的主页中为woocommerce转换一个类别静态滑块,但我不知道使用哪个循环或函数来实现这一点,下面是我的代码:

<div class="row mt-3 mb-5">
                <div class="col-12">
                    <div class="category-section dt-sn dt-sl">
                        <div class="category-section-title dt-sl">
                            <h3>More Than 3000 products</h3>
                        </div>
                        <div class="category-section-slider dt-sl">
                            <div class="category-slider owl-carousel">
                                <div class="item">
                                    <a href="#" class="promotion-category">
                                        <img src="./assets/img/category/notebook-computer.png" alt="">
                                        <h4 class="promotion-category-name">Electronics</h4>
                                        <h6 class="promotion-category-quantity">150</h6>
                                    </a>
                                </div>
                                <div class="item">
                                    <a href="#" class="promotion-category">
                                        <img src="./assets/img/category/lifeline-in-a-heart-outline.png" alt="">
                                        <h4 class="promotion-category-name">Beauty</h4>
                                        <h6 class="promotion-category-quantity">620</h6>
                                    </a>
                                </div>
                                <div class="item">
                                    <a href="#" class="promotion-category">
                                        <img src="./assets/img/category/repair-tools.png" alt="">
                                        <h4 class="promotion-category-name">Tools</h4>
                                        <h6 class="promotion-category-quantity">310</h6>
                                    </a>
                                </div>
                                <div class="item">
                                    <a href="#" class="promotion-category">
                                        <img src="./assets/img/category/hanbok.png" alt="">
                                        <h4 class="promotion-category-name">Clothes</h4>
                                        <h6 class="promotion-category-quantity">1000</h6>
                                    </a>
                                </div>
                                <div class="item">
                                    <a href="#" class="promotion-category">
                                        <img src="./assets/img/category/sofa.png" alt="">
                                        <h4 class="promotion-category-name">Home Appliance </h4>
                                        <h6 class="promotion-category-quantity">615</h6>
                                    </a>
                                </div>
                                <div class="item">
                                    <a href="#" class="promotion-category">
                                        <img src="./assets/img/category/school-material.png" alt="">
                                        <h4 class="promotion-category-name">Art</h4>
                                        <h6 class="promotion-category-quantity">60</h6>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

1 个回复
最合适的回答,由SO网友:tiago calado 整理而成

这是为了显示每个产品类别的名称和图像,并带有指向类别woocommerce页面的链接。要以动态方式自动获取它,可以使用函数get\\u terms()。这只显示关联了产品的类别,要显示所有类别,甚至是空类别,我们需要在这个$cat\\u args变量数组中将“hide\\u empty”更改为false。

$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
echo \'<pre>\';
print_r($cats);
有了这个,我们可以循环显示预期的信息。

$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
    echo $cat->name;
endforeach;
例如,显示;“第一代”;类别我们比较父项等于零。

$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
  if ($cat->parent == 0):
    echo $cat->name;
  endif;
endforeach;
如果我们有分类图像,我们可以这样显示它们。

$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
  if ($cat->parent == 0):
    $image = wp_get_attachment_url(get_term_meta($cat->term_id, \'thumbnail_id\', true));
    if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
    echo $cat->name;
  endif;
endforeach;
现在让我们假设类别有子类别(“第二代”),我们可以在foreach中创建foreach循环。

$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
  if ($cat->parent == 0):
    $image = wp_get_attachment_url(get_term_meta($cat->term_id, \'thumbnail_id\', true));
    if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
    echo $cat->name;
    foreach ($cats as $key => $cat2):
      if ($cat2->parent == $cat->term_id):
        $image = wp_get_attachment_url(get_term_meta($cat2->term_id, \'thumbnail_id\', true));
        if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
        echo $cat2->name;
      endif;
    endforeach;
  endif;
endforeach;
也可以有更多的子类别,比如“a”;“第三代”;,只需在第二个foreach循环中编写第三个foreach循环。

$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
  if ($cat->parent == 0):
    $image = wp_get_attachment_url(get_term_meta($cat->term_id, \'thumbnail_id\', true));
    if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
    echo $cat->name;
    foreach ($cats as $key => $cat2):
      if ($cat2->parent == $cat->term_id):
        $image = wp_get_attachment_url(get_term_meta($cat2->term_id, \'thumbnail_id\', true));
        if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
        echo $cat2->name;
        foreach ($cats as $key => $cat3):
          if ($cat3->parent == $cat2->term_id):
            $image = wp_get_attachment_url(get_term_meta($cat3->term_id, \'thumbnail_id\', true));
            if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
            echo $cat3->name;
          endif;
        endforeach;
      endif;
    endforeach;
  endif;
endforeach;
要将链接添加到类别页面,可以使用函数get\\u term\\u link()。

<a href="<?php echo get_term_link($cat); ?>"> <?php echo $cat->name ?></a>
如果有更多的子类别的子类别,这可以继续下去,但从用户的角度来看,我相信超过三个“;“世代”;就类别而言,这已经足够好了,比这更多的内容可能开始变得非常混乱。

在这个动态的产品类别菜单中或我们展示的任何东西中,例如在主页中,如果我们添加或删除一个类别,它们将显示或不显示原样。

这就是我使用的函数,现在我们只需要在上面添加html和css甚至javascript!

这里只是它的一个小结构。

<?php $cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args ); ?>
<div class="menu-body"> <?php
  foreach ($cats as $key => $cat):
    if ($cat->parent == 0): ?>
      <div class="menu-cat"> <?php
        $image = wp_get_attachment_url(get_term_meta($cat->term_id, \'thumbnail_id\', true));
        if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif; ?>
        <a href="<?php echo get_term_link($cat); ?>"> <?php echo $cat->name ?></a> <?php
        foreach ($cats as $key => $cat2):
          if ($cat2->parent == $cat->term_id): ?>
            <div class="menu-subcat"> <?php
              $image = wp_get_attachment_url(get_term_meta($cat2->term_id, \'thumbnail_id\', true));
              if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif; ?>
              <a href="<?php echo get_term_link($cat2); ?>"> <?php echo $cat2->name ?></a> <?php
              foreach ($cats as $key => $cat3):
                if ($cat3->parent == $cat2->term_id): ?>
                  <div class="menu-sub-subcat"> <?php
                    $image = wp_get_attachment_url(get_term_meta($cat3->term_id, \'thumbnail_id\', true));
                    if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;  ?>
                    <a href="<?php echo get_term_link($cat); ?>"> <?php echo $cat->name ?></a>
                  </div> <?php
                endif;
              endforeach; ?>
            </div> <?php
          endif;
        endforeach; ?>
      </div> <?php
    endif;
  endforeach; ?>
</div>
<style media="screen">
  .menu-body{
    background:gray;
    margin:10px;
    border-radius:10px;
  }
  .menu-body img{
    width:30px;
    height:30px;
  }
  .menu-cat{
    margin-left:15px;
    color:yellow;
  }
  .menu-subcat{
    margin-left:30px;
    color:lightgreen;
  }
  .menu-sub-subcat{
    margin-left:30px;
    color:lightblue;
  }
</style>

相关推荐

Sidebar slideshow widget

有人知道可以放在边栏上的高质量图像幻灯片小部件吗?