[英]How to Pass a Variable to my Function in functions.php (Wordpress Site)
[英]Looking to learn how a Custom Field can change a value in the functions.php of a wordpress site
我在wordpress functions.php頁面上添加了一些代碼,它可以按我的需要工作。
它將“ abcd”替換為“ 1234”
但是,我想做的是能夠用特定的自定義字段替換abcd。 因此,可以根據自定義字段的值將每個頁面上的abcd更改為其他內容。
我想知道,如果將我的自定義字段鍵稱為cftest,我需要將下面的代碼中的1234更改為什么。 還是我應該添加任何額外的代碼行?
您能提供給我的任何幫助將不勝感激。
function replace_text($text) {
$text = str_replace('abcd', '1234', $text);
$text = str_replace('efgh', '5678', $text);
return $text;
}
add_filter('the_content', 'replace_text');
您可以嘗試使用自定義字段的名稱。
另外,如果將鼠標懸停在Wordpress Admin中的自定義字段名稱上,請查看屏幕底部以查看tag_ID號。 此數字可能代表自定義字段中的值。
讓我知道是否有幫助。
非常感謝您的幫助。 通過查看您的鏈接,我找到了解決問題的方法。
我將上面的代碼更改為此,並且效果很好
function replace_text($text) {
global $post;
$cftest = get_post_meta($post->ID, "cftest", true);
$text = str_replace('abcd', '1234', $text);
$text = str_replace('efgh', $cftest, $text);
return $text;
}
add_filter('the_content', 'replace_text');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.