我使用wordpress,我已经在我的functions.php中添加了一些代码来自动处理所有不符合特定条件的链接。
当前,如果链接examplesite.com或examplesite2.com标记为dofollow,否则设置为nofollow
我用来实现这一点的代码(可以完美地工作)是:
function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "https://www.examplesite.com") === false && strpos($m[1], "https://www.examplesite2.com") === false)
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
else
return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');但是,我想添加一个额外的条件。
如果站点是examplesite或examplesite2.com,则将其标记为rel="follow"
其他
如果是examplesite3.com或examplesite4.com,则将其标记为rel=”sponsored”
否则(如果它不满足这两个条件中的任何一个,则返回
而不是no Follow。
有人能帮我一下吗?我试着添加
function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "https://www.examplesite.com") === false && strpos($m[1], "https://www.examplesite2.com") === false && strpos($m[1], "https://examplesite3.com") === false && strpos($m[3], "https://examplesite4.com") === false)
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
else
return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
return '<a href="'.$m[1].'" rel="sponsored" target="_blank">'.$m[3].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');但它创建了一个循环,并且不起作用。提前感谢您的任何想法!
我也尝试了这个,修改了elseif,但它似乎没有通过第二个条件。
function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "https://www.examplesite.com") === false && strpos($m[1], "https://www.examplesite2.com") === false && strpos($m[3], "https://www.bluehost.com") === false && strpos($m[3], "https://amzn.to") === false)
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
elseif (strpos($m[3], "https://www.examplesponsor.com") === false && strpos($m[3], "https://www.examplesponsor2.com") === false && strpos($m[3], "https://examplesponsor3.com") === false)
return '<a href="'.$m[3].'" rel="sponsored" target="_blank">'.$m[4].'</a>';
else
return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');发布于 2019-12-20 00:45:56
像这样试一下:
function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if ((strpos($m[1], "https://www.examplesite.com") !== false) ||
(strpos($m[1], "https://www.examplesite2.com") !== false)) {
return '<a href="'.$m[1].'" rel="follow" target="_blank">'.$m[2].'</a>';
} elseif ((strpos($m[1], "https://www.examplesite3.com") !== false) ||
(strpos($m[1], "https://www.examplesite4.com") !== false)) {
return '<a href="'.$m[1].'" rel="sponsored" target="_blank">'.$m[2].'</a>';
} else {
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
}
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');这将检查是否使用strpos找到了这两个rel=中的任何一个,并为每种情况返回适当的URL。对于所有其他URL,它将返回rel="nofollow"。如果您必须定义更多的URL,请确保在逻辑上也对它们执行||。
对于非常长的→列表,我将更改代码结构,以进行URLs类型的数组映射,并循环遍历匹配每个条目的条目。
https://stackoverflow.com/questions/59413270
复制相似问题