wordpress正确调用TDK的方法(title,description,keywords)

烈酒焚心 提交于 2019-11-28 07:50:55

wordpress不用插件调用页面的的tdk(title,description,keywords),自动判断当前页面位置,并进行合理的TKD布局优化,对搜索引擎更友好。将下面代码添加到wordpress主题的header.php

<?php if ( is_home() ) { ?><title><?php bloginfo('name'); ?> | <?php bloginfo('description'); ?></title><?php } ?>
<?php if ( is_search() ) { ?><title>搜索结果 | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_single() ) { ?><title><?php echo trim(wp_title('',0)); ?><?php if (get_query_var('page')) { echo '-第'; echo get_query_var('page'); echo '页';}?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_page() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_category() ) { ?><title><?php single_cat_title(); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_year() ) { ?><title><?php the_time('Y年'); ?>所有文章 | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_month() ) { ?><title><?php the_time('F'); ?>份所有文章 | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_day() ) { ?><title><?php the_time('Y年n月j日'); ?>所有文章 | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_404() ) { ?><title>亲,你迷路了! | <?php bloginfo('name'); ?></title><?php } ?>
<?php if (function_exists('is_tag')) { if ( is_tag() ) { ?><title><?php  single_tag_title("", true); ?> | <?php bloginfo('name'); ?></title><?php } ?><?php } ?>
<?php if ( is_tax('notice') ) { ?><title><?php setTitle(); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php
if (!function_exists('utf8Substr')) {
    function utf8Substr($str, $from, $len) {
        return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$str);
    }
}
if ( is_single() ) {
    if ($post->post_excerpt) {
        $description = $post->post_excerpt;
    } else {
    if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){
        $post_content = $result['1'];
    } else {
        $post_content_r = explode("\n",trim(strip_tags($post->post_content)));
        $post_content   = $post_content_r['0'];
    }
        $description = utf8Substr($post_content,0,220);
    }
        $keywords = "";
        $tags     = wp_get_post_tags($post->ID);
        foreach ($tags as $tag ) {
            $keywords = $keywords . $tag->name . ",";
    }
}
?>
<?php echo "\n"; ?>
<?php if ( is_single() ) { ?>
    <meta name="description" content="<?php echo trim($description); ?>" />
    <meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />
<?php } ?>
<?php if ( is_page() ) { ?>
    <meta name="description" content="<?php $description = get_post_meta($post->ID, 'description', true);{echo $description;}?>" />
    <meta name="keywords" content="<?php $keywords = get_post_meta($post->ID, 'keywords', true);{echo $keywords;}?>" />
<?php } ?>
<?php if ( is_category() ) { ?>
    <meta name="description" content="<?php echo category_description( $categoryID ); ?>" />
<?php } ?>
<?php if ( is_tag() ) { ?>
    <meta name="description" content="<?php echo single_tag_title(); ?>" />
<?php } ?>
<?php if ( is_home() ) { ?>
    <meta name="description" content="<?php echo get_option('首页文字描述'); ?>" />
    <meta name="keywords" content="<?php echo get_option('首页关键字'); ?>" />
<?php } ?>

  

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!