簡體   English   中英

如何在 Wordpress php function 中多次使用 return str_replace?

[英]How to Use return str_replace multiple times in Wordpress php function?

我想用占位符替換多個值,但這樣只有一個替換出現在內容中。 請糾正我。

function custom_the_content( $content ) {
    return str_replace('{apktitle}', get_the_title(), $content);
    return str_replace('{apkversion}', get_datos_info('version'), $content);
}
add_filter('the_content', 'custom_the_content');

它只顯示一個替換的原因是,一旦你從 function 返回,它之后的任何代碼都不會執行。

幸運的是str_replace可以將 arrays 作為前兩個 arguments

如果搜索和替換是 arrays,則 str_replace() 從每個數組中獲取一個值並使用它們來搜索和替換主題。

你可以這樣寫:

function custom_the_content( $content ) {
    $search = ['{apktitle}', '{apkversion}'];
    $replace = [get_the_title(), get_datos_info('version')];
    return str_replace($search, $replace, $content);
}
add_filter('the_content', 'custom_the_content');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM