如何从wordpress中删除标题p类

How to remove caption p class from wordpress?

本文关键字:标题 删除 wordpress      更新时间:2023-09-26

我不希望任何图像或它们的标题出现在我的循环中,但我想保持html的样式。

到目前为止,我可以使用以下功能清除自己的图像:

function remove_images () {
$content = get_the_content();
$content = apply_filters('the_content', $content);
$content = strip_tags($content, '<p><a>');
return $content;
}

现在,由于我保留了所有的<p>标签用于样式设计和保留书面文章内容,这意味着图像的标题不会被删除,因为它们被包裹在<p class="wp-caption-text">

那么,现在,我如何删除这一个类,这样字幕就不会出现?我想把它写成一个函数,在特定的时间调用它。我有多个循环,在其中一些循环中我想要标题,而在其他循环中我不。。。

首先,我会在您想要输出内容的地方(在您的模板中或在您使用它的任何地方)使用the_content(),而不是使用您这样做的方式。然后,使用add_filter()修改内容。

其次,要使用类删除标记,您必须使用我们的老朋友regex。所以你的代码可能看起来有点像这样:

function remove_images( $content ){
    if( /*condition for where you want this to occur*/ ){
        $content = strip_tags($content, '<p><a>');
        $content = preg_replace('#<p class="wp-caption-text">(.*?)</p>#', '', $content );
    }
    return $content;
}
add_filter( 'the_content', 'remove_images', 100, 1);

您可以使用jquery removeClass函数来删除特定的类

你可以这样使用它:

$( document ).ready(function() {
    $("p.wp-caption-text").removeClass("wp-caption-text");
});

您可以从这里了解更多关于removeclass的信息