一直以来,wordpress的后台的分类目录没有添加缩略图的功能,一般情况下,也没什么,毕竟很多网站也用不上这些。但有些企业网站可能就会需要这个缩略图,它们想在不同的分类目录页面展示不同的缩略图。当然,可以通过指定图片来实现,但那样一业,后期更换图片时,就相对不太灵活。而给wordpress后台分类目录里添加缩略图功能,就显得方便的多——如果想修改哪个分类的缩略图,只需要在后台设置一下就好了,多方便。闲话不多说,一起来看看怎样实现吧。(本文转自:wanlimm.com/77201902287140.html)
方法一:非上传形式添加缩略图功能。
//添加缩略图
function salong_add_category_field(){
echo '<div class="form-field">
<label for="thumb">'.__('缩略图','salong').'</label>
<input name="thumb" id="thumb" type="text" value="" size="40">
<p>'.__('输入分类的缩略图链接。','salong').'</p>
</div>';
}
add_action('category_add_form_fields','salong_add_category_field',10,2);
//分类编辑字段
function salong_edit_category_field($tag){
echo '<tr class="form-field">
<th scope="row"><label for="thumb">'.__('灰色地图','salong').'</label></th>
<td>
<input name="thumb" id="thumb" type="text" value="';
echo get_option('thumb-'.$tag->term_id).'" size="40"/><br>
<span class="thumb">'.$tag->name.__('分类的缩略图链接。','salong').'</span>
</td>
</tr>';
}
add_action('category_edit_form_fields','salong_edit_category_field',10,2);
//保存数据
function salong_category_thumb($term_id){
if(isset($_POST['thumb'])){
//判断权限--可改
if(!current_user_can('manage_categories')){
return $term_id;
}
$thumb_key = 'thumb-'.$term_id;
$thumb_value = $_POST['thumb'];
// 更新选项值
update_option( $thumb_key, $thumb_value );
}
}
add_action('created_category','salong_category_thumb',10,1);
add_action('edited_category','salong_category_thumb',10,1);
把上面这段代码添加到主题的functions.php文件中,这样就为wordpress分类目录添加好了缩略图功能。只是这个不是上传图片,而是填写图片url地址。
然后,在wordpress主题前台页面想要添加缩略图的地方调用下面这句:
echo get_option('thumb_color-'.$category_id)
方法二:上传缩略图功能
把下面这段代码添加到wordpress主题 的functions.php文件中:
<?php
/**
* Plugin Name: 分类图像
*/
?>
<?php
if (!defined('Z_PLUGIN_URL'))
define('Z_PLUGIN_URL', untrailingslashit(plugins_url('', __FILE__)));
define('Z_IMAGE_PLACEHOLDER', get_bloginfo("template_url")."/images/placeholder.jpg");
// l10n
load_plugin_textdomain('zci', FALSE, 'categories-images/languages');
add_action('admin_init', 'z_init');
function z_init() {
$z_taxonomies = get_taxonomies();
if (is_array($z_taxonomies)) {
$zci_options = get_option('zci_options');
if (empty($zci_options['excluded_taxonomies']))
$zci_options['excluded_taxonomies'] = array();