【问题标题】:How to remove caption p class from wordpress?如何从 wordpress 中删除标题 p 类?
【发布时间】:2015-12-19 03:05:17
【问题描述】:

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

到目前为止,我可以使用此功能摆脱图像:

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

现在,由于我保留了所有 &lt;p&gt; 标记以用于样式设置并保留书面文章内容,这意味着图像的标题不会被删除,因为它们被包裹在 &lt;p class="wp-caption-text"&gt;

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

【问题讨论】:

    标签: javascript php jquery wordpress


    【解决方案1】:

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

    其次,要删除带有类的标签,您必须使用我们的好老朋友正则表达式。所以你的代码可能看起来有点像这样:

    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);
    

    【讨论】:

    • 好的,所以我把这个函数放在functions.php 中,并设置了if 条件:is_page_template(get_template_directory_uri() . 'templates/slider-with-text.php') 然后在模板页面上,我不确定我应该做什么。我只尝试了the_content(),但出现了图像和大写字母。我试过remove_images(the_content()),结果相同。我尝试了remove_images($content) 并得到未定义$content 的错误。抱歉,我只是不确定如何在模板页面上调用此函数或测试我针对该模板的条件是否准确。感谢您的帮助
    • functions.phpremove_images() 函数和 add_filter 的正确位置。然后在模板中简单地使用the_content() 是正确的。如果您查看the_content(),您会看到有一个apply_filters() 函数。这是应用 add_filter() 函数所做的任何更改的地方。
    • 您可以通过删除 if 语句并仅执行其中的代码来测试 if 语句是否有问题。如果它有效,您需要更改您的 if 语句条件。
    • 在你的 if 语句 is_page_template( 'templates/slider-with-text.php' ) 中尝试这个条件。该函数应该知道您已经在主题目录中查找了。
    • 是的,问题出在 if 语句上。非常感谢。我将其更改为is_page_template( 'templates/slider-with-text.php' ,现在它可以完美运行了。再次感谢!
    【解决方案2】:

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

    你可以像这样使用它:

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

    你可以从here了解更多关于removeclass的信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多