wordpress函数删除特定类别帖子中的第一张图片
问题描述:
我目前使用此功能从我的帖子中删除第一张图片,但是,我需要从特定类别的帖子中删除第一张图片。任何帮助深表感谢。wordpress函数删除特定类别帖子中的第一张图片
function remove_first_image ($content) {
if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
$content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
}
return $content;
}
//add_filter('the_content', 'remove_first_image');
答
我想通了。
function remove_first_image ($content) {
if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
if(has_term(array('entertainment', 'food', 'home', 'travel'), 'category'))
$content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
}
return $content;
}
add_filter('the_content', 'remove_first_image');
答
可以使用in_category有条件的功能
function remove_first_image ($content) {
if (in_category('catslug')) {
$content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
}
return $content;
}
//add_filter('the_content', 'remove_first_image');