繁体   English   中英

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

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

我将SuperSimple主题用于wordpress博客,并尝试自定义category.php页面。 我希望在所有较旧帖子的较小网格上方具有每个类别的最新帖子的大形象。

到目前为止,我已经按照自己想要的方式工作了,除了顶部图片( div id="post1" )只是整体上的最新帖子,而不是该类别的最新帖子。 这是类别页面之一: http : //meanmargie.com/category/hospitality/

这是我的代码:

<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>

首先,使用get_query_var('cat')返回您所在类别页面的类别ID。

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

现在,使用'cat'=> $cat_id,返回该ID到WP_Query 请注意, showpostsposts_per_page ,请改用posts_per_page

不要使用 query_posts 您永远不要使用query_posts 它会破坏和更改主查询,并且在大多数情况下会因分页而彻底失败。

这是我最终使用的解决方案:

<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