我正在使用SF Taxonomy缩略图插件将类别图像添加到我的类别。
我试图在标题元素的样式内的背景图像中使用类别图像的src。
我可以使用以下命令在每个单独的类别上显示相关的类别图像:
echo get_term_thumbnail($wp_query->get_queried_object_id());
返回正确的img标签,但是我需要src而不是元素。什么好主意吗?
谢谢
您需要使用wp_get_attachment_image_src()
,它返回一个数组(url,宽度,高度,is_intermediate),或false,如果没有可用的图像
$term_thumbnail = get_term_thumbnail($wp_query->get_queried_object_id());
$size = 'medium';// choose the image size to get
$attachment_img_atts = wp_get_attachment_image_src( $term_thumbnail, $size);
if ( $attachment_img_atts ) {
?>
<img src="<?php echo $attachment_img_atts[0]; ?>" width="<?php echo $attachment_img_atts[1]; ?>" height="<?php echo $attachment_img_atts[2]; ?>" />
<?php
}
详细信息wp_get_attachment_image_src()
编辑:wp_get_attachment_image_url()返回一个数组,其中包含url,附件id在参数中的宽度和高度(参见其他可选和其默认值),您只需要$attachment_img_atts[0];在你的例子中,把它放在后台url。
function se_40495669($post_id){
$term_thumbnail = get_term_thumbnail($post_id);
$size = 'large';// choose the image size to get
$attachment_img_atts = wp_get_attachment_image_src( $term_thumbnail, $size);
if ( $attachment_img_atts ) {
$url = $attachment_img_atts[0];
}
return $url;
}
并在你的代码中使用它:
style="background-image: url(<?php echo se_40495669($wp_query->get_queried_object_id()); ?>)";
希望有帮助!
function expats_term_get_attachment(){
$args = array(
'orderby'=> 'name',
'order' => 'ASC',
'hide_empty' => true,
'exclude'=> array(),
'exclude_tree' => array(),
'include'=> array(),
'number' => '',
'fields' => 'all',
'slug' => '',
'parent' => '',
'hierarchical' => true, 'child_of' => 0,
'childless' => false,
'get' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false,
'offset' => '',
'search' => '',
'cache_domain' => 'core', );
}
$taxonomy = 'category';
expats_term_get_attachment();
$terms = get_terms($taxonomy, $args);
foreach ( $terms as $term ) {
$cat_url = wp_get_attachment_image_url( get_term_thumbnail_id( $term->term_taxonomy_id ), '' );
$cat_slug = $term->slug;
$cat_name = $term->name;
?>
<div class="col-md-3">
<div class="cat-image" style="background-url('<?php echo $cat_url; ?>');">
<p><?php echo $cat_name; ?></p>
</div>
</div>
<?php } ?>