[英]Displaying Advanced Custom Field (ACF) value in a WooCommerce hook
[英]Dinamically create a link using ACF (Advanced Custom Field) custom value
我正在嘗試基於自定義字段創建自定義鏈接,如下所示:
<a href='htts://wa.me/55[acf field="phone-number"]?text=more%20text%here'>Whatsapp</a>
也許創建另一個短代碼加載 de ACF 字段,但我不知道該怎么做。
我試過定制以下代碼,但沒有成功:
function diwp_enclosed_shortcode_social_links($attr, $content){
$args = shortcode_atts( array(
'url' => '#',
'color' => '#F0F',
'textsize' => '16px'
), $attr );
$output = '<a href="'.$args['url'].'" style="color:'.$args['color'].'; font-size:'.$args['textsize'].' ">'.$content.'</a>';
return $output;
}
add_shortcode( 'enclosed_social_links', 'diwp_enclosed_shortcode_social_links' );
您好,如文檔中所述,您只需添加與之關聯的帖子的 ID 即可加載 acf 字段:
$value = get_field( "phone-number", 123 );
您可以在后端編輯帖子的 url 中找到帖子 ID,例如:https://your-url/wp-admin/post.php?post=161&action=edit
在那種情況下,我們將從帖子 161 獲取電話號碼,並且應該全部設置,如果整個事情需要動態完成,那么我們可以只使用get_field()因為我們應該在該字段所在的頁面中保存。
聖誕節快樂!
我用以下代碼解決了我的問題:
<?php function numero_whatsapp_dinamico($attr) { $post_id = $attr['post_id']; $phone_number = get_field('numero_de_whatsapp', $post_id); $output = '<a class="botao-whatsapp-estabelecimento" href="https://wa.me/55'. $phone_number. '?text=more%20text%20here" ">Whatsapp</a>'; return $output; } add_shortcode('numero_whatsapp_estabelecimento', 'numero_whatsapp_dinamico')?>
希望它能幫助其他人解決同樣的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.