WP_QUERY与分页重定向到主页
我有一个自定义帖子类型设置的自定义循环,还添加了分页。但是,当 /page /2将其加载到主页时。是什么原因造成的?
这是我的代码:
function viares_graduates () {
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
'paged' => $paged,
'post_status' => 'publish',
'post_type' => 'graduates',
'posts_per_page' => 9,
'tag__not_in' => array( '76' ),
'orderby' => 'rand',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
//here my loop is displayed
endwhile;?>
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $loop->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div><?php
wp_reset_postdata();
}
add_shortcode('viares_graduates', 'viares_graduates');
分页在前端正确显示,循环正常工作。您可以在此处查看: https://dev.viares.at/graduates/ 但是,当我单击下一页时( https://dev.viares.at/graduates/页/2/)它重定向到 https://dev.viares.at/
帖子类型:
function custom_post_type() {
$labels = array(
'name' => _x( 'Graduates', 'Post Type General Name', 'twentytwenty' ),
'singular_name' => _x( 'Graduate', 'Post Type Singular Name', 'twentytwenty' ),
'menu_name' => __( 'Graduates', 'twentytwenty' ),
'parent_item_colon' => __( 'Parent Movie', 'twentytwenty' ),
'all_items' => __( 'All Graduates', 'twentytwenty' ),
'view_item' => __( 'View Graduate', 'twentytwenty' ),
'add_new_item' => __( 'Add New Graduate', 'twentytwenty' ),
'add_new' => __( 'Add New', 'twentytwenty' ),
'edit_item' => __( 'Edit Graduate', 'twentytwenty' ),
'update_item' => __( 'Update Graduate', 'twentytwenty' ),
'search_items' => __( 'Search Graduates', 'twentytwenty' ),
'not_found' => __( 'Not Found', 'twentytwenty' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwenty' ),
);
$args = array(
'label' => __( 'Graduates', 'twentytwenty' ),
'description' => __( 'Reviews from Graduates', 'twentytwenty' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 4,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'capability_type' => 'post',
'show_in_rest' => true,
'taxonomies' => array('post_tag')
);
register_post_type( 'graduates', $args );
}
add_action( 'init', 'custom_post_type', 0 );
希望任何人都可以提供帮助!
编辑/更新: 我在页面 /毕业生上添加了短代码,这造成了错误。因此,我将URL SLUG更改为 /研究生或其他与自定义帖子类型名称不同的东西,现在可以使用!
i have a custom loop for a custom post type set up and also added a pagination. But when the /page/2 is loading it redirects to the homepage. What is causing this?
Here is my code:
function viares_graduates () {
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
'paged' => $paged,
'post_status' => 'publish',
'post_type' => 'graduates',
'posts_per_page' => 9,
'tag__not_in' => array( '76' ),
'orderby' => 'rand',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
//here my loop is displayed
endwhile;?>
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $loop->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div><?php
wp_reset_postdata();
}
add_shortcode('viares_graduates', 'viares_graduates');
The pagination is displayed correctly on the frontend and the loop is working fine. You can check it out here: https://dev.viares.at/graduates/
However when I click on the next page (https://dev.viares.at/graduates/page/2/) it redirects to https://dev.viares.at/
Custom Post Type:
function custom_post_type() {
$labels = array(
'name' => _x( 'Graduates', 'Post Type General Name', 'twentytwenty' ),
'singular_name' => _x( 'Graduate', 'Post Type Singular Name', 'twentytwenty' ),
'menu_name' => __( 'Graduates', 'twentytwenty' ),
'parent_item_colon' => __( 'Parent Movie', 'twentytwenty' ),
'all_items' => __( 'All Graduates', 'twentytwenty' ),
'view_item' => __( 'View Graduate', 'twentytwenty' ),
'add_new_item' => __( 'Add New Graduate', 'twentytwenty' ),
'add_new' => __( 'Add New', 'twentytwenty' ),
'edit_item' => __( 'Edit Graduate', 'twentytwenty' ),
'update_item' => __( 'Update Graduate', 'twentytwenty' ),
'search_items' => __( 'Search Graduates', 'twentytwenty' ),
'not_found' => __( 'Not Found', 'twentytwenty' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwenty' ),
);
$args = array(
'label' => __( 'Graduates', 'twentytwenty' ),
'description' => __( 'Reviews from Graduates', 'twentytwenty' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 4,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'capability_type' => 'post',
'show_in_rest' => true,
'taxonomies' => array('post_tag')
);
register_post_type( 'graduates', $args );
}
add_action( 'init', 'custom_post_type', 0 );
Hope anyone can help!
EDIT/UPDATE:
I added the shortcode on the page /graduates, which made the error. So I changed the URL slug to /graduate or something else which is not identical to the custom post type name and it works now!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论