【问题标题】:Wordpress not filtering/sorting categoriesWordpress 不过滤/排序类别
【发布时间】:2015-02-26 10:40:50
【问题描述】:

我已经完成了一个自定义 wordpress 主题,但现在无法找到最初的人。

所有类别都显示在所有类别页面上,尽管我创建了一个新类别并将 2 个帖子移入其中。 (只是试图创建一个档案)

请各位好心人指出我需要在 category.php 页面中寻找的方向吗? (见下文)

<?php if ( is_day() ) : ?>
            <h1 class="page-title"><?php printf( __( 'Daily Archives: <span>%s</span>', 'your-theme' ), get_the_time(get_option('date_format')) ) ?></h1>

<?php elseif ( is_month() ) : ?>
            <h1 class="page-title"><?php printf( __( 'Monthly Archives:<span>

<?php $archive_query = new WP_Query('showposts=1000');
            while ($archive_query->have_posts()) : $archive_query->the_post(); ?>

                  <div class="row">
    <div class="span3">


            <?php the_post_thumbnail('full');?>
             </div>

                    <div class="span5">

            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

                <div class="entry-summary">  
<?php the_content( __( 'Continue reading <span class="meta-nav">
                </div><!-- .entry-summary -->

            </div><!-- #post-<?php the_ID(); ?> -->
             </div>
                              </div>

                        <?php endwhile; ?>

我不得不删除一些代码,因为它不会让我过去,这并没有让我充满信心。

我不是 Wordpress 爱好者,但知道基础知识(我认为)

提前致谢

【问题讨论】:

  • 这是您的代码所期望的。切勿在存档页面上使用自定义查询。改用pre_get_posts 更改主查询。
  • 我认为这将是开始的部分
  • 正确:-)。请记住也要从循环中删除 $archive_query-&gt; 部分
  • 那会是正确的吗? have_posts()) : $pre_get_posts->the_post(); ?>
  • 它仍然显示所有帖子?抱歉,我似乎知道的比我想的要少得多

标签: php wordpress categories


【解决方案1】:

你不应该用自定义查询替换存档页面或主页上的主查询,你总是会遇到一些你现在遇到的问题:-)

要解决您的问题,请删除此部分

<?php
$archive_query = new WP_Query('showposts=1000'); 

while ($archive_query->have_posts()) : 
    $archive_query->the_post(); ?> 

然后替换为

<?php
while(have_posts()) :
    the_post();

您现在应该会看到显示的正确帖子

如果您需要更改主查询中的某些内容,请使用pre_get_posts。让我们以您的示例代码为例。您需要在所有存档页面上将 posts_per_page 更改为 1000,然后您可以在您的 functions.php 中执行以下操作

add_action('pre_get_posts', function ($q) {

    if (!is_admin() && $q->is_main_query() && $q->is_archive()) {
        $q->set('posts_per_page', 1000);
    }

});

只要确定,上面的代码需要php 5.3+

【讨论】:

  • 非常感谢。我稍后会试试这个,让你知道我是怎么做的。非常感谢;-)
  • 没问题,慢慢来。 :-)
  • 彼得,你是个学者,也是个绅士。工作完美。非常感谢。
  • 这个问题仍然悬而未决的任何原因:-)
猜你喜欢
  • 2015-02-25
  • 1970-01-01
  • 2019-08-11
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多