【问题标题】:Loop for a specific category for homepage循环获取主页的特定类别
【发布时间】:2014-08-30 18:15:27
【问题描述】:

对于我的主页,我想显示特定类别的帖子(例如类别“食物”),而不是默认显示所有帖子。

如何做到这一点?我是否应该使用wp_query() 并避免使用query_posts()? 另外,我需要用wp_reset_postdata(); 重置index.php 中的这个循环吗?

我已经阅读了 codex 并在 Google 上四处搜索,但仍然对更改 Loop 没有信心。

我认为下面的代码可以工作,但我在 WordPress 主题 2012 和 2013 中出现白屏死机。我将 index.php 中的代码替换为

<?php
$query = new WP_Query('cat=1');
if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();
        the_title();
    endwhile;
endif;
?>

编辑:

白屏死机是由于我的index.php 文件中的其他地方缺少?&gt;。菜鸟错误。但现在我遇到了分页问题。第 2 页显示与第 1 页相同的帖子。可能有什么问题?

我在functions.php中的分页代码

if ( ! function_exists( 'my_pagination' ) ) :
    function my_pagination() {
        global $wp_query;
        $big = 999999999; // need an unlikely integer   
        echo paginate_links( array(
             'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 
             'format' => '?paged=%#%',
             'current' => max( 1, get_query_var('paged') ), 
             'prev_next'    => True,
             'total' => $wp_query->max_num_pages
        ) );
}

.
.
编辑:

我在下面使用了 Pieter Goosen 的解决方案。问题已解决。

【问题讨论】:

  • 此代码块没有损坏,因此页面上的其他地方可能存在错误。你检查过你的错误日志吗?

标签: php wordpress


【解决方案1】:

您不应将主查询更改为主页或任何存档页面上的自定义查询。自定义查询适用于次要内容,而不是显示您的主要内容。我在 WPSE 上就此事发表了一篇非常详细的帖子,您可以查看 here。它还介绍了为什么您应该永远不要使用 query_posts

要解决您的问题,请从 index.php 中删除您的自定义查询,然后添加默认循环,如

if(have_posts()){
   while(have_posts()) {
     the_post();

     //YOUR LOOP ELEMENTS

   }
} 

您现在将再次在主页上看到所有帖子。要仅在您的主页上显示所选类别,请在执行主查询之前使用pre_get_post 更改主查询。将此添加到您的functions.php

function show_only_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '1' );
    }
}
add_action( 'pre_get_posts', 'show_only_category' );

这也将解决您的分页问题

编辑

为了 SO 用户,我已通过同一 OP 将我的答案从 WPSE 转发到 this question

【讨论】:

  • 非常感谢彼得!这完美地工作并且分页正在工作!我对术语有点困惑。 “自定义查询”是否总是如下所示: ----- $query = new WP_Query('cat=1'); ----- ?我很困惑,因为我在Codex for custom query 中没有看到这个确切的代码 ----- 另外,query_posts() 是否也称为自定义查询?
  • 我也对术语感到困惑,因为article about reseting loops ----- wp_reset_postdata() -> 最好在使用 WP_Query 创建自定义或多个循环后使用 ----- wp_reset_query() - > 最好在 query_posts 循环之后用于重置自定义查询 ----- 他将 query_posts 称为自定义查询
  • 以下使用并视为自定义查询,WP_Queryget_postsquery_posts。关于循环的法典页面写得不好。永远不要使用query_posts。使用WP_Queryget_posts 创建的自定义查询应使用wp_reset_postdata() 重置。 query_posts 应使用 wp_reset_query 重置
  • 谢谢 Pieter,你帮了大忙!
  • 不,您不需要重置主查询,这是在内部完成的。只有自定义查询需要重置。 $wp_query 是主查询使用的全局变量。要使主查询的对象在循环之外可用,您需要调用全局$wp_query,这样在您的函数中是正确的
【解决方案2】:
<?php
    get_header(); ?>

    <div id="container">
        <div id="content" role="main">

        <?php
            $categories = get_categories(); //get all the categories

            foreach ($categories as $category) {
                $category_id= $category->term_id; ?>

                <div><?php echo $category->name ?></div>

                <?php
                    query_posts("category=$category_id&posts_per_page=100");

                    if (have_posts()) {
                        while (have_posts()) {
                            the_post(); ?>

                <a href="<?php the_permalink();?>"><?php the_title(); ?></a>

                            <?php }
                        }
                    }
            } ?>

        </div>
    </div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    相关资源
    最近更新 更多