【问题标题】:Wordpress Advanced Custom Fields get_contentWordpress 高级自定义字段 get_content
【发布时间】:2017-05-01 01:57:02
【问题描述】:

我有一个函数可以自动在我的标题周围添加一个跨度,以便我可以设置跨度的样式,例如:

<h2><span>my heading</span></h2>

这在我的正常 Wordpress 内容中运行良好,但高级自定义字段中的内容将其删除。有没有人有任何想法 - 花了几个小时在谷歌上搜索。

//add span into each title so can add flourish under span
add_filter('the_content', 'replace_content',1);
function replace_content($content) {
   $content = preg_replace( '/<h(\d{1,6})(.*?)>(.*?)<\/h(\d{1,6}).*?>/', '<h$1><span>$3</span></h$4>',  $content );
   return $content;
}

非常感谢任何可以提供帮助的人!

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    我还没有尝试过,但 ACF 有 acf/load_value 过滤器,这可能是您正在寻找的。该钩子允许您在从数据库加载字段后立即修改字段的值。

    来自documentation的代码示例:

    function my_acf_load_value( $value, $post_id, $field )
    {
        // run the_content filter on all textarea values
        $value = apply_filters('the_content',$value); 
    
        return $value;
    }
    
    // acf/load_value - filter for every value load
    add_filter('acf/load_value', 'my_acf_load_value', 10, 3);
    
    // acf/load_value/type={$field_type} - filter for a value load based on it's field type
    add_filter('acf/load_value/type=select', 'my_acf_load_value', 10, 3);
    
    // acf/load_value/name={$field_name} - filter for a specific value load based on it's field name
    add_filter('acf/load_value/name=my_select', 'my_acf_load_value', 10, 3);
    
    // acf/load_value/key={$field_key} - filter for a specific field based on it's name
    add_filter('acf/load_value/key=field_508a263b40457', 'my_acf_load_value', 10, 3);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-28
      • 2012-08-10
      • 2015-01-21
      • 1970-01-01
      • 2013-11-25
      • 2018-06-07
      • 2018-05-19
      • 2012-09-02
      相关资源
      最近更新 更多