WordPress 为分类添加图片的插件:Categories Images

转载 2015年07月10日 18:52:45

Categories Images is a WordPress plugin which allow you to add an image for each category (or term), it is so easy to user. This plugin will create a new text input that will had the image url of the category, in both forms when adding a new category or edit an existing one, you can put a url for the image or upload it bu click on the text input.

Installation:

  1. Open your wordpress admin panel, then plugins menu > Add New and search for ‘Categories Images’.
  2. Click to install.
  3. Once installed, activate and it is functional.

Or you can install in manual:

  1. Download the plugin from WordPress Plugins.
  2. Extract it, then upload the extracted folder ‘categories-images’ to ‘/wp-content/plugins/’.
  3. Go to your wordpress plugins menu, then activate it.

You are done! And the plugin is ready to use.
Usage and documentation:
after installing and activating the plugin, just use the following code:
Default usage:
use the following code in category or taxonomy template,put it in any <img /> tag :
1
<?php if (function_exists('z_taxonomy_image_url')) echo z_taxonomy_image_url(); ?>

or simply:

1
<?php if (function_exists('z_taxonomy_image')) z_taxonomy_image(); ?>
Difference between the two methods are as following:
  • z_taxonomy_image_url() return the taxonomy image url as a string so you can put it in any <img /> tag with support of the following parameters:
    • $term_id, the category or taxonomy ID, default NULL
    • $size, default ‘full’
    • $return_placeholder, default FALSE
  • z_taxonomy_image() return category or taxonomy image as html with support of the following parameters:
    • $term_id, the category or taxonomy ID, default NULL
    • $size, image size (check here for more about WordPress image resizing), default ‘full’
    • $attr, array of some html img tag attributes like: (default NULL) please check parameter $attrhere
      • alt, default is the image file name
      • class, default is ‘attachment-$size’
      • height, default none
      • width, default none
      • title, default none
    • $echo, to enable or disable printing out the html, default TRUE.
Using inside a loop:
use the following code in anywhere at your wordpress theme, here is an example:
If  looping for categories:
1
2
3
4
5
6
7
8
<ul>
 <?php foreach (get_categories() as $cat) : ?>
 <li>
 <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
 <a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a>
 </li>
 <?php endforeach; ?>
</ul>

or

1
2
3
4
5
6
7
8
<ul>
 <?php foreach (get_categories() as $cat) : ?>
 <li>
 <?php z_taxonomy_image($cat->term_id); ?>
 <a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a>
 </li>
 <?php endforeach; ?>
</ul>

If looping for taxonomies:

1
2
3
4
5
6
7
8
<ul>
 <?php foreach (get_terms('your_taxonomy') as $cat) : ?>
 <li>
 <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
 <a href="<?php echo get_term_link($cat->slug, 'your_taxonomy'); ?>"><?php echo $cat->name; ?></a>
 </li>
 <?php endforeach; ?>
</ul>

or

1
2
3
4
5
6
7
8
<ul>
 <?php foreach (get_terms('your_taxonomy') as $cat) : ?>
 <li>
 <?php z_taxonomy_image($cat->term_id); ?>
 <a href="<?php echo get_term_link($cat->slug, 'your_taxonomy'); ?>"><?php echo $cat->name; ?></a>
 </li>
 <?php endforeach; ?>
</ul>

If your post had more than one category and you want to loop for all post categories use the following example:

1
2
3
4
5
6
7
8
<ul>
 <?php foreach (get_the_category() as $cat) : ?>
 <li>
 <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
 <a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a>
 </li>
 <?php endforeach; ?>
</ul>

or

1
2
3
4
5
6
7
8
<ul>
 <?php foreach (get_the_category() as $cat) : ?>
 <li>
 <?php z_taxonomy_image($cat->term_id); ?>
 <a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a>
 </li>
 <?php endforeach; ?>
</ul>

And if your post had more than one taxonomy and you want to loop for all post taxonomies use the following example:

1
2
3
4
5
6
7
8
<ul>
 <?php foreach (get_the_terms(get_the_ID(), 'your_taxonomy') as $cat) : ?>
 <li>
 <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
 <a href="<?php echo get_term_link($cat->term_id, 'your_taxonomy'); ?>"><?php echo $cat->name; ?></a>
 </li>
 <?php endforeach; ?>
</ul>

or

1
2
3
4
5
6
7
8
<ul>
 <?php foreach (get_the_terms(get_the_ID(), 'your_taxonomy') as $cat) : ?>
 <li>
 <?php z_taxonomy_image($cat->term_id); ?>
 <a href="<?php echo get_term_link($cat->term_id, 'your_taxonomy'); ?>"><?php echo $cat->name; ?></a>
 </li>
 <?php endforeach; ?>
</ul>

Using resizing feature:

To resize category image simply add the size as a second parameter, for example:
if in category or taxonomy template:

1
<img src="<?php echo z_taxonomy_image_url(NULL, 'thumbnail'); ?>" />

or

1
<?php z_taxonomy_image(NULL, 'thumbnail'); ?>

or if inside a loop:

1
2
3
4
5
6
7
8
<ul>
 <?php foreach (get_the_terms(get_the_ID(), 'your_taxonomy') as $cat) : ?>
 <li>
 <img src="<?php echo z_taxonomy_image_url($cat->term_id, 'medium'); ?>" />
 <a href="<?php echo get_term_link($cat->term_id, 'your_taxonomy'); ?>"><?php echo $cat->name; ?></a>
 </li>
 <?php endforeach; ?>
