显示所有附件,但如果没有附件则回显为空

发布于 2024-11-07 08:59:27 字数 1196 浏览 0 评论 0原文

好的,以下是我在类别和标签页面上的代码。它显示给定类别或标签中最近 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

Okay, the following is code that I have on category, and tag pages. It shows all the attachments of the 8 most recent posts, in a given category or tag. So if I'm on the 'cars' category page, it displays only photos from posts that were categorized as 'cars'. That part works great.

What I'm trying to do is get it so that even if there are several posts in that category, if none of those posts have attachments, it will echo something like, "sorry no photos here".

<?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; ?>

I've tried adding an else statement, like this:

<?php endwhile; else: ?>
 <p>sorry no photos here</p>
 <?php endif; ?>

However, if one post has a photo but the other doesn't it displays that one photo but it also echos the else statement.

Phew! Long explanation. This isn't life or death but I've spent so much time on it to no avail, I'm probably way more bugged by it than I should be. :/

Thanks in advance!

Wordpress source: http://codex.wordpress.org/Template_Tags/get_posts#Show_all_attachments

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜吻♂芭芘 2024-11-14 08:59:27

您的 else 位于错误的位置,它充当 have_posts() 上的 else 条件,您需要它成为 if ($attachments) 上的 else 条件,如下所示:

if ($attachments) {
    foreach ( $attachments as $attachment ) {
        echo '<li>';
        the_attachment_link( $attachment->ID , false );
        echo '</li>';
    }
}
else
{
    echo "sorry no photos here";
}

如果这不起作用,请尝试:

if (count($attachments)>0) {
    foreach ( $attachments as $attachment ) {
        echo '<li>';
        the_attachment_link( $attachment->ID , false );
        echo '</li>';
    }
}
else
{
    echo "sorry no photos here";
}

Your else is in the wrong place, it's acting as an else condition on have_posts() you need it to be an else condition on if ($attachments) like so:

if ($attachments) {
    foreach ( $attachments as $attachment ) {
        echo '<li>';
        the_attachment_link( $attachment->ID , false );
        echo '</li>';
    }
}
else
{
    echo "sorry no photos here";
}

and if that doesn't work try:

if (count($attachments)>0) {
    foreach ( $attachments as $attachment ) {
        echo '<li>';
        the_attachment_link( $attachment->ID , false );
        echo '</li>';
    }
}
else
{
    echo "sorry no photos here";
}
别闹i 2024-11-14 08:59:27

抱歉,我工作时停电了...

所以我这样做了:

if ($attachments) {
    var_dump($attachments);
    foreach ( $attachments as $attachment ) {
        echo '<li>';
        the_attachment_link( $attachment->ID , false );
        echo '</li>';
    }
}
else
{
    echo "sorry no photos here";
}

并得到了:

NULL array(1) { [0]=>对象(stdClass)#567 (32) { ["ID"]=> int(65) ["post_author"]=> string(1) "1" ["post_date"]=>;字符串(19)“2011-03-24 03:50:31”[“post_date_gmt”]=>字符串(19)“2011-03-24 10:50:31”[“post_content”]==>字符串(0)“”[“post_title”]=> string(7) "Joe Ahn" ["show_in_menu"]=>字符串(1) "1" ["link_link"]=>字符串(1)“1”[“no_follow_link”]=>字符串(1)“0”[“alt_link_text”]=> NULL [“custom_link_class”]=> NULL [“redirect_url”] => NULL [“target_blank”]=>字符串(1) "0" ["alt_title_attribute"]=> NULL [“post_excerpt”] =>字符串(0)“”[“post_status”]=> string(7) "继承" ["comment_status"]=>字符串(4)“打开”[“ping_status”]=>字符串(4)“打开”[“post_password”]=>字符串(0)“”[“post_name”]=>字符串(3) "ahn" ["to_ping"]=>字符串(0)“”[“pinged”]=>字符串(0)“”[“post_modified”]=>字符串(19)“2011-03-24 03:50:31”[“post_modified_gmt”]=>字符串(19)“2011-03-24 10:50:31”[“post_content_filtered”]=>字符串(0)“”[“post_parent”]=> int(64) ["guid"]==>字符串(66)“http://www.ouhsd.k12.ca.us/news/wp-content/uploads/2011/03/ahn.jpg”[“menu_order”]=> int(0) ["post_type"]=>;字符串(10)“附件”[“post_mime_type”]=>字符串(10)“图像/jpeg”[“comment_count”]=>字符串(1)“0”[“过滤器”]=>字符串(3)“原始”}}

Sorry, the power went out at my work...

So I did this:

if ($attachments) {
    var_dump($attachments);
    foreach ( $attachments as $attachment ) {
        echo '<li>';
        the_attachment_link( $attachment->ID , false );
        echo '</li>';
    }
}
else
{
    echo "sorry no photos here";
}

AND got this:

NULL array(1) { [0]=> object(stdClass)#567 (32) { ["ID"]=> int(65) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2011-03-24 03:50:31" ["post_date_gmt"]=> string(19) "2011-03-24 10:50:31" ["post_content"]=> string(0) "" ["post_title"]=> string(7) "Joe Ahn" ["show_in_menu"]=> string(1) "1" ["link_link"]=> string(1) "1" ["no_follow_link"]=> string(1) "0" ["alt_link_text"]=> NULL ["custom_link_class"]=> NULL ["redirect_url"]=> NULL ["target_blank"]=> string(1) "0" ["alt_title_attribute"]=> NULL ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "inherit" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(3) "ahn" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2011-03-24 03:50:31" ["post_modified_gmt"]=> string(19) "2011-03-24 10:50:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(64) ["guid"]=> string(66) "http://www.ouhsd.k12.ca.us/news/wp-content/uploads/2011/03/ahn.jpg" ["menu_order"]=> int(0) ["post_type"]=> string(10) "attachment" ["post_mime_type"]=> string(10) "image/jpeg" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文