简体   繁体   English

使用 ACF(高级自定义字段)自定义值动态创建链接

[英]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.

相关问题 在WooCommerce挂钩中显示高级自定义字段(ACF)值 - Displaying Advanced Custom Field (ACF) value in a WooCommerce hook 如何创建通过高级自定义字段(ACF)分类返回自定义帖子类型的查询? - How to create a query that return custom post type by Advanced Custom Field(ACF) Taxonony? 以编程方式更新转发器字段中特定组字段的值 - 高级自定义字段 (ACF) - Wordpress - Update the value of specific group field inside repeater field programmatically - Advanced Custom Field (ACF) - Wordpress 显示来自自定义帖子类型分类法的高级自定义字段(ACF)值-Wordpress - Display advanced custom field (ACF) value from custom post type taxonomy - wordpress 高级自定义字段(ACF)-遍历转发器签出字段并输出每个值的单个实例 - Advanced Custom Fields (ACF) - loop through repeater checkout field and output single instance of each value 高级自定义字段/根据另一个 acf 字段填充选择 - Advanced custom fields / populate select based on another acf field 在CSS中将背景图片添加到高级自定义字段(ACF) - Adding background image in CSS to Advanced Custom Field (ACF) 如果产品内容为空,则显示高级自定义字段(ACF) - Show advanced custom field (ACF) if product content is empty ACF 高级自定义字段乘法返回 0 而不是正确的数学答案 - ACF Advanced Custom Field Multiplication returning 0 instead of the correct math answer WordPress高级自定义字段(ACF)-设置关系字段的样式-CSS - WordPress Advanced Custom Fields (ACF) - Styling the Relationship Field - CSS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM