Wordpress增加栏目分类SEO标题关键字描述
Wordpress没有常用的SEO三要素设置:标题(Title)、关键字(Keywords)、描述(Discription)等,不利于对网站排名,需要进行一定的PHP开发。在添加、修改分类时,可以创建这三要素。
把下面的代码直接复制到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”自行安装即可,功能比较强大,大家根据自己的喜好选择就好。
相关文章
- Wordpress栏目用分页插件与其他插件冲突出现404错误解决办法
- Wordpress首页列表页文章根据权重倒序排序
- Wordpress文章缩略图特色图片the_post_thumbnail函数应用
- Wordpress默认编辑器添加文字大小、字体
- Wordpress设置上传图片默认使用全尺寸
- Wordpress自定义字段插件Advanced Custom Fields使用及调用字段亲测有效
- Wordpress函数is_home判断是否首页失效及处理办法
- Wordpress插件contact form 7设置占位符描述文本placeholder
- Wordpress在任意页面调用联系表单简码
- Wordpress反复中毒,如何有效防止利用漏洞安装文件管理插件
- Wordpress调用指定分类有某TAG标签的文章
- Wordpress禁止指定用户名注册及增加邮箱验证