Home > Back-end >  Dinamically create a link using ACF (Advanced Custom Field) custom value
Dinamically create a link using ACF (Advanced Custom Field) custom value

Time:12-27

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 text%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' );

CodePudding user response:

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!

CodePudding user response:

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  href="https://wa.me/55' . $phone_number . '?text=more text here" ">Whatsapp</a>';
            return $output;
            }
            add_shortcode('numero_whatsapp_estabelecimento', 'numero_whatsapp_dinamico')

            ?>

Hope It'll help someone else with the same problem

  • Related