wordpress给分类添加:前后台SEO标题、关键词和描述的功能代码

知识技术 2021-08-26 22:41 1094
转载请务必注明: 来源:《建站管家》https://www.df81.com/news/219.html

wordpress给分类添加前后台SEO标题、关键词和描述的功能,把如下代码加到模板所在目录的functions.php里即可:


// albert@20210621
// 给分类设置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>';
}

// albert@20210621
// 给分类设置SEO标题,关键词、描述(后台)
// //分类扩展信息 编辑界面
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>';
}

// albert@20210621
//保存数据
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 );}
}



点赞 (0)

收藏 (0)

本站部分信息来源网络,如有侵权,请联系QQ:1062129401删除。

上一篇: 《建站管家》二次开发文档

下一篇: wordpress去掉图片宽高及size属性以自适应手机的功能代码

请先登录后,再发表评论 ~ ~
发表评论
发表评论
发表帖子
广告位招租( ¥5 / 天 )
点击咨询
相关帖子

如果要判断文件是否存在,用函数 is_file(),如果要判断目录是否存在,用函数 is_dir()
用宝塔安装TP程序后,网站前台可以访问,后台无法访问,不是程序问题,而是web服务器环境配置问题(PHP配置),出现这种问题,一般是windows主机安装宝塔造成的,linux安装宝塔不会出现此错误
广告位招租( ¥3 / 天 )
点击咨询
广告位招租( ¥2 / 天 )
点击咨询
广告位招租( ¥1 / 天 )
点击咨询
最新帖子

小朋友,你爸爸是谁啊?这很单纯的,就像我们碰到别人会问:你吃过饭了吗?类似;许多人初次相识,经常会问:你是做什么的啊?你做什么,好像决定你收入的高低,你,资产的范围
1、升级框架到ThinkPHP8.0.3【要求PHP8.1+环境,推荐PHP8.1】 2、启用/禁用调试模式等小细节改进
后台点击生成站点地图 一直转圈圈没有反应,
在线客服