在Woo商务中显示类别缩略图和链接

时间:2015-06-02 作者:steamfunk

我想在woocommerce中用缩略图显示我的类别。我可以列出子术语作为链接,但我想添加其他内容。我已经添加了下面的代码,我用它在模板主页上显示“product\\u cat”的子术语作为链接。php。但我也想添加类别图像。我真的很感激你的帮助,谢谢。

<?php

$taxonomyName = "product_cat";
//This gets top layer terms only.  This is done by setting parent to 0.  
$parent_terms = get_terms($taxonomyName, array(\'parent\' => 0, \'orderby\' => \'slug\', \'hide_empty\' => false));   
echo \'<ul>\';
foreach ($parent_terms as $pterm) {
    //Get the Child terms
    $terms = get_terms($taxonomyName, array(\'parent\' => $pterm->term_id, \'orderby\' => \'slug\', \'hide_empty\' => false));
    foreach ($terms as $term) {

        echo \'<li><a href="\' . get_term_link( $term->name, $taxonomyName ) . \'">\' . $term->name . \'</a></li>\';  
    }
}
echo \'</ul>\';


?>

3 个回复
最合适的回答,由SO网友:Domain 整理而成

做了一些定制。这将帮助您显示父类别和子类别图像。以后可以根据需要自定义此代码。

    $taxonomyName = "product_cat";
//This gets top layer terms only.  This is done by setting parent to 0.  
    $parent_terms = get_terms($taxonomyName, array(\'parent\' => 0, \'orderby\' => \'slug\', \'hide_empty\' => false));

    echo \'<ul>\';
    foreach ($parent_terms as $pterm) {

        //show parent categories
        echo \'<li><a href="\' . get_term_link($pterm->name, $taxonomyName) . \'">\' . $pterm->name . \'</a></li>\';

        $thumbnail_id = get_woocommerce_term_meta($pterm->term_id, \'thumbnail_id\', true);
        // get the image URL for parent category
        $image = wp_get_attachment_url($thumbnail_id);
        // print the IMG HTML for parent category
        echo "<img src=\'{$image}\' alt=\'\' width=\'400\' height=\'400\' />";

        //Get the Child terms
        $terms = get_terms($taxonomyName, array(\'parent\' => $pterm->term_id, \'orderby\' => \'slug\', \'hide_empty\' => false));
        foreach ($terms as $term) {

            echo \'<li><a href="\' . get_term_link($term->name, $taxonomyName) . \'">\' . $term->name . \'</a></li>\';
            $thumbnail_id = get_woocommerce_term_meta($pterm->term_id, \'thumbnail_id\', true);
            // get the image URL for child category
            $image = wp_get_attachment_url($thumbnail_id);
            // print the IMG HTML for child category
            echo "<img src=\'{$image}\' alt=\'\' width=\'400\' height=\'400\' />";
        }
    }
    echo \'</ul>\';
如果它符合你的要求,请告诉我。

SO网友:steamfunk

您好@Wisdmlabs感谢您的帮助。我发现这很有效,以防其他人想知道如何做到这一点。

$taxonomyName = "product_cat";
$prod_categories = get_terms($taxonomyName, array(
    \'orderby\'=> \'name\',
    \'order\' => \'ASC\',
    \'hide_empty\' => 1
));  

foreach( $prod_categories as $prod_cat ) :
    if ( $prod_cat->parent != 0 )
        continue;
    $cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, \'thumbnail_id\', true );
    $cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
    $term_link = get_term_link( $prod_cat, \'product_cat\' );
    ?>

    <img  src="<?php echo $cat_thumb_url; ?>" alt="" /> 
    <a class="button" href="<?php echo $term_link; ?>"> <?php echo $prod_cat->name; ?> </a> 
    <?php endforeach; 
wp_reset_query(); ?>

SO网友:jgangso

要进一步优化上述@Wisdmlabs的答案,请替换此行

$cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
使用

$cat_thumb_url = wp_get_attachment_image_src( $cat_thumb_id, \'thumbnail-size\' )[0]; // Change to desired \'thumbnail-size\'
这样,图像在服务器上被裁剪到合适的大小,以减少带宽负载。

结束

相关推荐

WP_SET_OBJECT_TERMS()不会替换Term,而是创建新的Term

如果我使用以下硬代码,请从自定义子菜单页:$post_id = 111; $flightCategory = array( 25 ); wp_set_object_terms( $post_id, $flightCategory, \'flight_categories\' ); 并刷新页面,它只需将所需的自定义分类术语分配给CPT。但是如果我继续下面的代码,我将从<form>, 它不像以前那样工作。if( $flightID !== NULL &&