My website uses the AeroCore theme. I’ve noticed two issues: the date archive page and the site search page have been indexed by Google, but these pages are of no use to users; the pagination titles on the category, tag, and author pages are exactly the same, so Google may consider these pages to be duplicates.
To solve the problem, I created a sub-topic functions.php Several PHP code snippets were added. This code consists of two parts: The first part adds "noindex" tags to the date archive pages and search pages; the second part adds page numbers to the pagination titles and descriptions. This approach improved the website's SEO.
1. Prevent Google from indexing date-based archive pages and search results pages
Date archive pages list posts chronologically, and different dates may display similar lists of posts; the site search page generates results based on user input. Neither of these pages offers any particular value; they waste Google’s crawling time and lower the site’s overall quality.
Complete Code
add_action( 'wp_head', function() {
global $wp_query;
if ( $wp_query->is_date ) {
echo '<meta name="robots" content="noindex,nofollow">' . "\n";
}
if ( $wp_query->is_search ) {
echo '<meta name="robots" content="noindex,nofollow">' . "\n";
}
}, 99 );
What does the code do?
The code checks whether the current page is a date archive page or a search results page. If so, it adds a tag to the page header instructing Google not to index the page. The tag contains the following: noindex, nofollow。noindex Indicates "Do not index,"nofollow This means you shouldn't follow the links on the page.
This action will not affect users' normal browsing experience; they will still be able to view these pages. However, it helps with Google SEO: Google will remove these pages from its index, which will improve the website’s overall quality signals. Users will also no longer see these irrelevant pages when searching on Google.
2. Add page numbers to the page titles and descriptions
Category pages, tag pages, and author pages may have multiple pages. For example, if a category has 10 pages of articles, and previously every page had the same title and description, Google couldn’t distinguish between Page 2 and Page 3, and users might also click on the wrong page. The code will add a page number to each paginated page; it only handles situations with multiple pages and makes no changes when there is only one page.
Complete Code
add_action('template_redirect', function () {
if (is_admin() || wp_doing_ajax() || (defined('REST_REQUEST') && REST_REQUEST)) {
return;
}
if (!(is_category() || is_tag() || is_author())) {
return;
}
$paged = max(1, (int) get_query_var('paged'));
global $wp_query;
$max_pages = empty($wp_query->max_num_pages) ? 0 : (int) $wp_query->max_num_pages;
if ($max_pages < 2) {
return;
}
$site_name = get_bloginfo('name');
$a_name = '';
if (is_author()) {
$author = get_queried_object();
$a_name = $author ? $author->display_name : '';
}
// 拿到当前归档的原始标题(分类名/标签名/作者名)
$base = single_cat_title('', false);
if (is_tag()) {
$base = single_tag_title('', false);
}
if (is_author()) {
$base = get_queried_object()->display_name ?? '';
}
// ⬇️ 关键:在 ob_start 回调里统一处理标题和描述
ob_start(function ($html) use ($site_name, $paged, $a_name, $base) {
// ---- 改写 <title> ----
$html = preg_replace_callback('/<title>(.*?)<\/title>/is', function ($m) use ($site_name, $paged, $a_name, $base) {
if ($a_name !== '') {
return '<title>' . $a_name . '的文章 - 第' . $paged . '页 - ' . $site_name . '</title>';
}
return '<title>' . $base . ' - 第' . $paged . '页 - ' . $site_name . '</title>';
}, $html, 1);
// ---- 改写 <meta name="description"> ----
$html = preg_replace_callback(
'#<meta\s+name=["\']description["\']\s+content=["\'](.*?)["\']\s*/?>#is',
function ($m) use ($paged, $a_name, $base) {
$orig = $m[1] ?? '';
if ($a_name !== '') {
$new = '这是作者' . $a_name . '的文章第' . $paged . '页,' . $orig;
} else {
$new = '这是' . $base . '第' . $paged . '页,' . $orig;
}
return '<meta name="description" content="' . $new . '">';
},
$html, 1
);
return $html; // ⬅️ 必须返回修改后的内容
});
}, PHP_INT_MAX - 1);
What does the code do?
- Get the current archive title: Through
single_cat_title()、single_tag_title()Retrieve the original title based on the author's name and save it to$baseVariables. - 在
ob_startHandle it uniformly in the callback: All modifications to the title and description are placed inside the callback function, and finally, usereturn $html;Send the modified content back to the buffer. - Rewrite the title: For category/tag pages, add “Page X” after the original title; for author pages, display “Articles by [Author’s Name] – Page X – [Site Name].”
- Rewrite the description: Add the prefix “This is page X of XXX,” before the original description.
When users see the page number in the title, they know which page they're on, and Google can also distinguish between different pages. The keyword positions haven't changed; the category name is still at the beginning of the title, the page number is in the middle, and the site name is at the end—which is SEO-friendly.
Compatibility Information
💡 Compatibility Notes: The code runs before the translation plugin, so it modifies the Chinese pages first, while the English pages are automatically handled by the translation plugin. The main link tags remain unchanged, so the SEO plugin can continue to function normally. This approach is very safe.