【发布时间】: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())进行交互吗?这里我不太理解循环的概念,这是主要问题。
该代码不会让我获得第一个附件,即使它位于该帖子的媒体管理器中的首位。
我应该选择二级循环还是嵌套循环?我已经阅读并重新阅读了法典和其他教程,但仍然无法理解它。
非常感谢!
【问题讨论】: