0

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 . '"]');
}
?>
1

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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