0

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

1 Answer 1

1

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

Your Answer

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

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