wordpress分类栏目添加自定义字段 (例如栏目图片)

2023-05-2113:28:54后端程序开发Comments1,055 views字数 3094阅读模式

1.添加方法,将下方代码复制到 function.php 中文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

<?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' ) );文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

// 保存自定义字段数据
add_action( 'edited_category', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_category', array( $this, 'save_tax_meta' ), 10, 2 );文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

// 新建分类页面添加自定义字段输入框文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

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>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

<!-- TODO: 在这里追加其他自定义字段表单,如: -->文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

<!--
<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文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

/**
* 编辑分类页面添加自定义字段输入框
*
* @uses get_option() 从option表中获取option数据
* @uses esc_url() 确保字符串是url
*/
public function edit_tax_image_field( $term ){文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

// $term_id 是当前分类的id
$term_id = $term->term_id;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

// 获取已保存的option
$term_meta = get_option( "ludou_taxonomy_$term_id" );
// option是一个二维数组
$image = $term_meta['tax_image'] ? $term_meta['tax_image'] : '';文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

/**
* 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 -->文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

<!-- TODO: 在这里追加其他自定义字段表单,如: -->文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

<!--
<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>
-->文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

<?php
} // edit_tax_image_field文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

/**
* 保存自定义字段的数据
*
* @uses get_option() 从option表中获取option数据
* @uses update_option() 更新option数据,如果没有就新建option
*/
public function save_tax_meta( $term_id ){文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

if ( isset( $_POST['term_meta'] ) ) {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

// $term_id 是当前分类的id
$t_id = $term_id;
$term_meta = array();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

// 获取表单传过来的POST数据,POST数组一定要做过滤
$term_meta['tax_image'] = isset ( $_POST['term_meta']['tax_image'] ) ? esc_url( $_POST['term_meta']['tax_image'] ) : '';文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

/**
* TODO: 在这里追加获取其他自定义字段表单的值,如:
* $term_meta['tax_keywords'] = isset ( $_POST['term_meta']['tax_keywords'] ) ? $_POST['term_meta']['tax_keywords'] : '';
*/文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

// 保存option数组
update_option( "ludou_taxonomy_$t_id", $term_meta );文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

} // if isset( $_POST['term_meta'] )
}
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

$wptt_tax_image = new Ludou_Tax_Image();
2.模板中调用方法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

//$cat 默认为当前分类id seo-title自定义字段
<?
$post_id = "category_".$cat;
$value = get_field( 'seo-title', $post_id );
echo $value;
?>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

//输出图片字段
<? $post_id = "category_".$cat; echo get_field('img_ioc',$post_id);?>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

//案例
<? $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文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/40988.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/40988.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定