wordpress给分类添加seo标题关键词和描述字段并调用
原生的wordpress程序中,新建的分类中是没有填写seo标题关键词和描述的,出于优化考虑,很多时候我们需要给分类添加独立的字段,使其可以独立添加title、description、keywords,也就是我们说的三大标签。
这样的方式既不会影响导航也不会影响栏目的链接,实现方法如下:
1、在主题文件目录创建一个seo.php的文件,将下面的代码复制到文件中并保存。
<?php //给分类目录添加 SEO标题、关键词、描述 //添加页面 挂载字段 add_action( 'category_add_form_fields', 'category_term_field' );//分类 add_action( 'post_tag_add_form_fields', 'category_term_field' );//标签 function category_term_field() { wp_nonce_field( basename( __FILE__ ), 'category_term_field_nonce' ); //wp_enqueue_script('dreamc_term_fields', get_template_directory_uri(). '/js/termmeta-upload.js'); echo '<div class="form-field category-term-field">'; echo '<label for="category-term-seo_title">SEO标题</label>'; echo '<input type="text" name="category_term_seo_title" id="category-term-seo_title" value="" />'; echo '</div>'; echo '<div class="form-field category-term-field">'; echo '<label for="category-term-seo_keywords">SEO关键词</label>'; echo '<textarea name="category_term_seo_keywords" id="category-term-seo_keywords"></textarea>'; echo '</div>'; echo '<div class="form-field category-term-field">'; echo '<label for="category-term-seo_description">SEO描述</label>'; echo '<textarea name="category_term_seo_description" id="category-term-seo_description"></textarea>'; echo '</div>'; } //分类扩展信息 编辑界面 add_action( 'category_edit_form_fields', 'edit_category_term_field' );//分类 add_action( 'post_tag_edit_form_fields', 'edit_category_term_field' );//标签 function edit_category_term_field( $term ) { //获取数据 $category_title = get_term_meta( $term->term_id, 'category_seo_title', true ); $category_keywords = get_term_meta( $term->term_id, 'category_seo_keywords', true ); $category_des = get_term_meta( $term->term_id, 'category_seo_des', true ); echo '<tr class="form-field category-term-field-wrap">'; echo '<th scope="row"><label for="category-term-title">SEO标题</label></th>'; echo '<td>'; echo wp_nonce_field( basename( __FILE__ ), 'category_term_field_nonce' ); echo '<input type="text" name="category_term_title" id="category-term-title" value="'.$category_title.'"/>'; echo '</td>'; echo '</tr>'; echo '<tr class="form-field category-term-field-wrap">'; echo '<th scope="row"><label for="category-term-keywords">SEO关键词</label></th>'; echo '<td>'; echo '<textarea name="category_term_keywords" id="category-term-keywords">'.$category_keywords.'</textarea>'; echo '</td>'; echo '</tr>'; echo '<tr class="form-field category-term-field-wrap">'; echo '<th scope="row"><label for="category-term-des">SEO描述</label></th>'; echo '<td>'; echo '<textarea name="category_term_des" id="category-term-des">'.$category_des.'</textarea>'; echo '</td>'; echo '</tr>'; } //保存数据 add_action( 'create_category', 'save_category_term_field' ); add_action( 'edit_category', 'save_category_term_field' );//分类 add_action( 'create_post_tag', 'save_category_term_field' ); add_action( 'edit_post_tag', 'save_category_term_field' );//标签 function save_category_term_field( $term_id ) { if ( ! isset( $_POST['category_term_field_nonce'] ) || ! wp_verify_nonce( $_POST['category_term_field_nonce'], basename( __FILE__ ) ) ) return; //获取 $category_title = isset( $_POST['category_term_title'] ) ? $_POST['category_term_title'] : ''; $category_keywords = isset( $_POST['category_term_keywords'] ) ? $_POST['category_term_keywords'] : ''; $category_des = isset( $_POST['category_term_des'] ) ? $_POST['category_term_des'] : ''; //更新 if( '' === $category_title){delete_term_meta( $term_id, 'category_seo_title' );}else{update_term_meta( $term_id, 'category_seo_title', $category_title );} if( '' === $category_keywords){delete_term_meta( $term_id, 'category_seo_keywords' );}else{update_term_meta( $term_id, 'category_seo_keywords', $category_keywords );} if( '' === $category_des){delete_term_meta( $term_id, 'category_seo_des' );}else{update_term_meta( $term_id, 'category_seo_des', $category_des );} } ;?>
2、然后打开该目录中的functions.php文件,加上如下代码:
//调用seo信息 include("seo.php");
3、最后在分类模板中使用如下代码调用相关字段
<title><?php echo get_term_meta( $cat, 'category_seo_title', true );?></title> <meta name="keywords" content="<?php echo get_term_meta( $cat, 'category_seo_keywords', true );?>" /> <meta name="description" content="<?php echo get_term_meta( $cat, 'category_seo_des', true );?>" />
这样就实现了wordpress分类单独分类的seo标题关键词和描述了!
版权保护: 本文由小冬SEO编辑发布,转载请保留链接: http://www.myseoyh.cn/shuo/113.html