

WordPress给特定文章添加Nofollow标签
source link: https://www.shephe.com/2023/07/wordpress-use-custom-fields-add-nofollow/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

WordPress给特定文章添加Nofollow标签
- 07/05/2023
- 网站建设
这几天折腾WordPress挺多,搞了一堆烂七八糟的页面出来,其中难免有一些不希望被搜索引擎索引,起先我打算在
Robots文件中禁用,但操作起来略显繁琐且又不够精细;又想到用专门的SEO插件如rankmath来控制,但这些个插件也是一笔不小的开销…在网上搜“WordPress如何给特定页面添加Nofollow标签”,也是没有看到合适的解决方案。
于是我想到了WordPress的自定义字段,一个好用且又简单的小组件。WordPress自定义字段是对WordPress Posts表的一种补充和扩展,一般来讲系统已经固化了日志的作者,分类,标签,时间等基本参数(metadata),我们还可以根据需要自定义出一系列其他信息,比如本例。
WordPress 自定义字段
如图所示是WordPress编辑器自定义字段所在地,要实现利用自定义字段给特定文章添加Nofollow标签其实很简单,仅需要在header.php
</head>
标签前添加如下代码:
<?php
$noindex_specific = get_post_meta($post->ID, 'noindex_post', true);
if ($noindex_specific) {
echo '<meta name="robots" content="noindex,nofollow" />';
}
?>
初次使用时,在任意编辑页面手动添加一个自定义字段noindex_post
,然后将值填为1
(按以上函数的写法值只要不为空则echo
短语)…点添加后效果应该立马就有了:
看明白了吧?是不是很简单,我能想到的是还可以利用自定义字段给指定文章添加特定CSS/JS、改写页面Title/descrition标签、添加文章来源页等等功能。比如若将下一段代码添加至<head>标签中,后台文章页面添加一个名称为descrition的字段,其其值则会优先设置为文章的描述,若没有给该字段赋值,则提取文章的前220个字符作为描述。
更多好玩的用户还待你去进一步挖掘,关于WordPress自定义字段的准确定义和用法可以看看官方文档。不小心把自定义字段玩烂了怎么办?删除呗:删除无效的 WordPress 自定义字段。
<?php $description = '';
if (is_home() || is_page()) {
$description = "世事洞明皆学问,人情练达即文章。";
}
elseif (is_single()) {
$description1 = get_post_meta($post->ID, "description", true);
$description2 = str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", 'utf-8'));
$description = $description1 ? $description1 : $description2;// 填写自定义字段description时显示自定义字段的内容,否则使用文章内容前220字作为描述
}
elseif (is_category()) {
$description = category_description();
}elseif (is_tag()){
$description = tag_description();
}else {
$description = "世事洞明皆学问,人情练达即文章。";
}
$description = trim(strip_tags($description));
?>
<meta name="description" content="<?php echo $description; ?>" />
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK