您现在的位置是:首页 > 网站制作 > WordpressWordpress
Taxonomy Images 插件如何读取分类图片路径
杰帅2022-11-18【Wordpress】人已围观
简介描述DISPLAYING YOUR IMAGES IN YOUR THEMEThere are a few filters that you can use in your theme to display the image associations created by this plugin. Please
描述
DISPLAYING YOUR IMAGES IN YOUR THEME
There are a few filters that you can use in your theme to display the image associations created by this plugin. Please read below for detailed information.
DISPLAY A SINGLE IMAGE REPRESENTING THE TERM ARCHIVE
The following filter will display the image associated with the term asked for in the query string of the URL. This filter only works in views that naturally use templates like category.php, tag.php, taxonomy.php and all of their derivatives. Please read about template hierarchy for more information about these templates. The simplest use of this filter looks like:
print apply_filters( 'taxonomy-images-queried-term-image', '' );
This code will generate and print an image tag. It’s output can be modifed by passing an optional third parameter to apply_filters(). This parameter is an array and the following keys may be set:
-
after (string) – Text to append to the image’s HTML.
-
attr (array) – Key / value pairs representing the attributes of the
img
tag. Available options include:alt
,class
,src
andtitle
. This array will be passed as the fourth parameter to WordPress core functionwp_get_attachment_image()
without modification. -
before (string) – Text to prepend to the image’s HTML.
-
image_size (string) – May be any image size registered with WordPress. If no image size is specified, ‘thumbnail’ will be used as a default value. In the event that an unregistered size is specified, this filter will return an empty string.
Here’s an example of what a fully customized version of this filter might look like:
print apply_filters( 'taxonomy-images-queried-term-image', '', array(
'attr' => array(
'alt' => 'Custom alternative text',
'class' => 'my-class-list bunnies turtles',
'src' => 'this-is-where-the-image-lives.png',
'title' => 'Custom Title',
),
'before' => '<div id="my-custom-div">',
'after' => '</div>',
'image_size' => 'medium'
) );
SIMILAR FUNCTIONALITY
If you just need to get the database ID for the image, you may want to use:
$image_id = apply_filters( 'taxonomy-images-queried-term-image-id', 0 );
If you need to get the full object of the image, you may want to use:
$image = apply_filters( 'taxonomy-images-queried-term-image-object', '' );
If you need to get the URL to the image, you may want to use the following:
$image_url = apply_filters( 'taxonomy-images-queried-term-image-url', '' );
You can specify the size of the image in an option third parameter:
$image_url = apply_filters( 'taxonomy-images-queried-term-image-url', '', array(
'image_size' => 'medium'
) );
If you need data about the image, you may want to use:
$image_data = apply_filters( 'taxonomy-images-queried-term-image-data', '' );
You can specify the size of the image in an option third parameter:
$image_data = apply_filters( 'taxonomy-images-queried-term-image-data', '', array(
'image_size' => 'medium'
) );
LIST TERM IMAGES ASSOCIATED WITH A POST OBJECT
When a post is being displayed you may want to display the images associated with all of the terms associated with the post. The taxonomy-images-list-the-terms
filter does this. Here’s what it looks like in its simplest form:
print apply_filters( 'taxonomy-images-list-the-terms', '' );
This filter accepts an optional third parameter that you can use to customize its output. It is an array which recognizes the following keys:
-
after (string) – Text to append to the output. Default value is a closing unordered list tag.
-
after_image (string) – Text to append to each image. Default value is a closing list-item tag.
-
before (string) – Text to prepend to the output. Default value is an open unordered list tag with an class attribute of “taxonomy-images-the-terms”.
-
before_image (string) – Text to prepend to each image. Default value is an open list-item tag.
-
image_size (string) – Any registered image size. Values will vary from installation to installation. Image sizes defined in core include: “thumbnail”, “medium” and “large”. “full” may also be used to get the unmodified image that was uploaded. Defaults to “thumbnail”.
-
post_id (int) – The post to retrieve terms from. Defaults to the ID property of the global
$post
object. -
taxonomy (string) – Name of a registered taxonomy to return terms from. Defaults to
category
.
Here’s an example of what a fully customized version of this filter might look like:
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="my-custom-class-name">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'post_id' => 1234,
'taxonomy' => 'post_tag',
) );
WORKING WITH ALL TERMS OF A GIVEN TAXONOMY
You will want to use the taxonomy-images-get-terms
filter. This filter is basically a wrapper for WordPress core function get_terms(). It will return an array of enhanced term objects: each term object will have a custom property named image_id
which is an integer representing the database ID of the image associated with the term. This filter can be used to create custom lists of terms. Here’s what it’s default useage looks like:
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
Here is what php’s print_r()
function may return:
Array
(
[0] => stdClass Object
(
[term_id] => 8
[name] => Pirate
[slug] => pirate
[term_group] => 0
[term_taxonomy_id] => 8
[taxonomy] => category
[description] => Pirates live in the ocean and ride around on boats.
[parent] => 0
[count] => 1
[image_id] => 44
)
)
As you can see, all of the goodness of get_terms()
is there with an added bonus: the image_id
parameter!
This filter recognizes an optional third parameter which is an array of arguments that can be used to modify its output:
-
cache_images (bool) If this value is
true
all associated images will be queried and cached for later use in various template tags. If it is set tofalse
, this query will be suppressed. Do not set this value to false unless you have a really good reason for doing so Taxonomy Images $image_url很赞哦! ()
相关文章
随机图文
-
wp_reset_postdata 和 wp_reset_query 的作用与区别
什么时候使用wp_reset_query,什么时候用wp_reset_postdata? -
wordpress发布文章HTML标签被自动过滤掉该如何处理?
wordpress发布文章时很多html标签都会自动过滤掉,造成了文章中无法添加<style></style><script></script>等标签。那么该如何如何处理呢? 解决方法一: 将wp-includes文件夹下 -
WordPress 页面模板(Page Template)下拉列表不显示的原因及解决方法
WordPress 的自定义页面模板是一个非常强大好用的功能,使用它新建一些静态页面(Page),添加上一些数据调用的函数,再在网页上做一个导航连接到对应的页面就可以实现很多自定义的功 -
wordpress 上传的图片不显示的问题 base64,data:image/gif
文章评论
本站推荐
标签云
猜你喜欢
- 什么是WordPress中的SEO友好URL结构
- wordpress默认评论模板怎么修改
- WordPress 插件开发教程:Part1 WordPress 插件简介
- wordpress优化头部冗余代码
- WPReplace插件实现WordPress内容字符快速替换效果
- 实现wordpress搜索结果页面搜索关键词高亮
- wordpress nginx多站点rewrite(重写)规则
- WordPress Zephyr Project Manager Plugin 跨站请求伪造漏洞
- Zblog转换为WordPress最简单的方法教程
- OpenLiteSpeed禁用xmlrpc.php 避免DDoS攻击