wordpress分类栏目添加自定义字段 (例如栏目图片)
1.添加方法,将下方代码复制到 function.php 中
<?php
class Ludou_Tax_Image{
function __construct(){
// 新建分类页面添加自定义字段输入框
add_action( 'category_add_form_fields', array( $this, 'add_tax_image_field' ) );
// 编辑分类页面添加自定义字段输入框
add_action( 'category_edit_form_fields', array( $this, 'edit_tax_image_field' ) );
// 保存自定义字段数据
add_action( 'edited_category', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_category', array( $this, 'save_tax_meta' ), 10, 2 );
}
// 新建分类页面添加自定义字段输入框
public function add_tax_image_field(){
?>
<div class="form-field">
<label for="term_meta[tax_image]">分类封面</label>
<input type="text" name="term_meta[tax_image]" id="term_meta[tax_image]" value="" />
<p class="description">输入分类封面图片URL</p>
</div>
<!-- TODO: 在这里追加其他自定义字段表单,如: -->
<!--
<div class="form-field">
<label for="term_meta[tax_keywords]">分类关键字</label>
<input type="text" name="term_meta[tax_keywords]" id="term_meta[tax_keywords]" value="" />
<p class="description">输入分类关键字</p>
</div>
-->
<?php
} // add_tax_image_field
/**
* 编辑分类页面添加自定义字段输入框
*
* @uses get_option() 从option表中获取option数据
* @uses esc_url() 确保字符串是url
*/
public function edit_tax_image_field( $term ){
// $term_id 是当前分类的id
$term_id = $term->term_id;
// 获取已保存的option
$term_meta = get_option( "ludou_taxonomy_$term_id" );
// option是一个二维数组
$image = $term_meta['tax_image'] ? $term_meta['tax_image'] : '';
/**
* TODO: 在这里追加获取其他自定义字段值,如:
* $keywords = $term_meta['tax_keywords'] ? $term_meta['tax_keywords'] : '';
*/
?>
<tr class="form-field">
<th scope="row">
<label for="term_meta[tax_image]">分类封面</label>
<td>
<input type="text" name="term_meta[tax_image]" id="term_meta[tax_image]" value="<?php echo esc_url( $image ); ?>" />
<p class="description">输入分类封面图片URL</p>
</td>
</th>
</tr><!-- /.form-field -->
<!-- TODO: 在这里追加其他自定义字段表单,如: -->
<!--
<tr class="form-field">
<th scope="row">
<label for="term_meta[tax_keywords]">分类关键字</label>
<td>
<input type="text" name="term_meta[tax_keywords]" id="term_meta[tax_keywords]" value="<?php echo $keywords; ?>" />
<p class="description">输入分类关键字</p>
</td>
</th>
</tr>
-->
<?php
} // edit_tax_image_field
/**
* 保存自定义字段的数据
*
* @uses get_option() 从option表中获取option数据
* @uses update_option() 更新option数据,如果没有就新建option
*/
public function save_tax_meta( $term_id ){
if ( isset( $_POST['term_meta'] ) ) {
// $term_id 是当前分类的id
$t_id = $term_id;
$term_meta = array();
// 获取表单传过来的POST数据,POST数组一定要做过滤
$term_meta['tax_image'] = isset ( $_POST['term_meta']['tax_image'] ) ? esc_url( $_POST['term_meta']['tax_image'] ) : '';
/**
* TODO: 在这里追加获取其他自定义字段表单的值,如:
* $term_meta['tax_keywords'] = isset ( $_POST['term_meta']['tax_keywords'] ) ? $_POST['term_meta']['tax_keywords'] : '';
*/
// 保存option数组
update_option( "ludou_taxonomy_$t_id", $term_meta );
} // if isset( $_POST['term_meta'] )
}
}
$wptt_tax_image = new Ludou_Tax_Image();
2.模板中调用方法:
//$cat 默认为当前分类id seo-title自定义字段
<?
$post_id = "category_".$cat;
$value = get_field( 'seo-title', $post_id );
echo $value;
?>
//输出图片字段
<? $post_id = "category_".$cat; echo get_field('img_ioc',$post_id);?>
//案例
<? $post_id = "category_".$cat; ?>
<title><?php echo get_field( 'seo-title', $post_id ); ?></title>
<meta name="keywords" content="<?php echo get_field( 'seo-keywords', $post_id ); ?>"/>
<meta name="description" content="<?php echo get_field( 'seo-description', $post_id ); ?>"/>
————————————————
版权声明:本文为CSDN博主「程序小小生」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cnpinpai/article/details/128093567