Php 使用Taxonomy Images插件获取每个Woocommerce产品标签的图像
我正在为Woocommerce开发一个插件,并使用该插件为Woocommerce产品标签自定义分类添加图像功能 现在,我试图在一个函数中为每个产品显示产品标签名称及其相应的图像。但我可能失败了,因为我看到了所有标签,而不仅仅是特定产品的标签 要获取产品ID,我已尝试使用Php 使用Taxonomy Images插件获取每个Woocommerce产品标签的图像,php,wordpress,woocommerce,product,custom-taxonomy,Php,Wordpress,Woocommerce,Product,Custom Taxonomy,我正在为Woocommerce开发一个插件,并使用该插件为Woocommerce产品标签自定义分类添加图像功能 现在,我试图在一个函数中为每个产品显示产品标签名称及其相应的图像。但我可能失败了,因为我看到了所有标签,而不仅仅是特定产品的标签 要获取产品ID,我已尝试使用global$product但它不工作 这是我的代码: function woo_idoneo_tab_content() { $id = get_the_ID(); $terms = apply_filter
global$product代码>但它不工作
这是我的代码:
function woo_idoneo_tab_content() {
$id = get_the_ID();
$terms = apply_filters( 'taxonomy-images-get-terms', '', array('taxonomy' => 'product_tag', 'id' => $id, ) );
if ( ! empty( $terms ) ) {
print '<div class="container">';
foreach ( (array) $terms as $term) {
$url = get_term_link ($term->slug, 'product_tag' , $id);
print '<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">';
print '<div class="card">';
print '<div class="card-img-top"><a href="'.$url.'">' . wp_get_attachment_image( $term->image_id, 'medium' ) . '</a></div>';
print '<div class="card-body">';
print "<h5 class='card-title'><a class='btn btn-primary' href='{$url}'>{$term->name}</a></h5>";
}
print '</div>';
print '</div>';
print '</div>';
}
print '</div>';
}
}
function-woo\u-idoneo\u-tab\u-content(){
$id=get_the_id();
$terms=apply_过滤器('taxonomy images get terms','',数组('taxonomy'=>'product_tag','id'=>$id,);
如果(!空($terms)){
打印“”;
foreach((数组)$terms作为$term){
$url=get\u term\u link($term->slug,'product\u tag',$id);
打印“”;
打印“”;
打印“”;
打印“”;
打印“”;
}
打印“”;
打印“”;
打印“”;
}
打印“”;
}
}
感谢您的帮助。请不要:
$terms = apply_filters( 'taxonomy-images-get-terms', '', array('taxonomy' => 'product_tag', 'id' => $id, ) );
你应使用:
$terms = wp_get_post_terms( get_the_ID(), 'product_tag' );
现在插件将分类图像存储在wp\u otion
表中。要获取该数据,您将使用:
$custom_taxonomy_images = get_option( 'taxonomy_image_plugin' ); // Array of term Ids / Image Ids pairs
因此,您重新访问的代码现在是:
function woo_idoneo_tab_content() {
// Check that plugin is active, if not we exit.
if( ! is_plugin_active( 'taxonomy-images/taxonomy-images.php' ) ) return;
$taxonomy = 'product_tag';
$post_id = get_the_ID();
$terms = wp_get_post_terms( $post_id, $taxonomy ); // Terms for this post
$custom_taxonomy_images = get_option( 'taxonomy_image_plugin' ); // Plugin images data
if ( empty( $terms ) ) return; // If no terms found, we exit.
## -- HTML Output below -- ##
echo '<div class="container">';
// Loop through each term in this post for the defined taxonomy
foreach ( $terms as $term ) {
$attachment_id = $custom_taxonomy_images[intval($term->term_id)]; // Get image Attachement ID
$image = wp_get_attachment_image( $attachment_id, 'medium' ); // Get image to be displayed
$url = get_term_link( $term->term_id, $taxonomy ); // Get term URL
?>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
<div class="card">
<div class="card-img-top">
<a href="<?php echo $url; ?>"><?php echo $image; ?></a>
</div>
<div class="card-body">
<h5 class="card-title">
<a class="btn btn-primary" href="<?php echo $url; ?>"><?php echo $term->name; ?></a>
</h5>
</div>
</div>
</div>
<?php
}
echo '</div>';
}
function-woo\u-idoneo\u-tab\u-content(){
//检查插件是否处于活动状态,否则退出。
如果(!is_plugin_active('taxonomy images/taxonomy images.php'))返回;
$taxonomy='product_tag';
$post_id=获取_id();
$terms=wp\u get\u post\u terms($post\u id,$taxonomy);//此帖子的术语
$custom_taxonomy_images=get_选项('taxonomy_image_plugin');//插件图像数据
if(empty($terms))return;//如果找不到术语,则退出。
##--下面的HTML输出--##
回声';
//循环浏览本文中定义的分类法的每个术语
foreach($terms作为$term){
$attachment_id=$custom_taxonomy_images[intval($term->term_id)];//获取图像附件id
$image=wp_get_attachment_image($attachment_id,'medium');//获取要显示的图像
$url=get_term_link($term->term\u id,$taxonomy);//获取术语url
?>
你需要在问题上直接输入代码,而不仅仅是粘贴库的链接。这是本网站的规则。谢谢。谢谢,这很好。一个问题,intval参数是什么?@user3421040[theinval](http://php.net/manual/en/function.intval.php)
php函数]()允许获取变量的整数值…请,如果这个答案回答了您的问题,谢谢。