加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 自定义分类和自定义分页404,找不到自定义分页404

发布时间:2020-12-13 13:54:02 所属栏目:PHP教程 来源:网络整理
导读:我的分页是在General – 的帖子中抛出404错误阅读量小于我自定义分类城市(自定义帖子类型城市)上的自定义帖子数量.从我在SO上看到的这个问题通常可以通过使用pre_get_posts的过滤器来修复,但不知道如何将它应用于我的案例. 我想提一下,在taxonomy-cities.php
我的分页是在General – >的帖子中抛出404错误阅读量小于我自定义分类城市(自定义帖子类型城市)上的自定义帖子数量.从我在SO上看到的这个问题通常可以通过使用pre_get_posts的过滤器来修复,但不知道如何将它应用于我的案例.
我想提一下,在taxonomy-cities.php中,我收到了一个自定义帖子类型的自定义分类的帖子.

分类学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(); ?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读