• Resolved elijahn

    (@elijahn)


    I’m trying to insert a unique(to each post) URL at the end of each post. The URL is stored in a custom field. I have tried the following:

    <?php
    $link = get_post_meta($post->ID, 'custom_field_name', true);
    if ($link){
      echo '<a href="'.$link .'">Visit this site.</a>';
    }
    ?>

    and

    <a href="<?php echo get_post_meta($post->ID, "custom_field_name", true); ?>">Visit this site.</a>

    Both of these codes have resulted in the URL being displayed with my domain attached to the beginning so it looks like:

    https://www.mydomain42342.com/www.thedomaininthecustomfield.com/news/topic.html

    Instead of the intended:

    www.thedomaininthecustomfield.com/news/topic.html

    How do I fix this?

    Thanks!

    Elijah

Viewing 3 replies - 1 through 3 (of 3 total)
  • You only need make sure you include the https:// when you put an address into the custom field, else the link is assumed to be relative to the current site.

    However you could just conditionalise the custom field code, adding the https:// when necessary, here’s a basic example..

    if( $link ) {
    	$link = esc_url( $link );
    	if( substr( $link , 0 , 7 ) != 'https://' )
    		$link = 'https://' . $link;
    	echo '<a href="'.$link .'">Visit this site.</a>';
    }

    Thread Starter elijahn

    (@elijahn)

    Thanks! That was the problem.

    I have a quick question on safetynewstoday.com, how can l get it to show a unique url like safetynewstoday.com/categoryname/article name

    instead of

    https://www.safetynewstoday.com/?cat=235

    Today is my 7th day bday using WordPress, l got it to work on this site

    https://visaliamarketinggroup.com

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Inserting URL from Custom Field’ is closed to new replies.