不用插件就能解决wordpress的SEO,修改functions.php加入如下代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-11-21 10:52:59 浏览: 评论:0
不用插件就能解决wordpress的SEO,修改functions.php加入如下代码即可.
很多人使用插件对wordpress进行SEO优化,其实很多是没有必要用到的,这里为大家提供了一个通用的wordpress 页面SEO设置方法.
将以下代码放置于你主题的functions.php文件中:
- function basic_wp_seo() {
- global $page, $paged, $post;
- $default_keywords = 'wordpress, plugins, themes, design, dev, development, security, htaccess, apache, php, sql, html, css, jquery, javascript, tutorials'; // customize
- $output = '';
- // 描述
- $seo_desc = get_post_meta($post->ID, 'mm_seo_desc', true);
- $description = get_bloginfo('description', 'display');
- $pagedata = get_post($post->ID);
- if (is_singular()) {
- if (!emptyempty($seo_desc)) {
- $content = $seo_desc;
- } else if (!emptyempty($pagedata)) {
- $content = apply_filters('the_excerpt_rss', $pagedata->post_content);
- $content = substr(trim(strip_tags($content)), 0, 155);
- $content = preg_replace('#\n#', ' ', $content);
- $content = preg_replace('#\s{2,}#', ' ', $content);
- $content = trim($content);
- }
- } else {
- $content = $description;
- }
- $output .= '<meta name="description" content="' . esc_attr($content) . '">' . "\n";
- // 关键字
- $keys = get_post_meta($post->ID, 'mm_seo_keywords', true);
- $cats = get_the_category();
- $tags = get_the_tags();
- if (emptyempty($keys)) {
- if (!emptyempty($cats)) foreach($cats as $cat) $keys .= $cat->name . ', ';
- if (!emptyempty($tags)) foreach($tags as $tag) $keys .= $tag->name . ', ';
- $keys .= $default_keywords;
- }
- $output .= "\t\t" . '<meta name="keywords" content="' . esc_attr($keys) . '">' . "\n";
- // robots设置
- if (is_category() || is_tag()) {
- $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
- if ($paged > 1) {
- $output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n";
- } else {
- $output .= "\t\t" . '<meta name="robots" content="index,follow">' . "\n";
- }
- } else if (is_home() || is_singular()) {
- $output .= "\t\t" . '<meta name="robots" content="index,follow">' . "\n";
- } else {
- $output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n";
- }
- // 标题
- $title_custom = get_post_meta($post->ID, 'mm_seo_title', true);
- $url = ltrim(esc_url($_SERVER['REQUEST_URI']), '/');
- $name = get_bloginfo('name', 'display');
- $title = trim(wp_title('', false));
- $cat = single_cat_title('', false);
- $tag = single_tag_title('', false);
- $search = get_search_query();
- if (!emptyempty($title_custom)) $title = $title_custom;
- if ($paged >= 2 || $page >= 2) $page_number = ' | ' . sprintf('Page %s', max($paged, $page));
- else $page_number = '';
- if (is_home() || is_front_page()) $seo_title = $name . ' | ' . $description;
- elseif (is_singular()) $seo_title = $title . ' | ' . $name;
- elseif (is_tag()) $seo_title = 'Tag Archive: ' . $tag . ' | ' . $name;
- elseif (is_category()) $seo_title = 'Category Archive: ' . $cat . ' | ' . $name;
- elseif (is_archive()) $seo_title = 'Archive: ' . $title . ' | ' . $name;
- elseif (is_search()) $seo_title = 'Search: ' . $search . ' | ' . $name;
- elseif (is_404()) $seo_title = '404 - Not Found: ' . $url . ' | ' . $name;//开源软件:phpfensi.com
- else $seo_title = $name . ' | ' . $description;
- $output .= "\t\t" . '<title>' . esc_attr($seo_title . $page_number) . '</title>' . "\n";
- return $output;
- }
以上方法包含了标题、关键字、描述、robots设置,你在需要使用的页面添加以上函数.
以上函数可以在:主页、文章页、标签页、分类页、归档页、搜索页、404页面使用,如果你需要其他页面,可以修改以上代码实现.
- <?php echo basic_wp_seo(); ?>
Tags: wpseo wordpress优化
相关文章
- ·wordpress打开慢优化方法(2014-06-21)
- ·完美优化wordpress网站10步加速优化曲(2015-02-27)
- ·wordpress做的网站SEO优化细节(2015-09-23)
- ·wordpress 优化 – 解决定时发布失败(2015-09-24)
- ·wordpress 优化 – gravatar问题(2015-09-24)
- ·wordpress 优化 – google字体问题(2015-09-24)
- ·关于WordPress的SEO优化相关的一些PHP页面脚本技巧(2021-06-29)
- ·几个优化WordPress中JavaScript加载体验的插件介绍(2021-06-30)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)