使用插件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类别横幅插件添加到每个类别并插入单个产品页面的任何想法?
答案 0 :(得分:0)
图片的网址存储在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' />";