Wordpress 类别模板显示来自所有类别的帖子,而不是特定类别的帖子
Posted
技术标签:
【中文标题】Wordpress 类别模板显示来自所有类别的帖子,而不是特定类别的帖子【英文标题】:Wordpress category template is showing posts from all categories instead of specific category posts 【发布时间】:2012-11-20 02:07:34 【问题描述】:我有一个存档页面,用于显示特定类别的帖子。但是,它不是在类别页面上仅显示给定类别的帖子,而是显示所有帖子。示例见here。
这是我在archive.php 页面上使用的代码。我知道这是循环使用不当,但我不知道如何修复它。感谢您的帮助。
<?php get_header(); ?>
<div id="content">
<div id="inner-content" class="wrap clearfix">
<h1 class="blogTitle" style="margin:10px 0 3px 0;">Blog Title</h1>
<nav class="blogNav" role="navigation">
<?php bones_blog_nav(); // Adjust using Menus in Wordpress Admin ?>
</nav>
<div id="main" class="eightcol first clearfix" role="main">
<div class="catwhiteBg">
<?php if (is_category()) ?>
<h1 class="catTitle">
<span><?php _e("", "bonestheme"); ?></span> <?php single_cat_title(); ?>
</h1>
<?php echo category_description( $category_id ); ?>
<?php elseif (is_author()) ?>
<div class="authorTop">
<?php
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
?>
<div class="author-pic"><?php echo get_avatar( $curauth->user_email, '80' ); ?></div>
<div class="author-name"><span style="font-weight: 200; color: #575757;">POSTS BY:</span>
<?php echo $curauth->first_name; ?> <?php echo $curauth->last_name; ?>
</div>
<div class="author-bio"><?php echo $curauth->description; ?></div>
<a href="<?php echo $curauth->twitter; ?>" title="Twitter"><div class="author-twitter"><span>twitter</span></div></a>
</div>
<?php elseif (is_day()) ?>
<h1 class="archive-title h2">
<span><?php _e("Daily Archives:", "bonestheme"); ?></span> <?php the_time('l, F j, Y'); ?>
</h1>
<?php elseif (is_month()) ?>
<h1 class="archive-title h2">
<span><?php _e("Monthly Archives:", "bonestheme"); ?></span> <?php the_time('F Y'); ?>
</h1>
<?php elseif (is_year()) ?>
<h1 class="archive-title h2">
<span><?php _e("Yearly Archives:", "bonestheme"); ?></span> <?php the_time('Y'); ?>
</h1>
<?php ?>
</div>
<div class="psts">
<?php
global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ( get_query_var('paged') )
$paged = get_query_var('paged');
elseif ( get_query_var('page') )
$paged = get_query_var('page');
else
$paged = 1;
query_posts(array('posts_per_page' => '8','paged'=>$paged));
?>
<?php
$count = 1;
while (have_posts()) : the_post(); ?>
<div class="sixcol small pst<?php if ((isset($count)) && ($count % 2 == 0 )) echo ' last'; // same logic to add class of last to last item in row of two ?>" id="post-<?php the_ID(); ?>">
<article id="post-<?php the_ID(); ?>" role="article">
<div class="thumb-wrapper mobile">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php if(has_post_thumbnail()) $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'post-thumb' ); echo '<img src="' . $image_src[0] . '" class="post-thumb" />'; ?></a>
<header class="post-thumb-header">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><h2 class="post-title"><?php the_title(); ?></h2></a>
</header> <!-- end article header -->
<p class="meta"><?php the_category(', '); ?></p>
</div>
<section class="mobile-content">
<?php the_excerpt(); ?>
</section>
</article> <!-- end article -->
</div>
<?php $count++; ?>
<?php endwhile; ?>
<nav class="wp-prev-next">
<?php echo rb_pagination(); ?>
</nav>
</div> <!-- end .psts -->
</div> <!-- end #main -->
<?php get_sidebar(); // sidebar 1 ?>
</div> <!-- end #inner-content -->
</div> <!-- end #content -->
<?php get_footer(); ?>
【问题讨论】:
【参考方案1】:我认为您正在用这一行覆盖原始查询:
query_posts(array('posts_per_page' => '8','paged'=>$paged));
你能把这条线换成这条线,然后告诉我结果吗?
global $query_string;
query_posts( $query_string . "&posts_per_page=8&paged=$paged" );
如果失败,试试这个:
query_posts(array('posts_per_page' => '8','paged'=>$paged, 'cat' => $category_id ));
【讨论】:
以上是关于Wordpress 类别模板显示来自所有类别的帖子,而不是特定类别的帖子的主要内容,如果未能解决你的问题,请参考以下文章