• Hey all,

    I’ve got a theme that employs a rating system that is set up on the post information screen and, once published, enforces the custom field ‘pyre_overall_score’ between 0.5 and 5, which decides what image to display.

    On my category template I’m trying to enforce the idea that if it contains the custom field that it should display the appropriate image. That works.

    However, even if you haven’t filled out the “rating details” section of the post screen, it still creates the custom field. Even more irritatingly, it is impossible to remove – if you delete the field it just comes back.

    So here’s my code:

    <?php $reviewvalue = get_post_custom_values("pyre_overall_score");
    
    if ($reviewvalue == '')  {
    			echo('');
         }
    else {  ?>
         <div class="post-review-widget"><img src="<?php bloginfo('template_directory'); ?>/images/stars/<?php echo get_post_meta(get_the_ID(), 'pyre_overall_score', true); ?>.png" alt=""/></div>
       <?php } ?>

    However, when doing this, obviously any post created with this theme makes the ‘pyre_overall_score’ custom field and, if there’s no data, just leaves the setting as 0, which means it pulls up the image ‘images/stars/0.png’ (which doesn’t exist and I’d prefer not to make it at all and have it ignored).

    So my alteration was to add inbetween:

    elseif ($reviewvalue == '0') {
    			echo('');

    …but obviously it wouldn’t work (or I wouldn’t be here!).

    I’ve tried just about everything, including putting another ‘if’ statement within the ‘else’ section etc etc.

    All my review posts are in the categories:
    1. Movies – Reviews – In Cinemas
    2. Movies – Reviews – On Blu-ray
    3. Gaming – Reviews
    4. Music – Reviews – Albums
    5. Music – Reviews – Concerts

    This being the case I was wondering if I could put a condition that it only creates the image if it’s in those categories, but if that’s the case, how would I go about doing that? (And that’s if the other option won’t work!!)

    C’mon bright sparks, I’m new to this, so I’m looking for some golden ideas from you wonderful people 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can use var_dump to see exactly what it’s returning:

    echo '<!--';
    var_dump( $reviewvalue );
    echo '-->';

    If I were making this, I would do something like:

    $review_value = get_post_meta( get_the_ID(), 'pyre_overall_score', true );
    
    if ( ! empty( $review_value ) ) {
        <div class="post-review-widget"><img src="<?php bloginfo( 'template_directory' ); ?>/images/stars/<?php echo $review_value; ?>.png" alt=""/></div>
    }

    If you can’t get it to work, you can check the category with in_category(). For example:

    if ( in_category( array( 27, 34, 89, 126 ) ) {
        /* Whatever */
    }
    Thread Starter magicwings

    (@magicwings)

    Using if (! empty( $review_value ) ) worked like a charm.

    Big bagel, you are a Superman amongst a world of Clark Kents.

    If I were wearing a hat, I would tip it for you, good sir.

    Thanks a million!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘If/Else – Custom Field Problems’ is closed to new replies.