wordpress自定义设置SEO标题、关键词和描述

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

wordpress自定义设置SEO标题、关键词和描述,把如下代码放到模板目录functions.php里即可:


/**
 * 设置SEO标题
 * @author albert
 * @date 2021-06-21
 */
function wp_seo_title() {
    global $s, $post;
	$category = get_the_category();//默认获取当前所属分类
	$seo_title = get_term_meta($category[0]->cat_ID, 'category_seo_title', true );
    echo $seo_title;
}

/**
 * 设置关键词
 * @author albert
 * @date 2021-06-02
 */
function wp_keywords() {
    global $s, $post;
    $keywords = '';
    if (is_single ()) {  //如果是文章页,关键词则是:标签+分类ID
        if (get_the_tags ( $post->ID )) {
            foreach ( get_the_tags ( $post->ID ) as $tag )
                $keywords .= $tag->name . ', ';
        }
        foreach ( get_the_category ( $post->ID ) as $category )
            $keywords .= $category->cat_name . ', ';
        $keywords = substr_replace ( $keywords, '', - 2 );
    } elseif (is_home ()) {
        $keywords = '';  //主页关键词设置,自己设置
    } elseif (is_tag ()) {  //标签页关键词设置
        $keywords = single_tag_title ( '', false );
    } elseif (is_category ()) {//分类页关键词设置
		$category = get_the_category();//默认获取当前所属分类
		$keywords = get_term_meta($category[0]->cat_ID, 'category_seo_keywords', true );
        // $keywords = single_cat_title ( '', false );
    } elseif (is_search ()) {//搜索页关键词设置
        $keywords = esc_html ( $s, 1 );
    } else {//默认页关键词设置
        $keywords = trim ( wp_title ( '', false ) );
    }
    if ($keywords) {  //输出关键词
        echo $keywords;
    }
}

/**
 * 设置描述
 * @author albert
 * @date 2021-06-02
 */
function wp_description() {
    global $s, $post;
    $description = '';
    $blog_name = get_bloginfo ( 'name' );
    if (is_singular ()) {  //文章页如果存在描述字段,则显示描述,否则截取文章内容
        if (! empty ( $post->post_excerpt )) {
            $text = $post->post_excerpt;
        } else {
            $text = $post->post_content;
        }
        $description = trim ( str_replace ( array (
                "rn",
                "r",
                "n",
                " ",
                " " 
        ), " ", str_replace ( "", "'", strip_tags ( $text ) ) ) );
        if (! ($description))
            $description = $blog_name . "-" . trim ( wp_title ( '', false ) );
    } elseif (is_home ()) {//首页显示描述设置
        $description = get_bloginfo ( 'description' ); // 首页要显示的描述,首頁要自己加
    } elseif (is_tag ()) {//标签页显示描述设置
        $description = $blog_name . "about '" . single_tag_title ( '', false ) . "' articles";
    } elseif (is_category ()) {//分类页显示描述设置
		$category = get_the_category();//默认获取当前所属分类
		$description = get_term_meta($category[0]->cat_ID, 'category_seo_des', true );
        // $description = $blog_name . "about '" . single_cat_title ( '', false ) . "' articles";
    } elseif (is_archive ()) {//文档页显示描述设置
        $description = $blog_name . "in: '" . trim ( wp_title ( '', false ) ) . "' articles";
    } elseif (is_search ()) {//搜索页显示描述设置
        $description = $blog_name . ": '" . esc_html ( $s, 1 ) . "' result";
    } else {//默认其他页显示描述设置
        $description = $blog_name . " about '" . trim ( wp_title ( '', false ) ) . "' articles";
    }
    //输出描述
    $description = mb_substr ( $description, 0, 220, 'utf-8' );
    echo $description;
}

add_action ( 'wp_head', 'wp_keywords' ); // 添加关键字
add_action ( 'wp_head', 'wp_description' ); // 添加页面描述



点赞 (0)

收藏 (0)

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

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

下一篇: wordpress设置统计文章浏览次数的代码

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

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

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