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

piztneat  于 2022-11-22  发布在  WordPress
关注(0)|答案(4)|浏览(163)

我创建了一个名为“产品”的自定义帖子类型和一个名为“产品类别”的自定义分类。我在分类“产品类别”中列出了许多产品类别名称及其图像。我希望显示所有产品类别名称及其上载的相应图像(使用类别图片插件上传)。图片应该上传到一个<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>

有人能帮我吗?
我已经附上了产品类别的截图。

3df52oht

3df52oht1#

如果我理解正确这是正确的方式,首先你需要在Word中创建菜单按管理菜单空白菜单.现在去function.php文件(主题文件)添加以下代码.
您可以通过此功能获取产品类别列表,

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' );
amrnrhlw

amrnrhlw2#

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

$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.'">';
}
a0zr77ik

a0zr77ik3#

<?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()提供的参数无效”

plicqrtu

plicqrtu4#

很老的帖子,但如果有人仍然在寻找答案,我这样做:

<?php
                        
    $orderby = 'name';
    $order = 'asc';
    $hide_empty = false;
    $cat_args = array(
          'orderby' => $orderby,
          'order' => $order,
           'hide_empty' => $hide_empty,
    );
    $product_categories = get_terms('projects-category', $cat_args); //your custom category taxonomy
    //var_dump($product_categories); //do var_dump in order to get the '_category_options', that I have
    if (!empty($product_categories)) {
       foreach ($product_categories as $key => $category) {
            $term_image = get_term_meta($category->term_id, '_category_options', true);
            $url = wp_get_attachment_image_src($term_image['category_image'], array(678, 420) ); //custom display image dimensions, according to your needs
?>
<img src="<?php echo $url[0]; ?> width="<?php echo $url[1]; ?>" height="<?php echo $url[2]; ?>" />
<?php
             } //end foreach loop
       } //end if
?>

相关问题