YOU
YOU

Reputation: 1080

Insert all HTML in PHP

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.

Upvotes: 1

Views: 46

Answers (1)

Unni Babu
Unni Babu

Reputation: 1814

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
      }

?>

Upvotes: 2

Related Questions