我在处理 Wordpress 主题时遇到以下问题:
如标题所示,尽管帖子肯定包含图片,但 get_posts() 返回的数组经常为空。
我正在使用以下方法来检索附件数组:
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post_id
) );
现在,$post_id 工作得很好......如果我在上面的代码片段之前回显它,它就会毫无错误地显示出来。我不知道错误在哪里。
为了完整起见,这是整个循环,除了附件检索外,它在各个方面都工作得很好:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : ?>
<?php the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="post-border">
<div class="post-date"><?php edit_post_link('Edit Post', '', ''); ?></div>
<?php if($pagename == 'news'): ?>
<div class="post-date">Posted: <?php the_time('F j, Y') ?></div>
<?php endif; ?>
<h5 class="posttitle"><?php the_title(); ?></h5>
<div class="post-entry">
<?php the_content(); ?>
<?php
$post_id = $post -> ID;
echo $post_id;
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => 20,
'post_parent' => $post_id
) );
print_r($attachments);
?>
<?php echo "<div class='clearboth'></div>"; ?>
</div> <!-- end of .entry -->
</div> <!-- end of .post-border -->
</div> <!-- end of .post -->
<?php endwhile; ?>
<?php endif; ?>
如果有人有任何建议,我将不胜感激!
最好的, J
最佳答案
附件从父帖子继承 post_status
,因此将以下内容添加到 args:
'post_status' => 'inherit',
关于wordpress - get_posts() 对于 post_type 附件总是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16549440/