<?php
//自定义分类标题
class zm_wp_title{
function __construct(){
// 分类
add_action( 'category_add_form_fields', array( $this, 'add_tax_title_field' ) );
add_action( 'category_edit_form_fields', array( $this, 'edit_tax_title_field' ) );
add_action( 'edited_category', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_category', array( $this, 'save_tax_meta' ), 10, 2 );
// 标签
add_action( 'post_tag_add_form_fields', array( $this, 'add_tax_title_field' ) );
add_action( 'post_tag_edit_form_fields', array( $this, 'edit_tax_title_field' ) );
add_action( 'edited_post_tag', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_post_tag', array( $this, 'save_tax_meta' ), 10, 2 );
}
public function add_tax_title_field(){
?>
<div class="form-field term-title-wrap">
<label for="term_meta[tax_zm_title]">自定义标题</label>
<input type="text" name="term_meta[tax_zm_title]" id="term_meta[tax_zm_title]" value="" />
<p class="description">搜索引擎优化自定义标题,不填写即为默认标题</p>
</div>
<?php
} // add_tax_title_field
public function edit_tax_title_field( $term ){
$term_id = $term->term_id;
$term_meta = get_option( "zm_taxonomy_$term_id" );
$zm_title = $term_meta['tax_zm_title'] ? $term_meta['tax_zm_title'] : '';
?>
<tr class="form-field term-title-wrap">
<th scope="row">
<label for="term_meta[tax_zm_title]">自定义标题</label>
<td>
<input type="text" name="term_meta[tax_zm_title]" id="term_meta[tax_zm_title]" value="<?php echo $zm_title; ?>" />
<p class="description">搜索引擎优化自定义标题,不填写即为默认标题</p>
</td>
</th>
</tr>
<?php
} // edit_tax_title_field
public function save_tax_meta( $term_id ){
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = array();
$term_meta['tax_zm_title'] = isset ( $_POST['term_meta']['tax_zm_title'] ) ? $_POST['term_meta']['tax_zm_title'] : '';
update_option( "zm_taxonomy_$t_id", $term_meta );
} // if isset( $_POST['term_meta'] )
} // save_tax_meta
} // zm_wp_title
$wptt_tax_title = new zm_wp_title();
function the_zm_title() {
$category = get_the_category();
$term_id = $category[0]->cat_ID;
$term_meta = get_option( "zm_taxonomy_$term_id" );
$tax_zm_title = $term_meta['tax_zm_title'] ? $term_meta['tax_zm_title'] : '';
echo $tax_zm_title;
}
function get_current_tag_id() {
$current_tag = single_tag_title('', false);
$tags = get_tags();
foreach($tags as $tag) {
if($tag->name == $current_tag) return $tag->term_id;
}
}
function zm_tag_title() {
$term_id = get_current_tag_id();
$term_meta = get_option( "zm_taxonomy_$term_id" );
$zm_tag_title = $term_meta['tax_zm_title'] ? $term_meta['tax_zm_title'] : '';
echo $zm_tag_title;
}
?>