当前位置:
测速网 >
cms教程 > 发布时间:2023-09-26 09:54 文章来源于网友投稿,仅供参考!
给wordpress分类添加字段
wordpress分类字段功能对于综合类网站来讲,是一个非常灵活的调用功能。可以根据网站不同的需求来调用不同的字段选项;下面给大家介绍一款非常实力的函数代码;是用来调用电话及联络方式的,各位可以根据自己的需求对代码进行调整和修改;测速网用来修改成了wordpress主题分类的标签id调用;亲测可用的;
1. 在wordpress主题模板目录下的主函数文本代码 function.PHP 里添加包含 页面 category_field.php
包括代码如下:
12// 分类添加字段require_once( dirname(__FILE__).'/category_field.php' );
2. 新建 category_field.php 页面
代码如下:
161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566<?php // 分类添加字段function ems_add_category_field(){echo '<div ><label for="cat-tel">Tel</label><input name="cat-tel" id="cat-tel" type="text" value="" size="40"><p>The telephone.</p></div>';echo '<div ><label for="cat-url">URL</label><input name="cat-url" id="cat-url" type="text" value="" size="40"><p>The URL.</p></div>';}add_action('category_add_form_fields','ems_add_category_field',10,2);// 分类编辑字段function ems_edit_category_field($tag){echo '<tr ><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 >'.$tag->name.' on the phone.</span></td></tr>';echo '<tr ><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 >'.$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);?>
前台字段调用方法
12345678910<?php // 取出当前分类 id: $categories[0]->term_id$categories = get_the_category();$term_id = $categories[0]->term_id;$cat_name = $categories[0]->name;?><div ><div ><?php echo get_option('cat-tel-'.$term_id);?></div><div ><a href="<?php%20echo%20get_option('cat-url-'.$term_id);?>%20" target="_blank"><?php echo $cat_name;?></a></div></div>
上一篇:wordpress安装主题插件需要输入ftp
下一篇:解决wordpress主题functions.php内的恶意代码