找不到具有自定义分页404的自定义分类和自定义帖子类型 [英] Custom taxonomy and custom post type with custom pagination 404 not found

查看:90
本文介绍了找不到具有自定义分页404的自定义分类和自定义帖子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当常规"->阅读"中的帖子小于我的自定义分类法cities(自定义帖子类型city)上的自定义帖子数时,我的分页抛出404错误.根据我的观察,通常可以使用pre_get_posts过滤器解决此问题,但不知道如何将其应用于我的案例. 我想提及的是,在taxonomy-cities.php中,我收到的是属于自定义帖子类型的自定义分类法类别的帖子.

My pagination is throwing a 404 error when the posts from General -> Reading are smaller than my custom number of posts on my custom taxonomy cities (custom post type city). From what I saw on SO this issue can be fixed usually by using a filter of pre_get_posts, but don't know how to apply it on my case. I want to mention that in the taxonomy-cities.php I am getting posts of a category of a custom taxonomy of a custom post type.

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:

For archive-city.php I am using this filter in the functions.php and it works fine, but it doesn't get applied to the taxonomy-cities.php so I get 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 (例如此处.

This is an old question however i recently had the same issue. The following Snippet is in fact the contents of my taxonomy-template.php using the standard WP_Query, example here.

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

查询

  • 首先,因为它是一个分类模板,所以第一行声明$term,使我可以访问术语数组,因此可以访问分类ID,名称url等.

    The Query

    • Firstly because it's a taxonomy template, the first line declares $term allowing me to access the term array and therefore the taxonomy id, name url etc.

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

      Secondly i'm setting the term id as a variable to be used in the tax_query parameter. This is used to query my custom post type videos and then get the taxonomy (category) for the loop, stored in $term_id.

      • 因为我要遍历帖子并限制结果,所以我们实际上需要搜索以查找剩余的帖子,同时排除之前的结果和之后的结果.这意味着非常简单:
      • Because i'm looping through the posts and limiting the results, we effectively need to search for our remaining posts while excluding the previous results and results thereafter. What this means is quite simple:

      必须将自定义帖子类型设置为可搜索,以便分页生效:

      You must make your custom post types searchable in order for pagination to work:

      重要:

      'exclude_from_search' => false,

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

      这篇关于找不到具有自定义分页404的自定义分类和自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