0

I created a custom-post-type using the Pods plugin. One of the fields is a "website" field. In single-portfolio.php, I have the following code in order to display the website field info.

<p>
<?php $website = get_post_meta($post->ID, 'website', true);?> Website: <a target="_blank" href="<?php echo $website; ?>"><?php echo preg_replace('|https?://|', '', $website ); ?></a><?php $site = preg_replace('{/$}', '', $website); ?>
</p>

2 Questions:

  1. This creates a link url, which when clicked on, the url is preceded by MY site url, and obviously links to a 404 page. What can I change to get ONLY the field's info url?

  2. How can I get the word "Website:" (which precedes the actual field info) to appear in the site ONLY if the field was completed in the backend. Meaning, if this particular project does not have any website info associated with it, the text "Website:" should not appear at all on the site page?

Emma
  • 7
  • 4

1 Answers1

0

To conditionally display the link (issue 2) check if the $website variable has a value:

<?php $website=get_post_meta($post->ID,'website',true);
if($website):?>
    Website: <a target="_blank" href="<?php echo $website; ?>"><?php echo preg_replace('|https?://|', '', $website ); ?></a>
<?php $site = preg_replace('{/$}', '', $website);
endif;?>

Issue 1 sounds like your URL doesn't start with http://. If it doesn't the browser will assume it's a relative link. If you can't know in advance whether the $website string will start with http://, implement a function like the one provided in this answer: https://stackoverflow.com/a/2762083/4772280.

Once you've added that function to your functions.php file add this line to your code, after you assign a value to $website:

$website=addhttp($website);
Community
  • 1
  • 1
Jared
  • 718
  • 5
  • 11
  • Okay, tried combing the addhttp to the code, where did I mess up since it's still not going to correct url? – Emma Jul 30 '15 at 00:58
  • sorry, more info here: I edited the code as follows: http://pastebin.com/tpfkg76Q, however I must've done something incorrectly since it's still not sending to correct url. The preg_replace is there because I didn't want the `http://` seen in the browser, for aesthetic purposes. Removing the preg_replace didn't help and the url was still preceded by site. – Emma Jul 30 '15 at 01:05
  • @Emma I've updated your code, let me know if this works: http://pastebin.com/8MrqmFMQ. – Jared Jul 30 '15 at 04:58
  • the link works great now, url is correct, though now the text "Website:" does appear when it doesn't have a value. – Emma Jul 31 '15 at 00:34