WordPress分类栏目添加自定义缩略图

WordPress分类栏目添加自定义缩略图

作者 : 资源客 发布时间: 2019-12-6

我们通常在 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)

 

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » WordPress分类栏目添加自定义缩略图