前面博客吧分享过wordpress增加分类关键词自定义字段的教程代码,而这次给分类和标签增加自定义字段的核心代码和分类关键词自定义字段代码其实是一样的,只是进行了小小的优化和扩展以及增加了标签的钩子函数,下面以给分类和标签增加自定义标题、自定义关键词和自定义描述的自定义字段的示例代码。
把下面的代码添加至主题的 functions.php 文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
<?php
function boke8_net_add_category_field(){
echo ‘<div class=”form-field”>
<label for=”tag-title”>自定义标题</label>
<input name=”tag-title” id=”tag-title” type=”text” value=”” size=”40″>
<p>请在此输入用于SEO优化的标题。</p>
</div>’;
echo ‘<div class=”form-field”>
<label for=”tag-keywords”>自定义关键词</label>
<input name=”tag-keywords” id=”tag-keywords” type=”text” value=”” size=”40″>
<p>请在此输入用于SEO优化的关键词。</p>
</div>’;
echo ‘<div class=”form-field”>
<label for=”tag-description”>自定义描述</label>
<textarea name=”tag-description” id=”tag-description” class=”large-text” rows=”5″ cols=”50″></textarea>
<p>请在此输入用于SEO优化的关键词。</p>
</div>’;
}
function boke8_net_edit_category_field($tag){
echo ‘<tr class=”form-field”>
<th scope=”row”><label for=”tag-title”>自定义标题</label></th>
<td>
<p><input name=”tag-title” id=”tag-title” type=”text” value=”‘;
echo get_option(‘_category_title’.$tag->term_id).‘” size=”40″/></p>
<p class=”description”>请在此输入用于SEO优化的标题。</p>
</td>
</tr>’;
echo ‘<tr class=”form-field”>
<th scope=”row”><label for=”tag-keywords”>自定义关键词</label></th>
<td>
<p><input name=”tag-keywords” id=”tag-keywords” type=”text” value=”‘;
echo get_option(‘_category_keywords’.$tag->term_id).‘” size=”40″/></p>
<p class=”cat-url”>请在此输入用于SEO优化的关键词。</p>
</td>
</tr>’;
echo ‘<tr class=”form-field”>
<th scope=”row”><label for=”tag-description”>自定义描述</label></th>
<td>
<p><textarea name=”tag-description” id=”tag-description” class=”large-text” rows=”5″ cols=”50″>’.get_option(‘_category_description’.$tag->term_id).‘</textarea></p>
<p class=”cat-url”>请在此输入用于SEO优化的描述。</p>
</td>
</tr>’;
}
function boke8_net_taxonomy_metadate($term_id){
if(isset($_POST[‘tag-title’]) && isset($_POST[‘tag-keywords’]) && isset($_POST[‘tag-description’])){
if(!current_user_can(‘manage_categories’)){
return $term_id;
}
$title_key = ‘_category_title’.$term_id; // key 选项名为 cat-tel-1 类型
$title_value = $_POST[‘tag-title’]; // value
$word_key = ‘_category_keywords’.$term_id;
$word_value = $_POST[‘tag-keywords’];
$desc_key = ‘_category_description’.$term_id;
$desc_value = $_POST[‘tag-description’];
update_option( $title_key, $title_value );
update_option( $word_key, $word_value );
update_option( $desc_key, $desc_value );
}
}
add_action(‘category_add_form_fields’,‘boke8_net_add_category_field’,10,2);
add_action(‘category_edit_form_fields’,‘boke8_net_edit_category_field’,10,2);
add_action(‘created_category’,‘boke8_net_taxonomy_metadate’,10,1);
add_action(‘edited_category’,‘boke8_net_taxonomy_metadate’,10,1);
add_action( ‘post_tag_add_form_fields’, ‘boke8_net_add_category_field’,10,2);
add_action( ‘post_tag_edit_form_fields’, ‘boke8_net_edit_category_field’,10,2);
add_action( ‘edited_post_tag’, ‘boke8_net_taxonomy_metadate’,10,1);
add_action( ‘create_post_tag’, ‘boke8_net_taxonomy_metadate’,10,1);
?>
|
输出代码
1
2
3
4
5
|
<?php
echo get_option(‘_category_title’.get_queried_object_id());
echo get_option(‘_category_keywords’.get_queried_object_id());
echo get_option(‘_category_description’.get_queried_object_id());
?>
|
版权声明:原创作品,未经允许不得转载,否则将追究法律责任。
本站资源有的自互联网收集整理,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
本文链接:65资源网https://www.65xwz.cn/1145.html
许可协议:《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
本站资源有的自互联网收集整理,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
本文链接:65资源网https://www.65xwz.cn/1145.html
许可协议:《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
评论0+