【发布时间】:2020-07-04 21:09:19
【问题描述】:
我找到了一个解决方案来更改我的 wordpress 主题中的链接,但不是内容中的链接。怎么可能获取内容中的 URL,所以我也可以更改它们?
我需要使用the content 过滤器。但是如何更改诸如 apple.com/test/apple.com/test-123/、apple.com、microsoft.com、microsoft.com/test/ 之类的 URL。该函数还应该正确更改内容中每个匹配的 URL。
add_filter('the_content ', 'function_name');
很遗憾,answer of a similiar question 不起作用。
这是我更改链接的工作解决方案,但不是内容中的链接。
add_filter('rh_post_offer_url_filter', 'link_change_custom');
function link_change_custom($offer_post_url){
$shops= array(
array('shop'=>'apple.com','id'=>'1234'),
array('shop'=>'microsoft.com','id'=>'5678'),
array('shop'=>'dell.com','id'=>'9876'),
);
foreach( $shops as $rule ) {
if (!empty($offer_post_url) && strpos($offer_post_url, $rule['shop']) !== false) {
$offer_post_url = 'https://www.network.com/promotion/click/id='.$rule['id'].'-yxz?param0='.rawurlencode($offer_post_url);
}
}
$shops2= array(
array('shop'=>'example.com','id'=>'1234'),
array('shop'=>'domain2.com','id'=>'5678'),
array('shop'=>'domain3','id'=>'9876'),
);
foreach( $shops2 as $rule ) {
if (!empty($offer_post_url) && strpos($offer_post_url, $rule['shop']) !== false) {
$offer_post_url = 'https://www.second-network.com/promotion/click/id='.$rule['id'].'-yxz?param0='.rawurlencode($offer_post_url);
}
}
return $offer_post_url;
}
【问题讨论】:
-
内容是指帖子编辑器内容中的链接吗?
-
@mikerojas 是的,完全正确。
-
我下面提供的方案是用来过滤内容的。
-
等等,帖子编辑器中实际出现的内容或输出中出现的内容或两者兼而有之,还是什么?
-
@CKMacLeod 我认为帖子编辑器中出现的内容。在输出中它应该已经被改变了。