【问题标题】:Hide DIV if Attachment does not exist如果附件不存在则隐藏 DIV
【发布时间】:2020-08-03 11:03:35
【问题描述】:

如果附件不存在,您需要一些帮助来隐藏此 div。我认为这可能是一个非常简单的解决方案,我的 PHP 是 poooooooor。请有任何建议。

   <!--Include Attachments to Post-->
<div class="attachments">
    <?php $attachments = new Attachments( 'attachments' ); /* pass the instance name */ ?>
        <?php if( $attachments->exist() ) : ?>
            <h3><strong>Attachments</strong></h3>
            </br>
            <?php while( $attachments->get() ) : ?>
            <h5><strong>Name:</strong> <?php echo $attachments->field( 'title' ); ?></h5>
            <h5><strong>Size:</strong> <?php echo $attachments->filesize(); ?></h5>
            <h5><strong><a href="<?php echo $attachments->url(); ?>"target="_blank">Download File</a></strong></h5>
            <?php endwhile; ?>
            <?php endif; ?>
</div>

【问题讨论】:

  • 结果如何?为什么不用if ($attachments-&gt;exist()) { .... } 而不是这个奇怪的结构?
  • 而且它会一直存在,因为你刚刚创建它不是吗?
  • @mplungjan 这个 sn-p 向我们的帖子页面添加了一个区域,如果有附件,则通过它们提取相关信息。当帖子编辑器中没有添加文件时,它会在前端显示一个空白区域。如果是这样,我们想尝试隐藏 div。这可以通过内联 CSS、Java 来完成,还是我们可以根据帖子元数据隐藏它?

标签: javascript php css


【解决方案1】:

给朋友打了电话,得到了结果。只需要在 if 语句中移动 div 即可。

   <!--Include Attachments to Post-->
    <?php $attachments = new Attachments( 'attachments' ); /* pass the instance name */ ?>
        <?php if( $attachments->exist() ) : ?>
           <div class="attachments">
            <h3><strong>Attachments</strong></h3>
            </br>
            <?php while( $attachments->get() ) : ?>
            <h5><strong>Name:</strong> <?php echo $attachments->field( 'title' ); ?></h5>
            <h5><strong>Size:</strong> <?php echo $attachments->filesize(); ?></h5>
            <h5><strong><a href="<?php echo $attachments->url(); ?>"target="_blank">Download File</a></strong></h5>
            <?php endwhile; ?>
            </div>
            <?php endif; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2023-03-15
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多