为WordPress的首页及内页每篇文章自动添加关键词(keywords)和描述(description)

  前面已经介绍了一种添加关键词(keywords)和描述(description)的方法,但仅限于添加首页的关键词(keywords)和描述(description),这里则是在每一篇文章的页面上添加关键词(keywords)和描述(description)。方法即在
<title><?php wp_title(‘&laquo;’, true, ‘right’); ?> <?php bloginfo(‘name’); ?></title>

<link rel=”stylesheet” href=”<?php bloginfo(‘stylesheet_url’); ?>” type=”text/css” media=”screen” />
之间添加:

    <?php if (is_home()){
    $description = “凸鱼个人博客:专注于网站前端与股市前沿,力求于将股市的飘逸与不羁融入网站前端的开发和营销策划,用网站前端开发和营销策划的艺术去指导股市的投资。”;
    $keywords = “凸鱼 网站前端 股市前沿 网站策划 网站运营 网络营销 股票K线 股票学习 股票分析”;
    }
    elseif (is_single()){
    if ($post->post_excerpt) {
    $description = $post->post_excerpt;
    }
    elseif( function_exists(‘wp_thumbnails_excerpt’)){
    $description = wp_thumbnails_excerpt($post->post_content, true);
    }
    else {
    $description = $post->post_title;
    }
    $keywords = “”;
    $tags = wp_get_post_tags($post->ID);
    foreach ($tags as $tag ) {
    $keywords = $keywords . “,” . $tag->name;
    }
    }
    elseif(is_category()){
    $description = category_description();
    }
    echo “<meta name=\”keywords\” content=\”$keywords\” />
    <meta name=\”description\” content=\”$description\” />”;
    ?>

  上述代码的主要原理是将Wordpress后台添加文章时的摘要转化为描述(description),将标签转化为关键词(keywords)。因此,添加新文章时只需每次添加好摘要和标签,关键词(keywords)和描述(description)就自动生成了。

4 comments

Leave a Reply to 厦门团购网 Cancel Reply

Required fields are marked *. Your email address will not be published.