1 回答
TA贡献1878条经验 获得超4个赞
您的代码中存在一些错误和遗漏的内容。尝试以下操作,使用自定义轻型 SQL 查询获取术语名称具有pa_dagaanbieding产品属性(分类法)的产品:Ja
if( !function_exists('display_product_dagaanbieding_ja') ) {
function display_product_dagaanbieding_ja( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'taxonomy' => 'pa_dagaanbieding', // The taxonomy of this product attribute
'term_name' => 'Ja', // The term name for this product attribute
),
$atts
));
global $wpdb;
// SQL query: To get the product ID from defined product attribute term name
$product_id = $wpdb->get_var( $wpdb->prepare("
SELECT tr.object_id
FROM {$wpdb->prefix}term_relationships tr
INNER JOIN {$wpdb->prefix}term_taxonomy tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms t
ON tt.term_id = t.term_id
WHERE tt.taxonomy = '%s'
AND t.name = '%s'
", $taxonomy, $term_name ) );
// Exit if there is no product Id
if( ! $product_id ) return;
// Get an instance of the product object
$product = wc_get_product( $product_id );
// Exit if the product object is not defined
if( ! is_a( $product, 'WC_Product' ) ) return;
// Displays go here
return '<div class="row">
<div class="col-md-6"><h3>'.$product->get_title().'</h3>'.$product->get_image().'<br><br><a href="'.$product->get_permalink().'"><button type="submit" class="single_add_to_cart_button button alt">Bekijk aanbieding</button></a></div>
<div class="col-md-6">'.$product->get_price_html().'</div>
</div>';
}
add_shortcode( 'product_ja', 'display_product_dagaanbieding_ja' );
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
用法: [product_ja]或echo do_shortcode('[product_ja]');
- 1 回答
- 0 关注
- 81 浏览
添加回答
举报