wordpress主题分类目录怎样添加添加缩略图功能?

本文详细介绍了如何为WordPress后台分类目录添加缩略图功能,包括两种方法:一种是通过输入图片URL,另一种是通过上传图片。通过添加自定义代码到主题的functions.php文件,可以实现分类目录的缩略图显示,并提供了调用方法。
摘要由CSDN通过智能技术生成

一直以来,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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值