我的分页是在General – >的帖子中抛出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' );
也许你可以没有过滤器解决这个问题
这是我的自定义帖子类型的分页工作
这是我的自定义帖子类型的分页工作
$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(); ?>