1

This is a simple but tricky question. I strucked in a problem where i am unable to understand how can i warp all of the html code so that it show on backend with PHP value.

<a href="<?php echo get_post_meta( $post->ID, 'fit_link', true );
?>" target="_blank">

<?php echo get_post_meta( $post->ID, 'fit_text', true ); ?>
</a>

The problem is when there is no Link and text value retrieved from DB it shows a empty anchor tag in source. Just like this: <a href="" target="_blank"></a>

Can anyone please help me to understand that how can i make above code to output when there is value. otherwise it should not show any code in source.

2
  • 2
    what about adding an if ( !empty(get_post_meta( $post->ID, 'fit_link', true ))) { .... } Commented Jun 27, 2015 at 14:47
  • Yes It shows empty value but the problem is HTML. it render without data just like <a href="" target="_blank"> </a> Commented Jun 27, 2015 at 14:57

1 Answer 1

2

just use isset() function

    <?php
      if(isset($post->ID) && strlen(get_post_meta( $post->ID, 'fit_link', true ))>1)
      {
          echo "THE A HREF LINK IS = ".get_post_meta( $post->ID, 'fit_link', true ); //remove this line after testing
        ?>
     <a href="<?php echo get_post_meta( $post->ID, 'fit_link', true );?>" target="_blank">
     <?php echo get_post_meta( $post->ID, 'fit_text', true ); ?>
     </a>
    <?php
      }

?>

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for your answer, Can this code will stop output empty link and text in anchor tag on front end?
Again :( Output <a href="" target="_blank"> </a> code in source
where is function get_post_meta?? according to my code, if $post->ID is set, then only code will get executed, so the problem is with get_post_meta
Yes but HTML is around it so it just empty the link and text variable but it show HTML with empty tag <a href="" target="_blank"> </a>
Good! Yes links are always greater then one character. Thank you so much once again!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.