admin管理员组文章数量:1122832
add_filter('the_content', 'removelink_content',1);
function removelink_content($content = '')
{
preg_match_all("#<a(.*?)>(.*?)</a>#i",$content, $matches);
$num = count($matches[0]);for($i = 0;$i < $num;$i++){
$content = str_replace($matches[0][$i] , $matches[2][$i] , $content);
}
return $content;
}
It works, but I would like to exclude some categories. That is, that the code works only on posts that are not in an excluded category.
add_filter('the_content', 'removelink_content',1);
function removelink_content($content = '')
{
preg_match_all("#<a(.*?)>(.*?)</a>#i",$content, $matches);
$num = count($matches[0]);for($i = 0;$i < $num;$i++){
$content = str_replace($matches[0][$i] , $matches[2][$i] , $content);
}
return $content;
}
It works, but I would like to exclude some categories. That is, that the code works only on posts that are not in an excluded category.
Share Improve this question asked Dec 22, 2022 at 3:55 unopermanenteunopermanente 32 bronze badges1 Answer
Reset to default 1To exclude certain categories from the filter function that removes links from the post content, you can modify the function to check the categories of the post before removing the links.
Here is an example of how you can modify the function to exclude certain categories:
add_filter('the_content', 'removelink_content',1);
function removelink_content($content = '') {
// Get the categories of the current post
$categories = get_the_category();
// Set up an array of excluded category IDs
$excluded_categories = array(2, 5, 8);
// Check if any of the categories of the current post are in the excluded categories array
$excluded = false;
foreach ( $categories as $category ) {
if ( in_array( $category->term_id, $excluded_categories ) ) {
$excluded = true;
break;
}
}
// If the post is not in an excluded category, remove the links from the content
if ( ! $excluded ) {
preg_match_all("#<a(.*?)>(.*?)</a>#i",$content, $matches);
$num = count($matches[0]);
for($i = 0;$i < $num;$i++){
$content = str_replace($matches[0][$i] , $matches[2][$i] , $content);
}
}
return $content;
}
In this example, the function first gets the categories of the current post using the get_the_category()
function. It then sets up an array of excluded category IDs (in this case, the IDs 2, 5, and 8). The function then checks if any of the categories of the current post are in the excluded categories array using the in_array()
function. If the post is in an excluded category, the $excluded
variable is set to true
.
If the $excluded
variable is not set to true
, the function proceeds to remove the links from the content as before. If the $excluded
variable is set to true
, the function does nothing and returns the content as-is.
I hope this helps! Let me know if you have any questions.
本文标签: phpRemove content links (internal and external)but exclude post at specific categories
版权声明:本文标题:php - Remove content links (internal and external), but exclude post at specific categories 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288554a1928117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论