【发布时间】: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