2016-01-29 53 views
1

我已经创建了一个名为Products的自定义发布类型和一个作为Product Categories的自定义分类。我已经在产品分类分类中列出了一些产品类别名称及其图像。我想显示所有产品类别名称及其上传的各自图片(使用类别图片插件上传)。图片应该上传到<a>标签。我不知道如何显示类别名称及其图像。我使用类型插件来创建自定义文章类型和分类和类别图像插件来加载类别图像。我想,以显示下面的代码组类别名称和他们的形象:如何在wordpress中显示自定义帖子分类中列出的所有类别名称和图像?

<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

回答

0

如果我理解正确,这是正确的做法, 首先,你需要在Word中创建按管理菜单空白菜单菜单。现在去函数.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

第二功能为产品类别的菜单写入.' $ list'具有您想要的所有类别 –

+0

我已经为它创建了自定义帖子类型”产品“和分类为”产品类别“使用类型插件。然后,我使用类别图像插件在分类中添加了“产品类别”和类别图像中的类别名称组。我知道如何在wordpress中显示其图像和类别名称? – anumol

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

不工作..显示错误。 – anumol

+0

你能告诉我 –

+0

上面显示的代码.. – anumol

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 } ?> 

我添加了这个代码,但不工作。它显示这样的错误“警告:为foreach()提供的无效参数在C:\ xampp \ htdocs \ Working-files \ rajab \ wp-content \ themes \ rajab \ page -templates \ building.php“

相关问题