如果一个元键值为空,则尝试获取另一个元键,如果两个元键都为空,则显示其他内容?

时间:2018-04-27 作者:MLL

我有以下代码:

<div class="video-embed" style="margin-top: 0px; z-index:102; margin-bottom: 0px;">
<?php

$url1 = get_post_meta( $post->ID , \'video_play\' , true );
$search     = \'#(.*?)(?:href="https?://)?(?:www\\.)?(?:youtu\\.be/|youtube\\.com(?:/embed/|/v/|/watch?.*?v=))([\\w\\-]{10,12}).*#x\';
$replace    = \'http://www.youtube.com/embed/$2\';
$url        = preg_replace($search,$replace,$url1);

?>

<iframe width="560" height="315" src="<?php echo $url; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

</div>
现在我需要实现功能。。。

如果video\\u play为空,请尝试获取video\\u stop$url1 = get_post_meta( $post->ID , \'video_stop\' , true );, 如果video\\u play和video\\u stop为空,则显示文本“此视频已删除”。

2 个回复
最合适的回答,由SO网友:Bikash Waiba 整理而成

您可以使用if-else:

<div class="video-embed" style="margin-top: 0px; z-index:102; margin-bottom: 0px;">

<?php

$url1 = get_post_meta( $post->ID , \'video_play\' , true ) ?: get_post_meta( $post->ID , \'video_stop\' , true ) ?: false;

if( $url1 ):
    $search     = \'#(.*?)(?:href="https?://)?(?:www\\.)?(?:youtu\\.be/|youtube\\.com(?:/embed/|/v/|/watch?.*?v=))([\\w\\-]{10,12}).*#x\';
    $replace    = \'http://www.youtube.com/embed/$2\';
    $url        = preg_replace($search,$replace,$url1);

?>

<iframe width="560" height="315" src="<?php echo $url; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<?php 
else: 
    echo "This video is removed";
endif;    
?>
</div>

SO网友:MLL

以上答案是正确的,如果用户登录以进行video\\u stop,则只需添加。

<div class="video-embed" style="margin-top: 0px; z-index:102; margin-bottom: 0px;">

<?php

if ( is_user_logged_in() ) {
   $url1 = get_post_meta( $post->ID , \'video_play\' , true ) ?: get_post_meta( $post->ID , \'video_stop\' , true ) ?: false;
  } else {
    $url1 = get_post_meta( $post->ID , \'video_play\' , true ) ?: false; 
  }

if( $url1 ):
    $search     = \'#(.*?)(?:href="https?://)?(?:www\\.)?(?:youtu\\.be/|youtube\\.com(?:/embed/|/v/|/watch?.*?v=))([\\w\\-]{10,12}).*#x\';
    $replace    = \'http://www.youtube.com/embed/$2\';
    $url        = preg_replace($search,$replace,$url1);

?>


<iframe width="560" height="315" src="<?php echo $url; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<?php 
else: 
    echo \'<div id="removed" style="
left: 0px;
right: 0px;
background: #111;
position: relative;
padding-bottom: 56.25%;
padding-top: 0px;
height: 0;
text-align: center;
"><p style="position: absolute;
    top: 45%;
    left: 0;
    width: 100%;
    height: auto;
    font-size: 25px;
    color: white;
    font-weight: bold;">
    Video is removed!
    </a></p></div>\';
endif;    
?>
</div>

结束

相关推荐