php: 如何在没有内容的情况下隐藏列表项
php: How to hide a list item if there is no content
在 php 中,我显示了一个带有 3 个列表项作为选项卡的 ul。对于没有内容的标签,如何让它们不显示?
<div class="grid col-540 fit" id="tabs">
<ul>
<li><a href="#tabs-1">Profile</a></li>
<li><a href="#tabs-2">Project Experience</a></li>
<?php if (!empty(get('bio_publications')) { ?>
<li><a href="#tabs-3">Publications</a></li>
<?php } ?><?php endif ?>
</ul>
<div class="profile" id="tabs-1">
<h2>Profile</h2>
<?php the_content( __( 'Read more ›', 'responsive' ) ); ?>
</div>
<div class="project-experience" id="tabs-2">
<h2>Project Experience</h2>
<?php echo get('bio_project_experience'); ?>
</div>
<div class="publications" id="tabs-3">
<h2>Publications</h2>
<?php echo get('bio_publications'); ?>
</div>
</div>
我以为我可以用 if 语句来完成,但它不起作用。此特定 ul 的出版物为空,因此不应显示该选项卡。
感谢您的帮助。
您确实需要 IF 条件,但您需要在代码中获取制表符值 'earlier' 来检查条件,如下所示:
<?php
//get your stuff
$content = get_the_content();
$bio_project_experience = get('bio_project_experience');
$bio_publications = get('bio_publications');
?>
<div class="grid col-540 fit" id="tabs">
<ul>
<?php if ($content): ?><li><a href="#tabs-1">Profile</a></li><?php endif; ?>
<?php if ($bio_project_experience): ?><li><a href="#tabs-2">Project Experience</a></li><?php endif; ?>
<?php if ($bio_publications): ?> <li><a href="#tabs-3">Publications</a></li><?php endif ?>
</ul>
<?php if ($content): ?>
<div class="profile" id="tabs-1">
<h2>Profile</h2>
<?php the_content( __( 'Read more ›', 'responsive' ) ); ?>
</div>
<?php endif; ?>
<?php if ($bio_project_experience): ?>
<div class="project-experience" id="tabs-2">
<h2>Project Experience</h2>
<?php echo get('bio_project_experience'); ?>
</div>
<?php endif; ?>
<?php if ($bio_publications): ?>
<div class="publications" id="tabs-3">
<h2>Publications</h2>
<?php echo get('bio_publications'); ?>
</div>
<?php endif; ?>
</div>
在 php 中,我显示了一个带有 3 个列表项作为选项卡的 ul。对于没有内容的标签,如何让它们不显示?
<div class="grid col-540 fit" id="tabs">
<ul>
<li><a href="#tabs-1">Profile</a></li>
<li><a href="#tabs-2">Project Experience</a></li>
<?php if (!empty(get('bio_publications')) { ?>
<li><a href="#tabs-3">Publications</a></li>
<?php } ?><?php endif ?>
</ul>
<div class="profile" id="tabs-1">
<h2>Profile</h2>
<?php the_content( __( 'Read more ›', 'responsive' ) ); ?>
</div>
<div class="project-experience" id="tabs-2">
<h2>Project Experience</h2>
<?php echo get('bio_project_experience'); ?>
</div>
<div class="publications" id="tabs-3">
<h2>Publications</h2>
<?php echo get('bio_publications'); ?>
</div>
</div>
我以为我可以用 if 语句来完成,但它不起作用。此特定 ul 的出版物为空,因此不应显示该选项卡。
感谢您的帮助。
您确实需要 IF 条件,但您需要在代码中获取制表符值 'earlier' 来检查条件,如下所示:
<?php
//get your stuff
$content = get_the_content();
$bio_project_experience = get('bio_project_experience');
$bio_publications = get('bio_publications');
?>
<div class="grid col-540 fit" id="tabs">
<ul>
<?php if ($content): ?><li><a href="#tabs-1">Profile</a></li><?php endif; ?>
<?php if ($bio_project_experience): ?><li><a href="#tabs-2">Project Experience</a></li><?php endif; ?>
<?php if ($bio_publications): ?> <li><a href="#tabs-3">Publications</a></li><?php endif ?>
</ul>
<?php if ($content): ?>
<div class="profile" id="tabs-1">
<h2>Profile</h2>
<?php the_content( __( 'Read more ›', 'responsive' ) ); ?>
</div>
<?php endif; ?>
<?php if ($bio_project_experience): ?>
<div class="project-experience" id="tabs-2">
<h2>Project Experience</h2>
<?php echo get('bio_project_experience'); ?>
</div>
<?php endif; ?>
<?php if ($bio_publications): ?>
<div class="publications" id="tabs-3">
<h2>Publications</h2>
<?php echo get('bio_publications'); ?>
</div>
<?php endif; ?>
</div>