Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do I get the following code to insert a link only if the custom function has a value?

<td><a href="<?php echo esc_html( get_post_meta( get_the_ID(), 'game_review_link', true ) ); ?>"><div class="rating-<?php echo esc_html( get_post_meta( get_the_ID(), 'game_rating', true ) ); ?>"><?php echo esc_html( get_post_meta( get_the_ID(), 'game_rating', true ) ); ?></div></a></td>

Basically I'm trying to link a review score to a review but if a review hasn't been written I don't want a link to appear.

Thanks in advance.

share|improve this question

closed as off-topic by Chip Bennett, toscho Aug 14 '13 at 18:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions should be specific to WordPress within defined scope (merely happening in its context, such as generic PHP/JS/HTML/CSS, is insufficient). Might be better asked at Stack Overflow or other appropriate site of Stack Exchange network." – Chip Bennett, toscho
If this question can be reworded to fit the rules in the help center, please edit the question.

    
This is primarily a PHP question, and not specific to WordPress. –  Chip Bennett Aug 14 '13 at 16:53
add comment

2 Answers 2

up vote 2 down vote accepted
<td>
<?php $review_link = get_post_meta( get_the_ID(), 'game_review_link', true );
if ( ! empty( $review_link ) ) : ?>
    <a href="<?php echo esc_html( $review_link ); ?>">
        <div class="rating-<?php echo esc_html( $review_link ); ?>">
            <?php echo esc_html( $review_link ); ?>
        </div>
    </a>
<?php endif; ?>
</td>

UPDATE

Thanks to Chip Bennet, this is now working code. Changed isset conditional check to ! empty.

share|improve this answer
1  
get_post_meta() returns either an empty array (if $single is false) or an empty string (if $single is true). So, the return value will always be set. You'll need to check for an empty string in the conditional. –  Chip Bennett Aug 14 '13 at 16:56
    
Thank you for the catch, Chip. I meant to remove the true parameter. I have updated the code in the answer. –  Rachel Baker Aug 14 '13 at 16:59
    
But that $single is kind of important in this context, isn't it? ;) –  Chip Bennett Aug 14 '13 at 17:00
    
+1 But: As in @ChipBennett answer, '' != get_post_meta() should be used instead of a variable as the var is only used once. Avoids filling unnecessary stuff into memory. –  kaiser Aug 14 '13 at 18:10
add comment

Looking at the Codex entry for get_post_meta(), the function returns the specified value (if it exists), or else it returns an empty string.

So, you just need to check for an empty string:

if ( '' != get_post_meta( get_the_ID(), 'game_review_link', true ) ) {
    // Post meta value exists; do something
}
share|improve this answer
1  
@JMa You can use get_the_ID() everywhere. Internally it calls get_post()->post and therefore fetches from the global and even updates on page cache so all future calls are fine as well. –  kaiser Aug 14 '13 at 18:08
    
Ah ok I was refering to the codex ("This tag must be within The Loop.") but I think you're right. –  JMau Aug 14 '13 at 18:39
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.