简体   繁体   中英

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.

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:

$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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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