【问题标题】:Remove P tags from Advanced Custom Fields images从高级自定义字段图像中删除 P 标签
【发布时间】:2015-07-20 23:25:25
【问题描述】:

我使用的是 WordPress 4.2.2,每次我向 WYSIWYG 添加图像时,它都会将输出图像包装在段落标签中。我需要去掉这些标签。我似乎在网上找到的所有东西都是 2011 年的,而且它似乎不起作用。

我试过把东西放在functions.php中,比如:

function filter_ptags_on_images($content){
  return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
add_filter('the_content', 'filter_ptags_on_images');

似乎没有任何效果。我怎样才能做到这一点。

顺便说一句,我正在使用 ACF Pro 的 WYSIWYG 和 JointsWP 入门主题,如果有影响,我的图像不会包含在链接标签中。

【问题讨论】:

  • 为什么不完全禁用wpautop
  • 我试过了:remove_filter('the_content', 'wpautop');但我认为这不起作用,因为我没有使用 the_content();我使用高级自定义字段所见即所得 [ the_field('myField'); ] 对吗?

标签: wordpress image tags


【解决方案1】:

由于您使用的是高级自定义字段,您应该可以使用get_field(),但请关闭格式:

echo get_field('myField', $post->ID, false);

阅读更多关于get_field() in the ACF Docs的信息。

您还可以通过在 functions.php 中执行以下操作来删除 wpautop 的 ACF 实现:

remove_filter('acf_the_content', 'wpautop');

【讨论】:

  • 实际上,即使在我需要将它们包装在段落标签中的段落中,这也会删除所有 p 标签。我只是想把它从图像中去掉
【解决方案2】:
$('p > img').unwrap();

nuff 说。

【讨论】:

    【解决方案3】:

    这对我有用 - 我在我的函数文件中添加了这个,以便在使用高级自定义字段所见即所得时摆脱图像周围的 p 标签:

    // gets rid of p tags around images when using the WYSIWYG advanced custom fields
    
    function filter_ptags_on_images($content)
    {
        $content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
    return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
    }
    add_filter('acf_the_content', 'filter_ptags_on_images');
    

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 2020-08-14
      • 2011-12-02
      • 2019-11-07
      • 2013-01-30
      • 2017-01-14
      • 2018-04-15
      • 1970-01-01
      • 2015-07-07
      相关资源
      最近更新 更多