[英]Dinamically create a link using ACF (Advanced Custom Field) custom value
I'm trying to create a custom link based on a custom field, something like this:我正在尝试基于自定义字段创建自定义链接,如下所示:
<a href='htts://wa.me/55[acf field="phone-number"]?text=more%20text%here'>Whatsapp</a>
Maybe creating another shortcode loading de ACF field, but I don't know how do that.也许创建另一个短代码加载 de ACF 字段,但我不知道该怎么做。
I've tried do customized the following code, but without success:我试过定制以下代码,但没有成功:
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' );
Hello as explained in the documentation you can load the acf field just by adding the id of the post it is associated with:您好,如文档中所述,您只需添加与之关联的帖子的 ID 即可加载 acf 字段:
$value = get_field( "phone-number", 123 );
You can find the post id in the url on the edit post in the backend for example: https://your-url/wp-admin/post.php?post=161&action=edit您可以在后端编辑帖子的 url 中找到帖子 ID,例如:https://your-url/wp-admin/post.php?post=161&action=edit
In that case we will get the phone-number from the post 161 and it should all be set, if the whole thing need to be done dynamically then we can just use get_field() because we should be in the page in which the field is saved.在那种情况下,我们将从帖子 161 获取电话号码,并且应该全部设置,如果整个事情需要动态完成,那么我们可以只使用get_field()因为我们应该在该字段所在的页面中保存。
Merry christmas!圣诞节快乐!
I solved my problem with the following code:我用以下代码解决了我的问题:
<?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')?>
Hope It'll help someone else with the same problem希望它能帮助其他人解决同样的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.