【问题标题】:Woocommerce Plug-in: Woocommerce Category Banner - Add Category banner to single productWoocommerce 插件:Woocommerce 类别横幅 - 将类别横幅添加到单个产品
【发布时间】:2014-09-19 05:11:13
【问题描述】:

使用插件Woocommerce Category Banner,我可以在每个 woocommerce 类别页面上插入横幅图像(尽管插件在后端的 woocommerce 类别页面上添加了横幅字段)。

我正在尝试找到一种在单个产品页面上插入类别横幅图像的方法(1 级以上)。

这是用于显示类别横幅的代码(我已将其包含在 /theme/woocommerce.php 中):

//Retreives and print the category banner

global $woocommerce;
global $wp_query;

// Make sure this is a product category page
if ( is_product_category() ) {

    $cat_id = $wp_query->queried_object->term_id;
    $term_options = get_option( "taxonomy_term_$cat_id" ); 

    // Ge the banner image id
    if ( $term_options['banner_url_id'] != '' )
        $url = wp_get_attachment_url( $term_options['banner_url_id'] ); 

    // Exit if the image url doesn't exist
    if ( !isset( $url ) or $url == false )
        return;

    // Get the banner link if it exists
    if ( $term_options['banner_link'] != '' )
        $link = $term_options['banner_link'];

    // Print Output
    if ( isset( $link ) )
        echo "<a href='" . $link . "'>"; 

    if ( $url != false ) 
        echo "<img src='" . $url . "' class='category_banner_image' />";

    if ( isset( $link ) )
        echo "</a>";
}

使用我在上面代码之后直接在此处找到的代码,我可以在单个产品页面上插入类别缩略图:

elseif ( is_product() ) {
    $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="category_banner_image" src="'.$image.'">';
}

关于如何修改任一代码块以提取“banner_url_id”、woocommerce 类别横幅插件添加到每个类别并插入单个产品页面的任何想法?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    图片的url存储在wp_option表中,所以你需要把这个放在content-single-product.php中:

    $cat_id = $wp_query->queried_object->term_id;
    $term_options = get_option( "taxonomy_term_$cat_id" );
    
    $url = wp_get_attachment_url( $term_options['banner_url_id'] );
    
    if ( $url != false ) 
        echo "<img src='" . $url . "' class='category_banner_image' />";
    

    编辑:

    如果您想在 content_single_product.php 中获取横幅,您需要考虑以下几点:每个产品都可以与许多术语相关联(例如:红色、蓝色、白色等),因此您需要选择一个随机图像关联到每个类别,对吧?

     $termArray =  array();
     $terms = get_the_terms($post->ID, "product_cat");
    
     //insert id's in to array
     foreach ($terms as $id) {
        $termArray[] = $id->term_id;
     }
    
    //get random id
    $randomId = array_rand($termArray);
    
     //final ID
    $cat_id = $termArray[$randomId];
    
    $term_options = get_option( "taxonomy_term_$cat_id" );
    
    $url = wp_get_attachment_url( $term_options['banner_url_id'] );
    
    if ( $url != false ) 
       echo "<img src='" . $url . "' class='category_banner_image' />";
    

    【讨论】:

    • 嘿,抱歉回复晚了。我将 content-single-product.php 复制到“theme/woocommerce/”并添加了上面的代码。它似乎没有做任何事情。看起来您没有更改代码中的任何内容,它是否应该在移动到新的 .php 页面后简单地工作?我之前应该提到过这一点,该插件在后端的单个产品页面上不包含“类别横幅”。我需要从产品所在的类别中提取“banner_url_id”。
    猜你喜欢
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2015-07-16
    • 2017-06-05
    相关资源
    最近更新 更多