分页给出了404 in模板

时间:2016-04-01 作者:Anders Stensaas

我在“页面”中有一个页面叫做“新闻”。此页面有一个包含我的页面自定义的模板WP_Query.

代码如下:

<?php

    $paged = 1;
    if (get_query_var(\'paged\')) {
      $paged = get_query_var(\'paged\');
    } elseif (get_query_var(\'page\')) {
      $paged = get_query_var(\'page\');
    }

    $arguments = array (
        \'posts_per_page\' => 3,
        \'post_type\' => \'fltnews\',
        \'paged\' => $paged,
        \'meta_key\' => \'publish_date\',
        \'orderby\' => \'meta_value\',
        \'order\' => \'DESC\'
    );

    $custom_wp_query = new WP_Query($arguments);
    $temp_wp_query  = $wp_query;
    $wp_query       = NULL;
    $wp_query       = $custom_wp_query;

    echo "<ul class=\'content-list\'>";
    if ($custom_wp_query->have_posts()) :
        while ($custom_wp_query->have_posts()) : $custom_wp_query->the_post();
            echo "<li class=\'content-list-item\'>";
            $podsForNewsItem = pods(\'fltnews\', $post->ID);
            $podsImage = pods_image ( $podsForNewsItem->display( \'thumbnail_picture\'), \'full\');
            if ($podsImage != ""){
                $podsImage = "<div class=\'content-list-thumbnail-picture\'>" . $podsImage . "</div>";
            }

            printf("<a href=\'%s\' class=\'item-container\'>%s
                            <div class=\'content-list-body\'>
                                <div class=\'content-list-publisher\'>
                                    <div class=\'date-block\'><span class=\'date\'>%s</span></div>
                                </div>
                                <div class=\'content-list-title\'>%s</div>
                                <div class=\'content-list-text\'>%s</div>
                                <div class=\'content-list-link\'>Les mer</div>
                            </div>
                        </a>",
                    esc_url(get_permalink($post->ID)),
                    $podsImage,
                    getDateAsNorwegian($podsForNewsItem->field(\'publish_date\')),
                    $post->post_title,
                    $post->post_excerpt);

            echo "</li>";
        endwhile;

        wp_reset_postdata();

        previous_posts_link(\'Nyere &raquo;\');
        next_posts_link(\'&laquo; Eldre nyheter\', $custom_wp_query->max_num_pages);

        $wp_query = NULL;
        $wp_query = $temp_wp_query;

    endif;
    echo "</ul>";
?>
我已经看了以下问题和答案:

  • How to fix pagination for custom loops?
  • Pagination gives 404 errorWP_Query. 然而,这对我不起作用。

    “新闻”页面位于以下URL:/News。这个next_posts_link 提供以下链接:/news/page/2。

    这就是给我404的那个。我的永久链接设置如下:日期和名称。

    我不明白为什么我要买404。

1 个回复
最合适的回答,由SO网友:Anders Stensaas 整理而成

我做了Rarst让我做的事。首先我在Pods Framework documentation. 这解决了我的问题。

下面是代码现在的样子。

<?php

  $params = array(
    \'orderby\' => \'publish_date.meta_value DESC\',
    \'limit\' => 10
  );

  $fltNewsPods = pods( \'fltnews\', $params );
  while ($fltNewsPods->fetch()) {
    echo "<li class=\'content-list-item\'>";
    $podsForNewsItem = pods(\'fltnews\', $fltNewsPods->display( \'id\' ));
    $podsImage = pods_image ( $podsForNewsItem->display( \'thumbnail_picture\'), \'full\');
    if ($podsImage != ""){
        $podsImage = "<div class=\'content-list-thumbnail-picture\'>" . $podsImage . "</div>";
    }

    printf("<a href=\'%s\' class=\'item-container\'>%s
                          <div class=\'content-list-body\'>
                              <div class=\'content-list-publisher\'>
                                  <div class=\'date-block\'><span class=\'date\'>%s</span></div>
                              </div>
                              <div class=\'content-list-title\'>%s</div>
                              <div class=\'content-list-text\'>%s</div>
                              <div class=\'content-list-link\'>Les mer</div>
                          </div>
                      </a>",
                  esc_url(get_permalink($podsForNewsItem->display( \'id\' ))),
                  $podsImage,
                  getDateAsNorwegian($podsForNewsItem->field(\'publish_date\')),
                  $podsForNewsItem->display( \'title\' ),
                  $podsForNewsItem->display( \'excerpt\' ));
      echo "</li>";
  }

  echo $fltNewsPods->pagination( array(
    \'type\' => \'paginate\',
    \'prev_next\' => false,
    \'first_last\' => false
    ) );

  echo "</ul>";
?>
虽然pods框架的使用率很高,但基本上是一样的。

我想这不适用于WP\\u查询版本,因为这不是一个自定义的帖子类型,所以它没有使用Wordpress框架。Wordpress不知道这些应该如何分页。

相关推荐

change pagination url

目前,我的分页页面URL为:http://www.example.com/category_name/page/2但我需要将此URL结构更改为:http://www.example.com/category_name/?page=2等有什么解决方案吗?