Home Backend Development PHP Tutorial 关于WordPress的SEO优化相关的一些PHP页面脚本技巧_PHP

关于WordPress的SEO优化相关的一些PHP页面脚本技巧_PHP

May 28, 2016 pm 01:13 PM
php wordpress

随着搜索引擎大兴, 排列在前的网站引入大量流量. 无论是搜索页面的广告还是查出来的结果, 与搜索者的目标匹配度都比较高 (如果搜索引擎足够智能), 所以通过搜索引擎而来的访客很可能会从网站上得到他想要的东西, 并记住这个网站. 也就是说, 搜索引擎会带来很多有价值的流量, 所以花点时间针对搜索引擎优化一下 WordPress 博客也是值得的. 本文将为你分享一些 WordPress SEO 技巧.

以前我没有花太多时间做搜索引擎优化, 搜索引擎收录效果不是很好. 去年二月我终于为博客搞了一下 SEO, 写下这个文章. 一年半以来陆续进行了更多 SEO 优化, 博客文章写得不多了, 流量却有增无减, 网站也重回 PageRank 7, 效果相当不错的. 这次我根据自己的 WordPress SEO 方案更新了本文的内容.

20151210160816577.png (708×431)

优化博客副标题

副标题 (slogan), WordPress 中称为 tagline. 它与博客标题不一样, 可能会承载一些描述博客的文字, 优化一下可以利用起来. 如我的副标题是 "mg12's Blog - Just Another WordPress Blog", 其中的 WordPress Blog 被我设置为 h1. 因为我想告诉爬虫, 这是一个关于 WordPress 的博客.

区分显示页面标题

页面的标题不要包含博客名称. WordPress 标题一般会用到 bloginfo('name') 和 wp_title() 两部分, 前者是博客名称, 后者是文章标题 (如果标题不存在则不显示). 经典主题和默认主题输出标题用的代码如下.

<title><&#63;php wp_title('&laquo;', true, 'right'); &#63;> <&#63;php bloginfo('name'); &#63;></title>
Copy after login

输出的标题结构是 "文章标题 » 博客名称".

除非你的标题和文章内容相关度比较高, 否则这样的标题对 SEO 显然是不好的. 标题是爬虫认为重要的内容之一, 如果标题中包含了与文章内容无关的信息, 多少会对该页面有所影响.

那应该怎么弄呢? 我们可以对不同类型的页面进行区分, 我的实现代码如下.

<title><&#63;php
 // 如果是首页和文章列表页面, 显示博客标题
 if(is_front_page() || is_home()) { 
 bloginfo('name');
 
 // 如果是文章详细页面和独立页面, 显示文章标题
 } else if(is_single() || is_page()) {
 wp_title('');
 
 // 如果是类目页面, 显示类目表述
 } else if(is_category()) {
 printf('%1$s 类目的文章存档', single_cat_title('', false));
 
 // 如果是搜索页面, 显示搜索表述
 } else if(is_search()) {
 printf('%1$s 的搜索结果', wp_specialchars($s, 1));
 
 // 如果是标签页面, 显示标签表述
 } else if(is_tag()) {
 printf('%1$s 标签的文章存档', single_tag_title('', false));
 
 // 如果是日期页面, 显示日期范围描述
 } else if(is_date()) {
 $title = '';
 if(is_day()) {
  $title = get_the_time('Y年n月j日');
 } else if(is_year()) {
  $title = get_the_time('Y年');
 } else {
  $title = get_the_time('Y年n月');
 }
 printf('%1$s的文章存档', $title);
 
 // 其他页面显示博客标题
 } else {
 bloginfo('name');
 }
&#63;></title>
Copy after login

Keywords 和 Description

Keywords 为搜索引擎提供网页包含的核心内容, Description 则为搜索引擎提供网页的描述信息. 我发布的主题曾经包括了对 keywords 和 description 的处理, 但因为一些 SEO 插件发生冲突, 在较新的版本中已经去除. 笔者认为 WordPress 的很多所谓 SEO 插件做得并不到位, 对中文博客支持都很差, 自己修改一下效果可能更好.

以下是我对 keywords, description 和页面标题的处理规则, 实现方法可以参考上一段关于页面标题的说明.

文章详细页面的描述一般取文章的前 220 个字符即可, 特别重要的页面和文章可以自定义摘要, 使其信息准确度更高. 如果存在摘要信息则使用摘要, 没有则使用前 220 个字符, 实现代码如下.

<&#63;php
 if($post->post_excerpt) {
 $description = $post->post_excerpt;
 } else {
 // utf8_trim 方法是为了在截取字符之前对字符串进行转义, 避免出现截取半个汉字的情况
 // 参考文档: http://php-utf8.61924.nl/documentation/functions/utf8_trim.html
 $description = utf8_trim(substr(strip_tags($post->post_content), 0, 220));
 }
&#63;>
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24
How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance &gt;Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

How to view the front-end of WordPress How to view the front-end of WordPress Apr 20, 2025 am 10:30 AM

You can view the WordPress front-end by logging into the dashboard and switching to the View Sites tab; automate the viewing process with a headless browser; installing the WordPress plugin to preview the front-end within the dashboard; viewing the front-end via a local URL (if WordPress is set locally).

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

See all articles