这篇文章主要介绍了如何给 wordpress Category添加字段,有一定的参考价值,感兴趣的朋友可以参考一下。
1. 在模板目录下的 function.php 里添加包含 页面 category_field.php
包括代码如下:
// 分类添加字段 require_once( dirname(__FILE__).'/category_field.php' );
2. 新建 category_field.php 页面
代码如下:
<?php
// 分类添加字段
function ems_add_category_field(){
echo '<p class="form-field">
<label for="cat-tel">Tel</label>
<input name="cat-tel" id="cat-tel" type="text" value="" size="40">
<p>The telephone.</p>
</p>';
echo '<p class="form-field">
<label for="cat-url">URL</label>
<input name="cat-url" id="cat-url" type="text" value="" size="40">
<p>The URL.</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1035">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680029477011.png" alt="Moshi Chat">
</a>
<div class="aritcle_card_info">
<a href="/ai/1035">Moshi Chat</a>
<p>法国AI实验室Kyutai推出的端到端实时多模态AI语音模型,具备听、说、看的能力,不仅可以实时收听,还能进行自然对话。</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="Moshi Chat">
<span>165</span>
</div>
</div>
<a href="/ai/1035" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="Moshi Chat">
</a>
</div>
</p>';
}
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-tel">Tel</label></th>
<td>
<input name="cat-tel" id="cat-tel" type="text" value="';
echo get_option('cat-tel-'.$tag->term_id).'" size="40"/><br>
<span class="cat-tel">'.$tag->name.' on the phone.</span>
</td>
</tr>';
echo '<tr class="form-field">
<th scope="row"><label for="cat-url">URL</label></th>
<td>
<input name="cat-url" id="cat-url" type="text" value="';
echo get_option('cat-url-'.$tag->term_id).'" size="40"/><br>
<span class="cat-url">'.$tag->name.' on the URL.</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-tel']) && isset($_POST['cat-url'])){
//判断权限--可改
if(!current_user_can('manage_categories')){
return $term_id;
}
// 电话
$tel_key = 'cat-tel-'.$term_id; // key 选项名为 cat-tel-1 类型
$tel_value = $_POST['cat-tel']; // value
// url
$url_key = 'cat-url-'.$term_id;
$url_value = $_POST['cat-url'];
// 更新选项值
update_option( $tel_key, $tel_value );
update_option( $url_key, $url_value );
}
}
// 虽然要两个钩子,但是我们可以两个钩子使用同一个函数
add_action('created_category','ems_taxonomy_metadate',10,1);
add_action('edited_category','ems_taxonomy_metadate',10,1);
?>调用方法
<?php
// 取出当前分类 id: $categories[0]->term_id
$categories = get_the_category();
$term_id = $categories[0]->term_id;
$cat_name = $categories[0]->name;
?>
<p class="phone">
<p class="p1"><?php echo get_option('cat-tel-'.$term_id);?></p>
<p class="p2"><a href="<?php echo get_option('cat-url-'.$term_id);?> " target="_blank"><?php echo $cat_name;?></a></p>
</p>【相关教程推荐】
1. php编程从入门到精通全套视频教程
2. php从入门到精通
3. bootstrap教程
全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号