【问题标题】:Trying to get an attachment from a post in a Wordpress loop but getting all, from all posts试图从 Wordpress 循环中的帖子中获取附件,但从所有帖子中获取所有内容
【发布时间】:2013-10-09 21:14:26
【问题描述】:

首先 - 这是一个主要关于循环™的问题。我也遇到了附件问题,但这在某种程度上是次要的,因为我认为那里有 sn-ps 可能有用。

好的,这就是我想要在首页上做的事情:

  • 从自定义帖子类型“投资组合”中获取 7 个帖子
  • 仅使用第一个,获取特定的附件(以任何可能的方式,无论是文件名,媒体管理器中的顺序......最好,更干净的方式)
  • 与其他 6 个一起,只需获取 the_post_thumbnail() 等等。

现在,我有这个:

<?php
$args = array (
'post_type' => 'portfolio',
'order' => 'DESC',
'posts_per_page' => 7
);

$query = new WP_Query( $args );
$first_post = true; /* we will take the latest work first */
?>

<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>

<?php
if ($first_post):
$first_post = false;
?>

    <div> /* This is the div for the first item */
            <?php /* let's take the first attachment */
            $args = array(
                'post_type' => 'attachment',
                'numberposts' => 1,
                'order' => 'DESC'
                );

            $attachments = get_posts( $args );
            if ( $attachments ) :
                foreach ( $attachments as $attachment ) :
                   echo wp_get_attachment_image( $attachment->ID, 'full' );
                endforeach;
            endif;
            ?>
        </div>
<?php else: ?>
    /* Do something with the other 6 posts and their post_thumbnail */
<?php endif ?>
<?php endwhile; ?>

现在是问题:

  • 首先:如果我在尝试恢复附件时将 'numberposts' 设置为全部 (-1),我会从所有 'portfolio' 帖子中获取所有附件。我不应该只与当前帖子(the_post())进行交互吗?这里我不太理解循环的概念,这是主要问题。

  • 该代码不会让我获得第一个附件,即使它位于该帖子的媒体管理器中的首位。

我应该选择二级循环还是嵌套循环?我已经阅读并重新阅读了法典和其他教程,但仍然无法理解它。

非常感谢!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    用户 Shakti Patel 给了我答案的关键,但并没有真正回答我关于循环的问题,所以就这样吧:

    问题在于get_posts。它实际上对主查询运行并行查询,而不考虑循环的当前步骤。所以我们必须向它询问当前帖子的附件,因为我们处于循环中,所以这些附件存储在$post-&gt;ID 中。因此,知道了这一点,我们必须像这样请求当前帖子的第一个附件:

    $args = array(
        'post_type' => 'attachment',
        'posts_per_page' => 1,
        'post_parent' => $post->ID,
        'exclude'     => get_post_thumbnail_id()
        );
    
    $attachments = get_posts( $args );
    

    这样我们可以指定我们将从哪个帖子中获取第一个附件,并且在我们处理该帖子时,排除帖子缩略图。

    我不知道这是否是最好的方法,因为我们已经在循环中并且我们不应该需要新的查询,我们不应该能够在没有get_posts 位的情况下检索该附件吗?

    无论如何,有关get_posts 的更多信息(在未半睡时阅读):http://codex.wordpress.org/Template_Tags/get_posts

    【讨论】:

      【解决方案2】:

      使用了这个代码:

      $attachments = get_posts( array(
                  'post_type' => 'attachment',
                  'posts_per_page' => -1,
                  'post_parent' => $post->ID,
                  'exclude'     => get_post_thumbnail_id()
              ) );
      
              if ( $attachments ) {
                  foreach ( $attachments as $attachment ) {
                      $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
                      $thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
                      echo $thumbimg;
                  }
      
              }
      

      【讨论】:

      • 第二部分没看懂,附件一。那个块有什么作用?我尝试了第一部分,但是我将它设置为 1 而不是 -1,并且使用额外的 post_parent 和 exclude 参数,它完全符合我的要求,所以谢谢你!尽管如此,我还是不明白循环的概念。为什么我必须使用 post_parent?我不是在任何给定的循环步骤中只与当前帖子交互吗?
      • 现在我再次考虑这个问题......难道get_posts() 正在从主查询中获取所有帖子并且可能忽略了当前循环步骤,这就是为什么需要post_parent
      猜你喜欢
      • 2015-05-10
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多