以短代码显示Wooccommerce子类别图像



所以我有一个运行良好的短代码,它可以拉入产品的子类别并显示图像和文本,但我意识到它位于内容的顶部,因为我使用了echo。所以我把HTML输出移到了一个变量中,这样我就可以返回它,但图像来自列表项,所以函数似乎有问题:woommerce_subcategory_thumbnail((

不太确定为什么,但我认为函数一定有回声?我想我只想得到图片的url并把它放在一个容器里?老实说,我不知道什么是最好的方法,但这就是我在的地方

add_shortcode( 'show_products_categories_os', 'categories_of_the_product_os' );
function categories_of_the_product_os() {
$term_id = get_term_by( 'slug', 'os', 'product_cat' );
$terms = get_the_terms( get_the_ID(), 'product_cat' );

if ( $terms ) {         
$output_html .= '<ul class="product-cats osp">';     
foreach ( $terms as $term ) {
if($term->parent === $term_id->term_id){
$output_html .= '<li class="category os">';
$output_html .= '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '" target="_blank">' . woocommerce_subcategory_thumbnail( $term ) . '</a>';
$output_html .= '<h2><a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '" target="_blank">' . $term->name . '</a></h2>';                                                                             
$output_html .= '</li>';
}                                                                    
}     
$output_html .= '</ul>'; 
}
return $output_html;
}

有没有另一个我找不到的函数可以给我jst图像的url?或者是一种将其从其他功能中剥离的方法?

您需要从术语的元数据中获取thumbnail_id,并将其作为参数传递给wp_get_attachment_url


add_shortcode( 'show_products_categories_os', 'categories_of_the_product_os' );
function categories_of_the_product_os() {
$term_id = get_term_by( 'slug', 'os', 'product_cat' );
$terms = get_the_terms( get_the_ID(), 'product_cat' );

if ( $terms ) {         
$output_html .= '<ul class="product-cats osp">';     
foreach ( $terms as $term ) {
$thumbnail_id = get_term_meta( $term->term_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
if($term->parent === $term_id->term_id){
$output_html .= '<li class="category os">';
$output_html .= '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '" target="_blank">' . $image_url . '</a>';
$output_html .= '<h2><a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '" target="_blank">' . $term->name . '</a></h2>';                                                                             
$output_html .= '</li>';
}                                                                    
}     
$output_html .= '</ul>'; 
}
return $output_html;
}

来源:https://stackoverflow.com/a/51465602/14481105

注意

现在,您需要将$image_url添加到img标记的src属性中,以便输出图像,因为目前只显示URL。

最新更新