</ul>

you can choose to resize from these (thumbnail, medium, large, full) or any custom sizes, also you can use it as array with dimensions for example:

1
<img src="<?php echo z_taxonomy_image_url(NULL, array(300, 250)); ?>" />

or

1
<?php z_taxonomy_image(NULL, array(300, 250)); ?>

Please check WordPress codex here for more information about resizing.

To display image with support for alt, class, width and height, you can do as this example:

1
2
3
4
5
6
7
8
9
<?php
$attr = array(
'class' => 'category_image',
'alt' => 'image alt',
'height' => 200,
'width' => 300,
'title' => 'category title',
);
z_taxonomy_image(NULL, 'full', $attr); ?>

if there were any bugs or you faced any problems please reply to this post, if I had more time I will update this plugin and add more features.

update 26-07-2012: I had noticed that there is some people talking about my plugin, here is a link to the post :)

http://wpshock.com/wordpress-category-images-taxonomy-images-plugin/

another one here also:

http://www.williamsgraphics.co.uk/featured-images-categories-wordpress/

Update 06-10-2012: Finally I got some time to test the plugin in WordPress MU and now I confirm that the plugin works so fine whether you choose to activate the plugin by blog or using network activation from your network control panel

Update 25-01-2013: A great update had been made to this plugin, and hope you liked it, Thank so much to Joe Tse http://tkjune.com

Update 14-06-2013: I had updated the plugin with some new features like using WordPress new media uploader and new sub menu (Categories Images) in Settings menu allowing you to exclude any taxonomies from the plugin, this for fixing and avoiding any conflicts with another plugins like WooCommerce plugin, hope you all like the last update.

Update 20-12-2013: I had updated the plugin with new features like resizing images please check the docs above for more help about using this new feature, and also adding support for Spanish language. Thanks so much to Maria Ramos and to Rahil Wazir for their help.

Update 2.5 28-03-2015: A nice update which introduce a new function to display category or taxonomy image directly with support for size and alt and more attributes, please check the documentation above. Also adding support for Ukrainian language, thanks so much to Michael Yunat.

Android开发者峰会:Fresco - loading images fast

Fresco是运用于 Android 设备的图片加载组件,使用了 Fresco 就可以不必烦恼图片的加载、显示这种繁琐的问题。本次分享主要介绍 Fresco 可以如何使用,它有哪些强大的功能,及其背后的一些设计。
  • 2016年10月11日 16:56

wordpress中thumbnail图片尺寸强制crop的二个方法

1.利用timthumb.php //这里设定timthumb.php存放在子主题中 function thumbnail_filter($html, $post_id, $post_thumbna...
  • u013933233
  • u013933233
  • 2014-03-06 11:04:57
  • 1179

20款最好用的wordpress图片相册插件

用插件把你的图片相册打扮的更加漂亮,更加容易自己管理,也更加利于访客的浏览.本文里面,帕兰为你收集了21款WordPress图片相册插件.让我们先来看一下Wordpress的图片相册类插件的几个大类...
  • liuxuekai
  • liuxuekai
  • 2016-08-04 19:52:28
  • 9903

wordpress图片上传问题汇总

在图片上传问题上我共遇到了三个问题: 1.如何在wordpress后台的多媒体中分类管理上传的图片: 在WordPress后台的媒体编辑页面,默认情况下是没有分类和标签给你选的 需要手动添加...
  • Ryan2B
  • Ryan2B
  • 2017-08-18 17:11:34
  • 250

WordPress 为分类添加图片的插件:Categories Images

Categories Images is a WordPress plugin which allow you to add an image for each category (or term...
  • gsls181711
  • gsls181711
  • 2015-07-10 18:52:45
  • 1148

WordPress 主题文件结构及执行

打开一个 WordPress 主题文件夹,它可能包括以下文件: WordPress 自带主题 twentysixteen 的文件构成 从文件名字我们也可以推断出其大致作用,就这个主...
  • liuxuekai
  • liuxuekai
  • 2016-08-30 18:25:00
  • 3239

wordpress媒体库增加分类和标签

从WordPress后台媒体库上传的媒体文件,不像文章那样可以给它指定分类和标签,但是很多时候我们又需要这样的功能,如一些下载站、图片站等。 媒体编辑页面的原始状态 很明显...
  • u013476542
  • u013476542
  • 2016-10-12 11:01:33
  • 824

如何去掉wordpress默认插入图片链接

如何去掉wordpress默认图片链接,就是插入文章时,图片默认都是有链接的,如何默认没有链接 打开后台找到:   什么都不用改,只要点下: 出现: 这样就可以了...
  • Q1059081877Q
  • Q1059081877Q
  • 2015-08-12 22:25:50
  • 1056

wordpress 删除 默认分类

在 wordpress 菜单 Settings ->Default Post Category
  • hanshileiai
  • hanshileiai
  • 2014-08-07 09:57:03
  • 699

修改wordpress列表页默认显示文章的数量和排序方式

修改模板archive文章分类的显示条数 今天看到首页显示文章15条在后台可以控制,而某个分类下只显示10条内容。因为去图片展示所以导致页面显示不完整。 所以修改内容页面也显示15条。修改...
  • hanshileiai
  • hanshileiai
  • 2014-11-25 15:39:24
  • 5990
收藏助手
不良信息举报
您举报文章:WordPress 为分类添加图片的插件:Categories Images
举报原因:
原因补充:

(最多只允许输入30个字)