【问题标题】:WordPress category.php not sorting by category when using query_postsWordPress category.php 使用 query_posts 时未按类别排序
【发布时间】:2014-07-23 20:16:38
【问题描述】:

我正在将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>

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    首先用get_query_var('cat')返回你所在分类页面的分类ID

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

    现在,使用 'cat'=&gt; $cat_id, 将该 ID 返回到 WP_Query。请注意,showposts 已贬值,请改用posts_per_page

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

    【讨论】:

    • 谢谢,我现在可以使用了。我是 WP 新手,所以我边走边学 php,感谢您的帮助。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      相关资源
      最近更新 更多