找不到具有自定义分页404的自定义分类法和自定义文章类型


Custom taxonomy and custom post type with custom pagination 404 not found

当来自General->Reading的帖子少于我在自定义分类cities(自定义帖子类型city)上的自定义帖子数时,我的分页会抛出404错误。从我在SO上看到的情况来看,这个问题通常可以通过使用pre_get_posts过滤器来解决,但不知道如何将其应用于我的案例。我想提到的是,在taxonomy-cities.php中,我得到了自定义帖子类型的自定义分类的一个类别的帖子。

taxonomy-cities.php

$cat_ID = get_query_var('cities');
$custom_id = get_term_by('slug', $cat_ID, 'cities');
add_filter('posts_orderby', 'edit_posts_orderby');
function edit_posts_orderby($orderby_statement) {
    global $aPostsIDs;
    $orderby_statement = 'FIELD(ID, '.implode(',',$_SESSION['saved_city_ids']).')';
    return $orderby_statement;
}
$offset     = ($paged - 1) * $num_city_guides_post; 
$args['post_type'] = 'city'; 
$args['posts_per_page'] = $num_city_guides_post; // 5
$args['orderby'] = $orderby; // rand
$args['order'] = $order; // DESC
$args['paged'] = $paged; // 1
$args['tax_query'] = array(
                        array(
                               'taxonomy' => 'cities',
                               'field' => 'id',
                               'terms' =>  array($custom_id->term_id) // 3742
                             )
                    );
}
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) : 
  while ( $wp_query->have_posts() ) : $wp_query->the_post();
  // some code here
  endwhile; 
else: echo 'No Posts';
endif; 
wp_reset_postdata();

对于archive-city.php,我在functions.php中使用了这个过滤器,它工作得很好,但它没有应用于taxonomy-cities.php,所以我得到了404:

function portfolio_posts_per_page_city( $query ) {
        if (array_key_exists('post_type', $query->query_vars)) {
        if ( $query->query_vars['post_type'] == 'city' ) 
        $num_city_guides_post = get_option('num_city_post');
        if( empty( $num_city_guides_post ) )
        $num_city_guides_post = 9;
        $query->query_vars['posts_per_page'] = $num_city_guides_post;
        return $query;
    }
}
if ( !is_admin() ) add_filter( 'pre_get_posts', 'portfolio_posts_per_page_city' );

这是一个老问题,但我最近遇到了同样的问题。下面的代码段实际上是使用标准WP_Query的taxonomy-template.php的内容,例如这里的示例。

如何正确地对自定义帖子类型分类法进行分页

查询

  • 首先因为它是一个分类模板,所以第一行声明$term允许我访问术语数组,从而访问分类id、名称url等。

  • 其次我将术语id设置为tax_query参数中要使用的变量。这用于查询我的自定义帖子类型videos,然后获得循环的分类法(类别),存储在$term_id中。

The Fix

  • 因为我在循环浏览帖子并限制结果,我们实际上需要搜索剩余的帖子,同时排除之前的结果和之后的结果。这意味着非常简单:

您必须使您的自定义帖子类型可搜索,以便分页工作:

重要

'exclude_from_search' => false,


示例taxonomy-template.php

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
<?php
$term_id = $term->term_id;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
  'post_type'       => 'videos',
  'posts_per_page'  => 4,
  'paged'           => $paged,
  'tax_query'       => array(
    array(
      'taxonomy' => $term->taxonomy,
      'field' => 'term_id',
      'terms' => $term_id
    )
  )
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <!-- Loop -->
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) :  ?>
<?php endif; ?>
<?php else: ?>
  <!-- Nothing Found -->
<?php endif; ?>

也许你可以在没有过滤器的情况下解决这个问题。这是我的自定义帖子类型的分页工作

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array( 'post_type' =>'cities', 'posts_per_page' => 0, 'paged' => $paged );
$query = query_posts( $args );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//Do wordpress Loop
<?php endwhile; else:  include( get_404_template() ); ?>
<?php endif; wp_reset_postdata(); ?>

我在自定义后分类法分页时也遇到了同样的问题。旧的帖子页面带有一个404页面,找不到。这个问题与WP分类法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');

这应该适用于所有自定义的post-type分类,如果你想更具体,那么你可以尝试更新这两个变量,如下所示:

$post_types=get_post_types(数组('name'=>'city','public'=&&gt;true,'_builtin'=<false),'objects');

$taxonomies=get_taxomies(数组('name'=>'cities','public'=&&gt;true,'_builtin'=<false),'objects');

最后,只需使用默认的查询循环,而不传递任何参数。页面将基于常规生成->阅读