欢迎光临
我们一直在努力

WordPress为分类栏目添加自定义缩略图字段功能

在wordpress企业主题开发中wordpress分类栏目通常是需要单独设置缩略图的,之前给大家分享过wordpress分类缩略图插件,今天给大家分享一个如何通过代码来添加分类栏目缩略图字段功能

将如下代码添加到wordpress主题functions.php中:

//////////////////////////////////为分类添加缩略图
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);

添加好以上代码,如何调用呢,在需要显示分类缩略图的位置添加以下代码即可完成自动的调用功能。

	
echo get_option('thumb_color-'.$category_id)
赞(0) 打赏
未经允许不得转载:WORDPRESS大侠 » WordPress为分类栏目添加自定义缩略图字段功能

评论 抢沙发

评论前必须登录!

 

更好的WordPress主题

支持快讯、专题、百度收录推送、人机验证、多级分类筛选器,适用于垂直站点、科技博客、个人站,扁平化设计、简洁白色、超多功能配置、会员中心、直达链接、文章图片弹窗、自动缩略图等...

联系我们联系我们

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册