首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Wordpress中使用functions.php过滤ACF的最好方法

在Wordpress中使用functions.php过滤ACF的最好方法
EN

Stack Overflow用户
提问于 2016-07-14 23:01:15
回答 1查看 641关注 0票数 0

我现在正在通过一个取值为"section_content“的自动取款机把多个内容放入我的帖子中。此外,我使用下面的代码来清理我的可湿性粉剂帖子。如何修改此筛选器以包括我的ACF?

代码语言:javascript
复制
  <?php
/**
* Clean posts from inline styling and unnecessary tags
*/

add_filter( 'the_content', 'clean_post_content' );
function clean_post_content($content) {
    if ( is_single() ) {
        $patterns = array(
        '/(<[^>]+) style=".*?"/i',                      // Remove inline styling
        '/<\/?font[^>]*>/',                             // Remove font tag
        '/<(p|span)>(?>\s+|&nbsp;|(?R))*<\/\1>/',       // Empty p, span (font tags already removed)
        '/(<h[1-6]>[^<]*)<\/?strong>(.*?<\/h[1-6]>)/',  // h1-6
        );

        $replacements = array(
        '$1',
        '',
        '',
        '$1$2'
        );

        $old_content = '';
        while ($old_content != $content) {
          $old_content = $content;
          $content = preg_replace($patterns, $replacements, $content);
        }
    }
    return $content;
}

?>
EN

回答 1

Stack Overflow用户

发布于 2016-07-22 17:49:18

我想你可以使用ACF Filters。我通常挂接到acf/format_value,以便在打印之前清理或修改我的自定义字段值。

您甚至可以仅挂钩到某些字段类型,例如:

代码语言:javascript
复制
function acf_brand_trademark( $value, $post_id, $field ) {
    $value = preg_replace( '/Brand /', 'Brand<sup>™</sup> ', $value );
    return $value;
}

add_filter('acf/format_value/type=textarea', 'acf_brand_trademark', 10, 3);
add_filter('acf/format_value/type=text', 'acf_brand_trademark', 10, 3);

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38377681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档