添加过滤器以替换字符串的一部分

时间:2016-10-02 14:08:08

标签: php string wordpress replace

添加过滤器以替换回声中部分字符串的正确方法是什么?

功能如下:

let result = [{
  "id": 1,
  "first_name": "Jean",
  "last_name": "Owens",
  "email": "jowens0@google.ru",
  "gender": "Female"
}, {
  "id": 2,
  "first_name": "Marie",
  "last_name": "Morris",
  "email": "mmorris1@engadget.com",
  "gender": "Female"
}]

我试图在我的functions.php中实现这个:

let = searchText = "s";
    let result = data.filter(object=>{
      for (var property in object) {
        if (object.hasOwnProperty(property)) {
          return object[property].toString().toLowerCase().indexOf(searchText) !== -1;
        }
      }
    });

但这不会影响h3值,过滤那些最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

functionreturn值,以便您可以直接使用它或echo值:

add_filter( 'woocommerce_template_loop_product_title', 'product_title_mod');

// option 1
function product_title_mod($str) {
    return str_replace("h3","h2",$str);
}

// option 2
function product_title_mod($str) {
    echo str_replace("h3","h2",$str);
}

不完全确定wordpress过滤器是如何工作的,但我认为你无法达到你想要的效果,因为woocommerce_template_loop_product_title函数echo直接产生结果。

相关问题