Site logo

Archived topic

Dynamic Text External Link

7 replies · Started by Matt on August 25, 2021

Viewing posts 1–8 of 8

Hi Guys,

Just wondering if there is a way to make dynamic text links open a new tab (ie. target="_blank")?

Thanks

Hi there,

select the Button link - beside the URL field, theres a 3 dot menu, select that and select Open Link in New Tab.

Hi!

I'm only seeing the 3 dots when it's a button element.
Is there a way to open a text link (h2) in a new tab?

Hi there,

there isn't the option for that, where is the link stored ?

It is a header (H2) with dynamic text and a dynamic link from meta fields.

Hmmm.... i think this would be easier to create a shortcode then it would be to filter the rel attribute in.
A PHP Snippet like this for the shortcode:


add_shortcode('db_meta_link', function() {
    $url = get_post_meta( get_the_ID(), 'url-meta-key', true );
    $text = get_post_meta( get_the_ID(), 'text-meta-key', true );
    
    $html = '<a href="' . $url . '" rel="_blank">' . $text . '</a>';
    
    return $html;
} );

Then you can add the [db_meta_link] inside the Headline Block.

Ok, that looks good.

I'll just need to input the meta keys into the shortcode coz it's not always the same.

Ok so give this shortcode a try:

function custom_meta_link( $atts ) {

    $atts = shortcode_atts(
        array(
            'text' => '',
			'link' => '',
        ), $atts, 'custom_field_display' 
    );

    $html = '';
    $text = get_post_meta(get_the_id(), $atts['text'], true);
    $link = get_post_meta(get_the_id(), $atts['link'], true);
	
    if(!empty($text) && !empty($link) ){
        $html = sprintf('<a href="%2$s">%1$s</a>', $text, $link );
    }

    return $html;
}
add_shortcode( 'db_meta_link', 'custom_meta_link' );

Now you can do:

[db_meta_link text="your_text_meta_key" link="your_link_meta_key"]

This archived topic is closed to new replies.