【问题标题】:wordpress 3.8.1 category page 2 error 404 not found / custom post typewordpress 3.8.1 类别第 2 页错误 404 未找到 / 自定义帖子类型
【发布时间】:2014-03-24 03:05:23
【问题描述】:

首先是问题,然后是尝试。

问题

问题是,如果我访问第一个类别页面以外的另一个页面,我会收到 404 NOT FOUND 错误。 在类别页面上,我有一个正常的分页。第一个站点有效。 (http://mypage.com/category/properties)

点击“下一页”按钮后,我在页面 http://mypage.com/category/properties/page/2 上并收到错误 404 NOT FOUND。

但是为什么呢?

尝试

首先我尝试了这个问题Custom Post Type and Taxonomy pagination 404 error,但exclude_from_search 和下面的查询不起作用。

我也试过这个。 http://wordpress.org/support/topic/one-again-page-not-found-on-second-and-further-pages 但是 query_posts 尝试与 WP_Query 尝试具有相同的结果。

我也尝试过带有预查询的事件。但是问题是一样的-.-

示例/PHP

<?php

/* /srv/www/mypage/wp-content/themes/twentythirteen/category-1.php */

global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array_merge($wp_query->query, array(
    'posts_per_page' => 4,
    'post_type' => 'property',
    'post_status' => 'publish',
    'meta_key' => 'property_typ',
    'meta_value' => 'Rent',
    'category_name' => null
));

$wp_query = new WP_Query($args);

echo '<ul>';
while (have_posts())
{
    the_post();
    echo '<li><a href="' . get_permalink(get_the_id()) . '">'
         . get_the_title() . '</a></li>';
}
echo '</ul>';

echo paginate_links(array(
    'base' => str_replace(99999, '%#%', esc_url(get_pagenum_link(99999))),
    'total' => $wp_query->max_num_pages,
    'format' => '?paged=%#%',
    'current' => max(1, get_query_var('paged'))
));

结果

第 1 页

第 2 页

【问题讨论】:

  • 您是否访问过/wp-admin/option-permalinks.php 来刷新重写规则?只要加载该页面就足够了,如果这确实是问题!
  • 那什么都没做 :( 我保存并刷新了重写规则并再次收到错误 404。
  • 您正在使用辅助查询,其中主要查询定义它是 404 还是 202 页面。替换线 - $wp_query = new WP_Query($args); 尝试 query_posts($args); 。由于您的页面模板是 category-1.php - 只有当前类别 id 为 - 1 时才会加载此模板。类别 properties 是否使用 id - 1 ?
  • 是的propertiesid=1。如果我用query_posts($args) 替换查询,我也会遇到同样的问题。 query_posts 使用变量 $wp_query ... ^^ 我在哪里可以找到类别的“主要”查询?例如我的category.php 没有使用query_posts ...

标签: php pagination wordpress wordpress-theming


【解决方案1】:

我的自定义帖子分类法分页遇到了同样的问题。较旧的帖子页面带有未找到的 404 页面。此问题与 WP taxonomy slug 有关,因此您必须为您的自定义帖子类型分类重写规则,如下所示:

function generate_taxonomy_rewrite_rules( $wp_rewrite ) {

    $rules = array();

    $post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' );
    $taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ), 'objects' );

    foreach ( $post_types as $post_type ) {
        $post_type_name = $post_type->name;
        $post_type_slug = $post_type->rewrite['slug'];

        foreach ( $taxonomies as $taxonomy ) {
            if ( $taxonomy->object_type[0] == $post_type_name ) {
                $terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) );
                foreach ( $terms as $term ) {
                    $rules[$post_type_slug . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
                    $rules[$post_type_slug . '/' . $term->slug . '/page/?([0-9]{1,})/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug . '&paged=' . $wp_rewrite->preg_index( 1 );
                }
            }
        }
    }

    $wp_rewrite->rules = $rules + $wp_rewrite->rules;

}

add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules');

这应该适用于所有自定义帖子类型分类法,并且只需使用您的默认查询循环而不传递任何参数。页面会根据General -> Reading生成。

【讨论】:

    【解决方案2】:

    试试这个,应该可以了。遇到类似问题

     <?php
            // Display pagination
            global $wp_query;
            $pagination_args = array(
                'base' => '%_%',
                'format' => '?' . $query_string . '&paged=%#%',
                'current' => max(1, get_query_var('paged')),
                'total' => $wp_query->max_num_pages,
                'type' => 'array'
            );
    
            $pagination = paginate_links($pagination_args);
    
    $big = 999999999; // need an unlikely integer
    
    
    $links .= paginate_links(array(
      'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
      'format' => '?' . $query_string . '&paged=%#%&keyword='.$keyword,
      'current' =>max(1, get_query_var('paged')),
      'total' => $wp_query->max_num_pages
    ));
    echo $links;
            ?>
    

    【讨论】:

      【解决方案3】:

      尝试更改 pre_get_posts 过滤器。

      function namespace_add_custom_types( $query ) {
        if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
          $query->set( 'post_type', array(
                    'post', 'property'
                  ));
          return $query;
         }
      }
      add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
      

      http://css-tricks.com/snippets/wordpress/make-archives-php-include-custom-post-types/找到这个

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-12
        • 2018-06-22
        • 2015-06-07
        • 1970-01-01
        • 2012-01-01
        • 2020-03-22
        • 1970-01-01
        相关资源
        最近更新 更多