logo

$20
Making values in meta box conditional?

Unsure if the title is fitting, let me break it down. I am adding a jquery slider to my theme, using a meta box with 5 different text fields for pasting the image url. So far so good.

But what I want to achieve is to make each text field in the meta box, so if no image url is added it won't show in the slider as an image, or broken image as the url is not there.

So how do I make the values in the meta box conditional? Here's the code I suspect needs to tweaking:

<div id="slider" class="nivoSlider">

<img src="<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE); echo $my_meta['urltwo']; ?>" alt="" />

<img src="<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE); echo $my_meta['urlthree']; ?>" alt="" />

<img src="<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE); echo $my_meta['urlfour']; ?>" alt="" />

<img src="<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE); echo $my_meta['urlfive']; ?>" alt="" />

</div>

LeTune | 08/22/11 at 4:22am | Edit


(5) Possible Answers Submitted...

  • avatar
    Last edited:
    08/22/11
    4:32am
    Utkarsh Kukreti says:


    <div id="slider" class="nivoSlider">
    <?php
    $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);

    foreach(array('urltwo', 'urlthree', 'urlfour', 'urlfive') as $key) {
    if(isset($my_meta[$key])) {
    echo "<img src='" . $my_meta[$key] . "'/>";
    }
    }

    ?>
    </div>


    Previous versions of this answer: 08/22/11 at 4:28am | 08/22/11 at 4:28am | 08/22/11 at 4:32am

  • avatar
    Last edited:
    08/22/11
    4:28am
    AdamGold says:


    <?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE); ?>
    <div id="slider" class="nivoSlider">


    <?php if( isset($my_meta['urltwo']) && $my_meta['urltwo'] != '' ) { ?>
    <img src="<?php echo $my_meta['urltwo']; ?>" alt="" />
    <?php } ?>


    <?php if( isset($my_meta['urlthree']) && $my_meta['urlthree'] != '' ) { ?>
    <img src="<?php echo $my_meta['urlthree']; ?>" alt="" />
    <?php } ?>

    <?php if( isset($my_meta['urlfour']) && $my_meta['urlfour'] != '' ) { ?>
    <img src="<?php echo $my_meta['urlfour']; ?>" alt="" />
    <?php } ?>

    <?php if( isset($my_meta['urlfive']) && $my_meta['urlfive'] != '' ) { ?>
    <img src="<?php echo $my_meta['urlfive']; ?>" alt="" />
    <?php } ?>


    </div>


    What I am doing here is making sure the value even exists before making an image with it.

    • 08/22/11 4:47am

      LeTune says:

      thanks Adam, that did the trick. Let me know if you are interested in working for us at MonsterThemes.com as dedicated wp developer.

  • avatar
    Last edited:
    08/22/11
    4:31am
    Ivaylo Draganov says:

    Hello,

    simply change the queries to your meta values to include an 'if' statement before them:


    <?php if ( get_post_meta( $post->ID,'_my_meta', TRUE ) && $my_meta['urltwo'] != '' ) {
    $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
    ?>
    <img src="<?php echo $my_meta['urltwo']; ?>" alt="" />
    <?php } ?>


    * also keep the variable out of the img src attribute

    Reformat all your img tags like this and let me know how it went. Cheers!

    Previous versions of this answer: 08/22/11 at 4:31am

  • avatar
    Last edited:
    08/22/11
    4:31am
    John Cotton says:

    How about this?

    <div id="slider" class="nivoSlider">

    <?php
    $meta = get_post_meta($post->ID,'_my_meta',TRUE);

    if ( $meta) {

    $fields = array ( 'urltwo', 'urlthree', 'urlfour', 'urlfive' );

    foreach( $fields as $field ) {

    if( !empty( $meta[$field] ) ) {
    echo '<img src="'.$meta[$field].'" alt="" />';
    }
    }
    }
    ?>
    </div>

  • avatar
    Last edited:
    08/22/11
    4:35am
    Nenad Ticaric says:

    <?php
    if(get_post_meta($post->ID,'_my_meta',TRUE)){
    $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
    foreach($my_meta as $meta){
    ?>
    <img src="<?php echo $meta; ?>" />
    <?php
    }
    }
    ?>

    Previous versions of this answer: 08/22/11 at 4:35am

This question has expired.



AdamGold had additional discourse to offer.

LeTune voted on this question.



Current status of this question: Completed