繁体   English   中英

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM