2016-07-14 18 views
-1

我目前通過一個帶有值「:section_content」的AFC將多段內容插入到我的帖子中。此外,我正在使用下面的代碼來清理我的WP帖子。我如何修改此過濾器以包含我的ACF?使用functions.php在Wordpress中過濾ACF的最佳方式

<?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; 
} 

?> 

回答

0

我想你可以使用ACF Filters。我通常掛鉤到acf/format_value爲了清潔或修改我的自定義字段值之前打印它們。

你甚至可以只鉤某些字段類型,如:

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

希望這有助於!