Home > php教程 > php手册 > body text

如何给 wordpress Category添加字段(附代码)

PHPz
Release: 2018-10-22 17:20:22
forward
1452 people have browsed it

这篇文章主要介绍了如何给 wordpress Category添加字段,有一定的参考价值,感兴趣的朋友可以参考一下。

1.  在模板目录下的 function.php 里添加包含 页面 category_field.php

包括代码如下:

// 分类添加字段
require_once( dirname(__FILE__).'/category_field.php' );
Copy after login

2.  新建 category_field.php 页面

代码如下:

<?php 
// 分类添加字段
function ems_add_category_field(){
	echo &#39;<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>
		  </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="&#39;;
				echo get_option(&#39;cat-tel-&#39;.$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="&#39;;
				echo get_option(&#39;cat-url-&#39;.$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);
?>
Copy after login

调用方法

<?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(&#39;cat-tel-&#39;.$term_id);?></p>
  <p class="p2"><a href="<?php echo get_option(&#39;cat-url-&#39;.$term_id);?> " target="_blank"><?php echo $cat_name;?></a></p>
</p>
Copy after login

【相关教程推荐】

1. php编程从入门到精通全套视频教程
2. php从入门到精通 
3. bootstrap教程

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!