Recently, I have been doing a new theme migration, and I have added the old theme to the thin content tab. noindex The tag code has also been reorganized. I took this opportunity to compare three different implementation methods, and found that the choice of code style and plug-in position is quite particular, and I will share with you my tossing process and final choice.
📋 Background description
In old topics, in order to prevent the thin content tabs with fewer than 2 articles being indexed by the search engine, I directly
functions.phpAdded code. This migration to a new theme, I decided to put all the custom code into the WPCode plugin for management, and also want to optimize the code structure.🔍 Comparison of three ways
1. The original code (left version of the old theme)
This is the code originally used in the old theme, directly mount to
wp_headHook, all logic is nested in conditional judgment:
// 为文章数小于2的薄内容标签页添加 noindex 标签
add_action( 'wp_head', 'add_noindex_to_thin_tag_pages' );
function add_noindex_to_thin_tag_pages() {
// 仅在标签归档页面执行
if ( is_tag() ) {
// 获取当前标签对象
$tag = get_queried_object();
// 判断是否存在且文章计数小于2
if ( $tag && isset( $tag->count ) && $tag->count < 2 ) {
// 针对您需求中的 Googlebot
echo '<meta name="googlebot" content="noindex, follow">' . "\n";
// 建议同时添加通用 robots 标签,以便 Bing、百度等其他搜索引擎也遵循此规则
echo '<meta name="robots" content="noindex, follow">' . "\n";
}
}
}2. Frontend only + wp_head (middle optimized version)
After migrating to WPCode, I noticed that the plugin has the option of ‘Frontend Only’ (the front end only takes effect). If you choose this, the code will not be executed in the background, so that you can omit it is_admin() judgment. At the same time, the code style is changed to ‘exclude those who do not meet the conditions first, and directly return The style of the Wei statement:
<?php
/**
* 为文章数小于2的薄内容标签页添加 noindex 标签
* 仅在前端执行(通过wpcode Location设置实现)
*/
function add_noindex_to_thin_tag_pages() {
// 仅在标签归档页面执行,不是标签页直接返回
if ( !is_tag() ) {
return;
}
// 获取当前标签对象,不存在则返回
$tag = get_queried_object();
if ( !$tag || !isset( $tag->count ) ) {
return;
}
// 判断文章计数小于2
if ( $tag->count < 2 ) {
echo '<meta name="googlebot" content="noindex, follow">' . "\n";
echo '<meta name="robots" content="noindex, follow">' . "\n";
}
}
add_action( 'wp_head', 'add_noindex_to_thin_tag_pages' );
?>
3. Site Wide Header (final version)
Later, I found that WPCode also has a ‘site wide (frontend) → site wide header’ location option. If you choose this, the plugin will directly insert the code into the web page <head> tags, even add_action(wp_head, ... ) This hook is not needed, the code can directly execute the output. Combined with the refreshing style of the previous code 2, the final code is sorted out:
<?php
/**
* 为文章数小于2的薄内容标签页添加 noindex 标签
* 直接输出到页面头部(通过wpcode Site Wide Header实现)
*/
// 不是标签页直接返回
if ( !is_tag() ) {
return;
}
// 获取当前标签对象,不存在则返回
$tag = get_queried_object();
if ( !$tag || !isset( $tag->count ) ) {
return;
}
// 判断文章计数小于2
if ( $tag->count < 2 ) {
echo '<meta name="googlebot" content="noindex, follow">' . "\n";
echo '<meta name="robots" content="noindex, follow">' . "\n";
}
?>
📊 Code comparison and selection analysis
In these three paragraphs of code, I compare from these perspectives:
| Compare the dimension | Code 1 (original version) | Code 2 (frontend only + wp_head) | Code 3 (Site Wide Header) |
|---|---|---|---|
| code structure | nest if Deep, logic is not intuitive enough | Wei statement style, clear logic, flat hierarchy | The same Wei statement style, but completely out of the hook mechanism |
| execute dependencies | theme-dependent wp_head() function call | theme-dependent wp_head() function call | Not dependentAny theme hooks, directly injected |
| scope of execution | The front and back office will be executed (you need to judge the background yourself) | Through the plugin position setting, only the front end is executed | Through the plugin position setting, only the front end is executed |
| Compatibility | Affected by the theme, if the theme is not called wp_head() then fail | Affected by the theme, if the theme is not called wp_head() then fail | Almost unaffected by the theme, as long as the theme has <head> tag |
| execution efficiency | Need to queuure and callback through hooks | Need to queuure and callback through hooks | Execute directly, no need to go through the hook mechanism |
| I ended up choosingCode 3, the main reasons are as follows: |
- The code style is more refreshing: I prefer ‘exclude inappropriate conditions directly
return‘The style of the Wei statement, the logic is clear at a glance, the nesting level is reduced, and the reading and maintenance experience is better. - More direct and efficient execution: Using WPCode’s ‘Site Wide Header’ insertion mechanism, the code is directly output at the header,theme-free
wp_head()hook call, compatibility and execution efficiency are better. This avoids the incorrect call due to the themewp_head()the risk of code failure. - The separation of duties is more thorough: The code itself only focuses on business logic (judgment of the tab and the number of articles), and the condition of ‘execute on the front end’ is guaranteed by the position setting of WPCode, which realizes the separation of configuration and code, which is more in line with the concept of modern plug-in management.
✅ Actual effect verification
After the code was deployed, I deliberately found two different tabs to verify the effect:
Scenario 1: Thin Content Tab (Only 1 Article)
Visit a tab of only 1 article, view the source code of the web page, you can clearly see the <head> has been successfully output noindex Label, indicating that the logical judgment is in effect.

Scenario 2: Normal tab page (more than 1 article)
Then visit a normal tab with multiple articles, check the source code, it is clean and there is no extra noindex Label output perfectly avoids the inclusion of normal pages by accidental injury.

💡 Summarize and think
The gains of this tossing are:Using the smart position of the plugin with the simple code style, it not only achieves the goal, but also keeps the code fresh.
For WordPress developers, understand the hook mechanism (such as wp_head) and conditional functions (such as is_tag(), get_queried_object()) is very important, but equally important, making good use of the tools and configuration options provided by modern plugins can allow us to write less sample code and focus more on the business logic itself.
If you are also doing theme migration or code optimization, you might as well try this method. Of course, no matter which method is used,Be sure to check the source code of the page after deployment, to ensure that the effect is as expected, this is the most critical step.
Final tips: Add to the tab
noindexBe cautious, be sure to confirm that it is for the ‘thin content’ page, avoid accidentally injuring the normal tab page with rich content, and affect the indexing and traffic of the website.

Leave a Reply