head头部有很多代码都和seo有关,比如可以添加规范标签canonical、robots.txt标签等等。那么如何自定义在分类中添加head代码呢

代码
global $texonomy_slug_definedhead;
$texonomy_slug_definedhead='category';
add_action($texonomy_slug_definedhead.'_add_form_fields','categorydefinedhead');
function categorydefinedhead($taxonomy){ ?>
<div>
<label for="tag-definedhead">自定义head</label>
<input type="text" name="tag-definedhead" id="tag-definedhead" value="" /><br /><span>输入head代码</span>
</div>
<?php }
add_action($texonomy_slug_definedhead.'_edit_form_fields','categorydefinedheadedit');
function categorydefinedheadedit($taxonomy){ ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="tag-definedhead">自定义head</label></th>
<td><input type="text" name="tag-definedhead" id="tag-definedhead" value="<?php echo get_option('_category_definedhead'.$taxonomy->term_id); ?>" /><br /><span class="description">输入head代码</span></td>
</tr>
<?php }
add_action('edit_term','categorydefinedheadsave');
add_action('create_term','categorydefinedheadsave');
function categorydefinedheadsave($term_id){
if(isset($_POST['tag-definedhead'])){
if(isset($_POST['tag-definedhead']))
update_option('_category_definedhead'.$term_id,$_POST['tag-definedhead'] );
}
}
在函数模板上面的代码,添加一个字段
调用
分类添加自定义head
<?php
if(is_category()){
$title = the_zm_title();
$category_info = get_the_category();
$category_id = $category_info[0]->cat_ID;
$cat_definedhead = get_option('_category_definedhead'.$category_id);
?>
<?php echo $cat_definedhead;?>
<?php } ?>
将代码放到header中使用即可