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 only want one of these shortcodes to display if the custom field value is true. I'm not understanding the logic to do this. So basically, if there is a twitch video and a own3d video I only want the top video (twitch) to display. So on and so forth. Is this beyond the if else statement? I'm adding this code of course to my template.php file.

My second question is would this code be better off using the switch method?

<?php
$twitch = get_post_meta( $post->ID, 'twitch', $single = true );
$own3d = get_post_meta( $post->ID, 'own3d', $single = true );
$livestream = get_post_meta( $post->ID, 'livestream', $single = true );
$ustream = get_post_meta( $post->ID, 'ustream', $single = true );
$justin = get_post_meta( $post->ID, 'justin', $single = true );


if ($twitch == '') {
}
else { 
echo do_shortcode('[livestream type="twitch" channel="' . $twitch . '"]');
}

if ($own3d == '') {
}
else { 
echo do_shortcode('[livestream type="own3d" channel="' . $own3d . '"]');
}
if ($livestream == '') {
}

else { echo do_shortcode('[livestream type="livestream" channel="' . $livestream . '"]'); }

if ($ustream == '') {
}
else {
echo do_shortcode('[livestream type="ustream" channel="' . $ustream . '"]');
}

if ($justin == '') {
}
else {
echo do_shortcode('[livestream type="justin" channel="' . $justin . '"]');
}
?>
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

This is rather a simple PHP question and almost off topic. You can simplify your code drastically:

$metas = array ( 'twitch', 'own3d', 'livestream', 'ustream', 'justin' );
foreach ( $metas as $meta )
{
    $value = get_post_meta( $post->ID, $meta, TRUE );
    if ( $value )
    {
        echo do_shortcode("[livestream type='$meta' channel='$value']");
        break;
    }
}

This will run over the list of possible meta fields and stop if it finds one with content.

I wouldn’t use do_shortcode() like this. Use the shortcode handler function instead (the function that is called by this shortcode) to speed the processing up.
Use do_shortcode() only if you have content outside of the shortcode.

share|improve this answer
    
I'm trying to learn. I apologize if it was off topic. More apologies if I'm to newbie for this website. –  TheBetterJORT Jul 4 '12 at 3:40
    
Forgot to thank you! Thank you! I understand the array now. :) –  TheBetterJORT Jul 4 '12 at 3:41
add comment

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.