【问题标题】:Show all attachments, but if no attachments echo empty显示所有附件,但如果没有附件回显为空
【发布时间】: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

【问题讨论】:

  • 在模板中的任何循环之外执行此操作。

标签: php wordpress


【解决方案1】:

你的 else 在错误的地方,它在 has_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";
}

【讨论】:

  • 遗憾的是,他们都没有工作。 :/我真的认为第二个会起作用。我认为这是因为“附件”代表是否找到了帖子,我只是认为没有办法区分有帖子和有附件的帖子。我删除了 if 和 while 语句,它在没有照片的页面上工作,它显示“没有照片”,但是当我转到一个应该有照片的页面时,它只显示了上一篇文章中的照片。在这方面,您的两个解决方案都一样。还有其他建议吗?感谢您的帮助!
  • 您可以将var_dump($attachments); 作为 if 的第一行并报告您的发现吗?
【解决方案2】:

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

所以我这样做了:

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]=> object(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"] => string(0) "" ["post_title"]=> string(7) "Joe Ahn" ["show_in_menu"]=> string(1) "1" ["link_link"]=> string(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"]=> string(0) "" ["post_status"]=> string(7) "继承" ["comment_status"]=> string(4) " open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(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"]=> string(10) "attachment" ["post_mime_type"]=> string (10) "image/jpeg" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } }

【讨论】:

  • 哦,我应该补充一点,它显示了所有这些,只有一张照片,仍然是上一篇文章中的那张。
猜你喜欢
  • 1970-01-01
  • 2013-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 2018-02-15
相关资源
最近更新 更多