有人告诉我get_posts
是一个非常糟糕的函数new WP_query
绝对更好。
我的问题是get_posts
代码可以工作,但当我尝试将其转换为WP_query
, 它返回空白结果。请帮忙?:(我真的很想这样做,因为我怀疑index.php/category-9.php上的get\\u帖子干扰了category.php上的正常循环。)
Get\\u posts版本:
<?php if ( have_posts() ) : while ( (have_posts() ) ) : the_post(); ?>
<div class="postcontainer">
<h2><a href="<?php the_permalink();?>"><?php the_title(\'\'); ?></a></h2>
<?php
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'numberposts\' => 4,
\'post_status\' => null,
\'post_parent\' => $post->ID,
\'orderby\' => name,
\'order\' => ASC
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post); ?>
<a class="postpreview" href="<?php echo get_permalink($post->post_parent) ?>"><?php echo wp_get_attachment_image($post->ID, thumbnail) ?></a>
<?php };
}; ?>
<div class="clear"></div>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
WP\\U查询版本:<?php if ( have_posts() ) : while ( (have_posts() ) ) : the_post(); ?>
<div class="postcontainer">
<h2><a href="<?php the_permalink();?>"><?php the_title(\'\'); ?></a></h2>
<?php
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'numberposts\' => 4,
\'post_status\' => null,
\'post_parent\' => $post->ID,
\'orderby\' => name,
\'order\' => ASC
);
$attachments = new WP_Query($args);
while ($attachments->have_posts()) : ?>
<a class="postpreview" href="<?php echo get_permalink($post->post_parent) ?>"><?php echo wp_get_attachment_image($post->ID, thumbnail) ?></a>
<?php endwhile; wp_reset_postdata(); ?>
<div class="clear"></div>
</div>
<?php endwhile; endif; ?>