文章描述:
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
请登录后查看评论内容