wordpress 3.8.1 category page 2 error 404 not foun

2019-02-19 02:34发布

问题:

first the problem, then the tries.

Problem

The problem is that i get a 404 NOT FOUND error if i visit another page than the first category page. On the category page i have a normal pagination. The first site works. (http://mypage.com/category/properties)

After i click on the "Next page" button I'm on the page http://mypage.com/category/properties/page/2 and got the error 404 NOT FOUND.

But why?

Tries

First I tried this Question Custom Post Type and Taxonomy pagination 404 error, but the exclude_from_search and the queries below doesnt work.

I tried this, too. http://wordpress.org/support/topic/one-again-page-not-found-on-second-and-further-pages But the query_posts try has the same result as the WP_Query try.

The event with the pre query i tried, too. But the problem is the same -.-

Example / 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'))
));

Results

Page 1

Page 2

回答1:

Try change the pre_get_posts filter.

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' );

Found this at http://css-tricks.com/snippets/wordpress/make-archives-php-include-custom-post-types/



回答2:

Try this, it should work.had similar problem

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

I had the same problem with my custom post taxonomies pagination. Older posts page was coming with a 404 page not found. This issue is related to WP taxonomy slug so you have to rewrite rules for your custom post type taxonomies like this below:

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');

This should work for all custom post type taxonomies and just use your default query loop without passing any arguments. Pages will be generated based on General -> Reading.