如果metabox为空,则显示wordpress中的其他内容



我试图替换或显示其他内容,如果metabox是空的,但我不能使它工作,这就是我所做的。

如果"ytvideourl"是空显示"ytembed"iframe内容

<div class="trailer"><iframe src="<?php echo get_post_meta($post->ID, 'ytvideourl', true);?>
<?php if( ! empty( $ytvideourl ) ) : ?>
<iframe src="https://www.youtube.com/embed/<?php echo get_post_meta($post->ID, 'ytembed', true);?>?rel=0">
<?php endif; ?> ?rel=0"></iframe></div>

当前代码:

<div class="trailer">
<?php
$ytvideourl = get_post_meta($post->ID, 'ytvideourl', true);
?>

<?php if( ! empty( $ytvideourl ) ) : ?>
<iframe src="<?php echo $ytvideourl; ?>"></iframe>
<?php else : ?>
<iframe src="https://www.youtube.com/embed/<?php echo get_post_meta($post->ID, 'ytembed', true);?>?rel=0?" width="400" height="230" frameborder="0" allowfullscreen="allowfullscreen"><span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span><span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span><span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span><span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span></iframe>  
<?php endif; ?>
</div>

首先,必须将元值存储到一个变量中。然后检查该变量是否为空。如果不为空,则使用该值。否则,如果它为空,则获取第二个元值并使用它。

示例如下:

<?php
$ytvideourl = get_post_meta($post->ID, 'ytvideourl', true);
if( $ytvideourl !== false ) $ytvideourl = trim( $ytvideourl ); //-- remove any white spaces
?>
<div class="trailer">
<?php if( ! empty( $ytvideourl ) ) : ?>
<iframe src="<?php echo $ytvideourl; ?>"></iframe>
<?php else : ?>
<iframe src="https://www.youtube.com/embed/<?php echo get_post_meta($post->ID, 'ytembed', true);?>?rel=0"></iframe>  
<?php endif; ?>
</div>

最新更新