Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Making values in meta box conditional? WordPress

  • SOLVED

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>

Answers (5)

2011-08-22

AdamGold answers:


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


LeTune comments:

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

2011-08-22

Utkarsh Kukreti answers:


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


2011-08-22

Ivaylo Draganov answers:

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!

2011-08-22

John Cotton answers:

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>

2011-08-22

Nenad Ticaric answers:

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