欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」WordPress高效的SEO优化方法,非插件,适用于英文站

作者 : 自学哈 本文共2689个字,预计阅读时间需要7分钟 2022-11-6 共121人阅读
也想出现在这里? 联系我们

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 = '';    // description    $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 (!empty($seo_desc)) {            $content = $seo_desc;        } else if (!empty($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";    // keywords    $keys = get_post_meta($post->ID, 'mm_seo_keywords', true);    $cats = get_the_category();    $tags = get_the_tags();    if (empty($keys)) {        if (!empty($cats)) foreach($cats as $cat) $keys .= $cat->name . ', ';        if (!empty($tags)) foreach($tags as $tag) $keys .= $tag->name . ', ';        $keys .= $default_keywords;    }    $output .= "tt" . '<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 .=  "tt" . '<meta name="robots" content="noindex,follow">' . "n";        } else {            $output .=  "tt" . '<meta name="robots" content="index,follow">' . "n";        }    } else if (is_home() || is_singular()) {        $output .=  "tt" . '<meta name="robots" content="index,follow">' . "n";    } else {        $output .= "tt" . '<meta name="robots" content="noindex,follow">' . "n";    }    // title    $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 (!empty($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;    else                              $seo_title = $name . ' | ' . $description;    $output .= "tt" . '<title>' . esc_attr($seo_title . $page_number) . '</title>' . "n";    return $output;}

需修改一下$default_keywords 后面默认的关键字

使用方法:用下面代码:

<?php echo basic_wp_seo(); ?>

替换主题header.php模板

<title></title>  

注:可能不同的主题有所区别

可惜这段SEO代码对中文支持不好

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」WordPress高效的SEO优化方法,非插件,适用于英文站
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号