Link title to external link


/ Published in: PHP
Save to your folder(s)

Now whenever you want to share an external URL, add a custom field to your post namedtitle_url and set its value as the URL of the resource.


Copy this code and paste it in your HTML
  1. // function
  2. function print_post_title() {
  3. global $post;
  4. $thePostID = $post->ID;
  5. $post_id = get_post($thePostID);
  6. $title = $post_id->post_title;
  7. $perm = get_permalink($post_id);
  8. $post_keys = array(); $post_val = array();
  9. $post_keys = get_post_custom_keys($thePostID);
  10.  
  11. if (!empty($post_keys)) {
  12. foreach ($post_keys as $pkey) {
  13. if ($pkey=='title_url') {
  14. $post_val = get_post_custom_values($pkey);
  15. }
  16. }
  17. if (empty($post_val)) {
  18. $link = $perm;
  19. } else {
  20. $link = $post_val[0];
  21. }
  22. } else {
  23. $link = $perm;
  24. }
  25. echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
  26. }
  27.  
  28. // template tag
  29.  
  30. <?php print_post_title() ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.