【发布时间】:2011-05-14 01:27:26
【问题描述】:
好的,以下是我在类别和标签页面上的代码。它显示给定类别或标签中 8 个最新帖子的所有附件。因此,如果我在“汽车”类别页面上,它只会显示归类为“汽车”的帖子中的照片。这部分效果很好。
我要做的是得到它,这样即使该类别中有多个帖子,如果这些帖子都没有附件,它会回显类似“对不起,这里没有照片”之类的内容。
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => 8, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo '<li>';
the_attachment_link( $attachment->ID , false );
echo '</li>';
}
}
?> <?php endwhile; ?>
<?php endif; ?>
我尝试添加一个 else 语句,如下所示:
<?php endwhile; else: ?>
<p>sorry no photos here</p>
<?php endif; ?>
但是,如果一个帖子有照片,而另一个帖子没有,它会显示那张照片,但它也会与 else 语句相呼应。
呸!长解释。这不是生死攸关的事,但我花了这么多时间却无济于事,我可能对它的困扰比我应该的要多。 :/
提前致谢!
Wordpress 来源:http://codex.wordpress.org/Template_Tags/get_posts#Show_all_attachments
【问题讨论】:
-
在模板中的任何循环之外执行此操作。