我现在有这两块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>它需要满足以下方面:
如果网络技术存在,输出它。不要输出站点-url,如果它不存在。
如果网络技术存在,输出它。如果站点-url存在,则输出它。
如果站点-url存在,则输出它。如果不存在,就不要输出网络技术。
如果两个变量都不存在,则不应该输出包含的div。
我错过了一个显而易见的方法吗?这看起来很琐碎,但我无法让if/ get / trivial语句对齐。
发布于 2013-10-18 15:42:22
可以将输出存储在变量中,如下所示:
<?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;
?>这样,只有当您的输出变量中有设置时,它才会显示任何内容。
这能解决你的问题吗?
发布于 2013-10-18 15:41:45
听起来,在输出存在于其中的容器之前,需要检查这两个变量。
<?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
}
?>发布于 2013-10-18 15:20:08
if (a or b) then {
OpenTechTitle;
if (a) SetAtext;
if (b) SetBtext;
CloseTechTitle;
}https://stackoverflow.com/questions/19453107
复制相似问题