wordpress栏目新增关键词和描述

文章描述:

WordPress栏目分类新增字段以及在模板里面调用

显示

// 分类添加字段
function ems_add_category_field(){
    echo \'<div class=\"form-field\">
            <label for=\"cat-keywords\">关键词</label>
            <input name=\"cat-keywords\" id=\"cat-keywords\" type=\"text\" value=\"\" size=\"240\">
            <p>The keywords.</p>
          </div>\';
    echo \'<div class=\"form-field\">
            <label for=\"cat-description\">描述</label>
            <input name=\"cat-description\" id=\"cat-description\" type=\"text\" value=\"\" size=\"500\">
            <p>The description.</p>
          </div>\';

}
add_action(\'category_add_form_fields\',\'ems_add_category_field\',10,2);

// 分类编辑字段
function ems_edit_category_field($tag){
    echo \'<tr class=\"form-field\">
            <th scope=\"row\"><label for=\"cat-keywords\">关键词</label></th>
            <td>
                <input name=\"cat-keywords\" id=\"cat-keywords\" type=\"text\" value=\"\';
    echo get_option(\'cat-keywords-\'.$tag->term_id).\'\" size=\"40\"/><br>
                <span class=\"cat-keywords\">\'.$tag->name.\' on the keywords.</span>
            </td>
        </tr>\';

    echo \'<tr class=\"form-field\">
            <th scope=\"row\"><label for=\"cat-description\">描述</label></th>
            <td>
                <input name=\"cat-description\" id=\"cat-description\" type=\"text\" value=\"\';
    echo get_option(\'cat-description-\'.$tag->term_id).\'\" size=\"40\"/><br>
                <span class=\"cat-description\">\'.$tag->name.\' on the description.</span>
            </td>
        </tr>\';


}
add_action(\'category_edit_form_fields\',\'ems_edit_category_field\',10,2);

// 保存数据
function ems_taxonomy_metadate($term_id){
    if(isset($_POST[\'cat-keywords\']) && isset($_POST[\'cat-description\'])){
        //判断权限--可改
        if(!current_user_can(\'manage_categories\')){
            return $term_id;
        }
        // 关键词
        $keywords_key = \'cat-keywords-\'.$term_id; // key 选项名为 cat-keywords-1 类型
        $keywords_value = $_POST[\'cat-keywords\'];	// value

        // 描述
        $description_key = \'cat-description-\'.$term_id;
        $description_value = $_POST[\'cat-description\'];

        // 更新选项值
        update_option( $keywords_key, $keywords_value );
        update_option( $description_key, $description_value );
    }
}

// 虽然要两个钩子,但是我们可以两个钩子使用同一个函数
add_action(\'created_category\',\'ems_taxonomy_metadate\',10,1);
add_action(\'edited_category\',\'ems_taxonomy_metadate\',10,1);

 

使用

<?php
// 取出当前分类 id
$categories = get_the_category();
$term_id = $categories[0]->term_id;
$cat_name = $categories[0]->name;
?>
<div class=\"main\">
    <div class=\"p1\"><?php echo get_option(\'cat-keywords-\'.$term_id);?></div>
    <div class=\"p2\"><?php echo get_option(\'cat-description-\'.$term_id);?></div>
</div>

 

THE END
版权声明 1、本网站名称:AOPK资源网
2、本站永久网址:https://www.aopk.cn
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。

点赞15赞赏 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容