php:如果没有内容,如何隐藏列表项

问题描述 投票:0回答:1

在PHP中,我有一个ul显示有3个列表项作为选项卡。对于没有内容的标签,如何防止它们显示?

<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 &#8250;', '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>

Publications tab has no content, so it should not appear

我以为我可以用if语句来做,但它不起作用。对于此特定ul,出版物为空,因此不应显示该选项卡。

谢谢你的帮助。

php wordpress listitem
1个回答
1
投票

您真的需要IF条件,但是您需要在代码中“更早”获取选项卡值以检查条件,如下所示:

<?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 &#8250;', '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>
© www.soinside.com 2019 - 2024. All rights reserved.