[英]How can I combine these two if/else statements for different results?
我目前有這兩個php塊正在拉動結果:
<?php
$webtech = article_custom_field('web-tech');
if ( !empty($webtech) ) :
?>
<div class="tech-list">
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
</div>
<?php
endif;
?>
和
<?php
$url = article_custom_field('site-url');
elseif ( !empty($url) ) :
?>
<div class="site-url"><a href="<?php echo $url; ?>" target="_blank">Visit</a></div>
<?php
endif;
?>
我想將它們組合起來輸出一個單獨的塊,例如:
<div class="tech-list">
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
<div class="site-url"><a href="<?php echo $url; ?>" target="_blank">Visit</a></div>
</div>
它需要滿足以下要求:
如果存在web-tech,則輸出它。 如果不存在,請不要輸出site-url。
如果存在web-tech,則輸出它。 如果site-url存在,則輸出它。
如果site-url存在,則輸出它。 如果不存在,請不要輸出web-tech。
如果兩個變量都不存在,則根本不應輸出包含div。
我錯過了一個明顯的方法嗎? 這似乎微不足道,但我無法將if / else / elseif語句排成一行。
聽起來你需要在輸出它們存在的容器之前檢查這兩個變量。
<?php
$webtech = article_custom_field('web-tech');
$url = article_custom_field('site-url');
if ( !empty($webtech) || !empty($url))
{
?>
<div class="tech-list">
<?php
if ( !empty($webtech) )
{
?>
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
<?php
}
if ( !empty($url) )
{
?>
<div class="site-url"><a href="<?php echo $url; ?>" target="_blank">Visit</a></div>
<?php
}
?>
</div>
<?php
}
?>
您可以將輸出存儲在變量中,如下所示:
<?php
$output = '';
$webtech = article_custom_field('web-tech');
if ( !empty($webtech) ) :
$output .= '<div class="tech-title">Technologies:</div>'
. '<ul class="tech-ul">' . $webtech . '</ul>';
endif;
$url = article_custom_field('site-url');
if(!empty($url)) :
$output .= '<div class="site-url"><a href="' . $url . '" target="_blank">Visit</a></div>';
endif;
if($output != ''):
echo '<div class="tech-list">';
echo $output;
echo '</div>';
endif;
?>
這樣,只有在輸出變量中設置了某些內容時,它才會顯示任何內容。
這會解決您的問題嗎?
if (a or b) then {
OpenTechTitle;
if (a) SetAtext;
if (b) SetBtext;
CloseTechTitle;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.