首页 文章

如何在wordpress中显示自定义帖子分类中列出的类别中的所有类别名称和图像?

提问于
浏览
1

我创建了一个名为Products的自定义帖子类型和一个自定义分类作为Product Categories . 我在分类标准产品类别中列出了许多产品类别名称及其图像 . 我想显示所有产品类别名称及其各自的图像上传(使用类别图像插件上传) . 图像应上传到 <a> 标签 . 我不知道如何显示类别名称及其图像 . 我使用了类型插件来创建自定义帖子类型和分类以及类别图像插件来加载类别图像 . 我想在下面的代码中显示类别名称及其图像组:

<div class="col-md-4 col-sm-4 col-xs-12">
  <a href="pvc_hose.html">
    <div class="service wow slideInLeft">
      <div class="icon"><img src="<?php bloginfo('template_url'); ?>/images/buiding/icon18.png"></div>
      <h4>PVC hose</h4>
    </div>
  </a>
</div>

谁可以帮我这个事 ?

我附上了产品类别的屏幕截图 .

enter image description here

3 回答

  • 0

    如果我理解这是正确的方法,首先你需要在word press admin菜单空白菜单中创建菜单 . 现在转到function.php文件(主题文件)中添加以下代码 .

    你可以从这个功能获得产品cateogorty列表,

    function get_product_terms( $term_id ) {
    
            $html = '';
    
            $args = array( 'hide_empty' => 0, 'parent' => $term_id );
    
            $terms = get_terms('product_cat', $args);
    
            foreach ($terms as $term) {
    
                $html .= '<li';
    
                if( $term_id == 0 ) {
    
                    $html .= ' class="top_li"';
    
                }
    
                $html .= '><a href="'.get_term_link($term->slug, 'product_cat').'">' . $term->name . '</a>';    
    
                if( $list = get_product_terms( $term->term_id )) {
    
                    $html .= '<ul class="second_level">'.$list.'</ul>';
    
                }
    
                $html .= '</li>';
    
            }
    
            return $html;
    
        }
    

    您可以使用此功能将产品类别添加到菜单,

    // Filter wp_nav_menu() to add additional links and other output
    function new_nav_menu_items($items) {
        // Woo function
    
        /*//product_cat
        $terms = get_terms( 'product_cat', $args );
        print_r($terms);*/
        if( $list = get_product_terms( 0 )) {
    
    
    
        $menu1link = '<li class="home"><a href="' . home_url( '/' ) . '">' . __($list) . '</a></li>';
        $homelink = '<li class="home"><a href="' . home_url( '/' ) . '">' . __('Home') . '</a></li>';
        // add the home link to the end of the menu
        $items = $items . $homelink;
        $items = $items .$menu1link;
        }
        return $items;
    
    }
    add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
    
  • 0

    在单个产品页面上显示类别图像

    $terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ( $terms as $term ){
      $category_name = $term->name;
      $category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
      $image = wp_get_attachment_url($category_thumbnail);
      echo '<img class="absolute category-image" src="'.$image.'">';
    }
    
  • 0
    <?php
                     $terms = get_the_terms( $post->ID, 'product-category' );
    
                    foreach ( $terms as $term ){
                   $category_name = $term->name;
                 $category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
                $image = wp_get_attachment_url($category_thumbnail);
                      ?>
                <div class="col-md-4 col-sm-4 col-xs-12">
                    <a href="">
                    <div class="service wow slideInLeft">
                        <div class="icon"><img src="<?php bloginfo('template_url'); ?>/images/buiding/icon18.png"></div>
                        <h4><?php echo $category_name; ?></h4>
                    </div>
                    </a>
                </div>
    
                <?php }   ?>
    

    我添加了这个代码 . 但是没有工作 . 它显示如下错误“警告:在C:\ xampp \ htdocs \ Working-files \ rajab \ wp-content \ themes \ rajab \ page-中为foreach()提供的参数无效模板\ building.php”

相关问题