只做利于SEO的网站,提供网站建设、SEO、网站代运营等服务。服务中心 | 建站流程 | 网站地图

Wordpress增加栏目分类SEO标题关键字描述

2021-10-14小猴建站 阅读()相关主题:wordpress

Wordpress没有常用的SEO三要素设置:标题(Title)、关键字(Keywords)、描述(Discription)等,不利于对网站排名,需要进行一定的PHP开发。在添加、修改分类时,可以创建这三要素。

wp增加seo三要素

把下面的代码直接复制到Wordpress主题的functions.php文件的最后即可

后台添加SEO三要素功能

//给分类目录添加 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 );}
}

模板调用方法

<?php echo get_option('seotitle');?>
<?php echo get_option('seokeywords');?>
<?php echo get_option('seodescription');?>

当然也有人专门写了插件,可以搜索一下“Smart SEO Tool”自行安装即可,功能比较强大,大家根据自己的喜好选择就好。