最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

adding a URL to a post meta

programmeradmin0浏览0评论

OK, a pretty dorky question, but I am new to PHP so bear with me.

How would I add an a href link to the code below so the custom meta field is linked on the front-end?

<?php if ( get_post_meta( $post->ID, "leetpress_website", true ) ){ ?>
<div class="pros"><p><strong>Movie Website:  </strong>
  <?php echo get_post_meta( $post->ID, "leetpress_website", true );?></p></div>
<?php } ?>

Thanks for putting up with my dorky-ness...

UPDATE: Got it working! Thanks for the help pointing me in the right direction Bainternet!

<?php
  if ( get_post_meta( $post->ID, "leetpress_website", true ) ){
    echo '<div class="pros"><p><strong>Movie Website:  </strong><a href="' .
    get_post_meta( $post->ID, "leetpress_website", true ) .
    '" target="_blank">' .
    get_post_meta( $post->ID, "leetpress_website", true ) .
    '</a></p></div>';
  }
?>

OK, a pretty dorky question, but I am new to PHP so bear with me.

How would I add an a href link to the code below so the custom meta field is linked on the front-end?

<?php if ( get_post_meta( $post->ID, "leetpress_website", true ) ){ ?>
<div class="pros"><p><strong>Movie Website:  </strong>
  <?php echo get_post_meta( $post->ID, "leetpress_website", true );?></p></div>
<?php } ?>

Thanks for putting up with my dorky-ness...

UPDATE: Got it working! Thanks for the help pointing me in the right direction Bainternet!

<?php
  if ( get_post_meta( $post->ID, "leetpress_website", true ) ){
    echo '<div class="pros"><p><strong>Movie Website:  </strong><a href="' .
    get_post_meta( $post->ID, "leetpress_website", true ) .
    '" target="_blank">' .
    get_post_meta( $post->ID, "leetpress_website", true ) .
    '</a></p></div>';
  }
?>
Share Improve this question edited Aug 22, 2019 at 22:22 alo Malbarez 4451 gold badge6 silver badges7 bronze badges asked May 17, 2011 at 17:35 Monique AndersonMonique Anderson 211 silver badge3 bronze badges 1
  • 1 Monique, it is not necessary that you update your question with the answer you got. You can just mark Bainternet's answer as correct and eventually thanks him, or comment his answer with your implementation details/corrections. – Drake Commented May 18, 2011 at 9:00
Add a comment  | 

2 Answers 2

Reset to default 2

Use another custom field with the URL and call it something like leetpress_website_url then change your code to this:

<?php
if ( get_post_meta( $post->ID, "leetpress_website", true ) ){
  echo '<div class="pros"><p><strong>Movie Website:  </strong><a href="' .
  get_post_meta( $post->ID, "leetpress_website_url", true ).'">' .
  get_post_meta( $post->ID, "leetpress_website", true ) .
  '</a></p></div>';
}
?>

It looks like something happened to the code you pasted, it's not complete, but here's an example of a meta field in an href:

<?php
$mylink = get_post_meta($post->ID, 'leetpress_website', true);
if($mylink){
?>
    <a href="<?php echo $mylink ?>" title="Movie Website">Movie Website</a>
<?php
}
?>

Edit- ok, Bainternet edited your code, see his answer.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论