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.

I'm looking for cleaner way to display this code; basically I need to display a review score and if it has a review link to link it in, I know there is an easier way but my knowledge is lesser than my current task.

So the way it should work is as follows...

display game_rating, if review_link has a value then link game_rating to review_link

<?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 ); ?>">
        <?php endif; ?>

        <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>

        <?php $review_link = get_post_meta( get_the_ID(), 'game_review_link', true ); if ( ! empty( $review_link ) ) : ?>
        </a>
        <?php endif; ?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

In situations like this sometimes it's easier to get all the data up front and then build the output.

First get the link and rating, then check if the link is empty. If the link isn't empty append it to the rating. Lastly, print the rating to the page.

<?php
$review_link = esc_html( get_post_meta( get_the_ID(), 'game_review_link', true ) ); 
$game_rating = esc_html( get_post_meta( get_the_ID(), 'game_rating', true ) );
if ( ! empty( $review_link ) ) {
    $game_rating = '<a href="' . $review_link . '">' . $game_rating . '</a>';
}
?>

<div class="rating-<?php echo esc_html( get_post_meta( get_the_ID(), 'game_rating', true ) ); ?>">
    <?php echo $game_rating; ?>
</div>
share|improve this answer
    
You, sir, are a legend :) thanks :) –  Dave Burns Aug 15 '13 at 17:35
    
@DaveBurns Thank you kindly! –  epilektric Aug 15 '13 at 19:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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