我正在尝试在我的 wordpress 自定义主题中创建一个相关的帖子部分。这里的目标是,如果用户查看某个类别的帖子,它还将在下面显示该类别的所有帖子。我在下面附上了一张图片。
http://i255.photobucket.com/albums/hh140/testament1234/relatedposts_zpsa830adfc.jpg
我如何实现这一目标?我像这样使用 wp_query 吗?
<?php query_posts('cat=6', 'posts_per_page=-4=-'); if(have_posts()) : while(have_posts()) :the_post(); ?>
上面的代码设置了它获取帖子的类别。'猫'
kwncc的解决方案
<div id="post-container" class="eleven columns alpha omega post">
<?php $postCategories = ''; while ( have_posts() ) : the_post(); ?>
<h2 class="post-title"><?php the_title() ?></h2>
<?php setPostViews(get_the_ID()); ?> <!-- Set Post Views -->
<ul class="meta-icons-large">
<li id="meta-author-large"><span><?php the_author_posts_link() ?> /</span></li>
<li id="meta-categories-large"><span><?php the_category(', ') ?> /</span></li>
<li id="meta-comments-large"><span><?php comments_number() ?> /</span></li>
<li id="meta-date-large"><span><?php the_time('F jS, Y') ?> /</span></li>
<li id="meta-views-large"><span><?php echo getPostViews(get_the_ID()); ?></span></li> <!-- Display Post Views -->
</ul>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div id="related-posts-container" class="eleven columns alpha omega related-post">
<h2>related posts</h2>
<?php $postCatIds = ''; foreach($postCategories as $catIndex => &$catValue){
$postCatIds .= $category->cat_ID; if( $catIndex < (count($postCategories)-1)){ $postCatIds .= ', ';
}
}
?>
<?php query_posts('cat='.$postCatIds, 'posts_per_page=4'); if(have_posts()) : while(have_posts()) { the_post(); } ?>
<div id="related-post-thumbnail-container" class="three columns alpha related-posts">
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php endwhile; endif; wp_reset_query();?>
</div>
<div id="comments-container" class="eleven columns alpha omega comments">
<?php comments_template('', true); ?>
</div>
</div>