简体   繁体   English

WordPress category.php在使用query_posts时未按类别排序

[英]WordPress category.php not sorting by category when using query_posts

I'm using the SuperSimple Theme for a wordpress blog and am trying to customize the category.php page. 我将SuperSimple主题用于wordpress博客,并尝试自定义category.php页面。 I want to have a big image for the latest post in each category on top of a smaller grid of all the older posts. 我希望在所有较旧帖子的较小网格上方具有每个类别的最新帖子的大形象。

So far I have it working the way I want, except the top image ( div id="post1" ) is just the most recent post overall instead of the latest post for the category. 到目前为止,我已经按照自己想要的方式工作了,除了顶部图片( div id="post1" )只是整体上的最新帖子,而不是该类别的最新帖子。 Here's one of the category pages: http://meanmargie.com/category/hospitality/ 这是类别页面之一: http : //meanmargie.com/category/hospitality/

And here's the code I have: 这是我的代码:

<header class="header">
<h1 class="entry-title"><?php single_cat_title(); ?></h1>
<?php if ( '' != category_description() ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . category_description() . '</div>' ); ?>
</header>

<div id="post1">
<?php query_posts('showposts=1'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured'); } ?>
<div id="post-info"><a href="<?php the_permalink(); ?>"title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a><br><a href="<?php echo get_permalink(); ?>"> Read More</a></div>
<?php endwhile; endif;  wp_reset_query();?>
</div>
<br>

<div id="post2">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?>
<div id="post-info"><a href="<?php the_permalink(); ?>"title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a><br><a href="<?php echo get_permalink(); ?>"> Read More</a></div>
<?php endwhile; endif; ?>
</div>

First of all, return the category ID of the category page you are on with get_query_var('cat') 首先,使用get_query_var('cat')返回您所在类别页面的类别ID。

$category = get_query_var('cat');
  $cat_id = $category->cat_ID;

Now, return that id back into WP_Query , using 'cat'=> $cat_id, . 现在,使用'cat'=> $cat_id,返回该ID到WP_Query Please note, showposts is depreciated, use posts_per_page instead 请注意, showpostsposts_per_page ,请改用posts_per_page

DO NOT USE query_posts . 不要使用 query_posts You should never ever use query_posts . 您永远不要使用query_posts It breaks and alters the main query and fails outright in most cases with pagination. 它会破坏和更改主查询,并且在大多数情况下会因分页而彻底失败。

Here's the solution I ended up using: 这是我最终使用的解决方案:

<div id="post1">
<?php //new query to limit number of posts
    $wp_query = new WP_Query();
    $wp_query->query($query_string."&posts_per_page=1&paged=".$paged);
?>
<?php WP_Query; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured'); } ?>
<div class="caption"><a href="<?php the_permalink(); ?>"title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a></div><div class="excerpt"><a href="<?php echo get_permalink(); ?>"> Read More</a></div>
<?php endwhile; endif; wp_reset_query();?>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM