admin管理员组文章数量:1025361
Having some trouble with pagination and custom post types.
I have 3 custom post types active currently: 'webinar', 'resource', and 'faq'.
For each of these custom post types, I'm using the archive-{post-type}.php
file to show a list of the given post types.
For the /webinars
and /faqs
pages, this works as intended.
However for /resources
, I'm looking to display a list of ALL of the following post types with pagination: 'post', 'webinar', 'resource'.
To do this, I have the following custom query:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$custom_query = new WP_Query(
array(
'post_type' => array('resource', 'post', 'webinar'),
'posts_per_page' => 4,
'paged'=> $paged,
'order' => 'desc',
'orderby' => 'date') );
// Pagination fixes
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
?>
<?php if ( $custom_query->have_posts() ) : ?>
<div class='posts-ctn'>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<?php get_template_part( 'template-parts/resource/content', 'excerpt' ); ?>
<?php endwhile; ?>
</div>
<?php
// Reset postdata
wp_reset_postdata();
// if ($custom_query->max_num_pages > 1) {
// the_posts_pagination( array(
// 'prev_text' => '❮' . '<span class="screen-reader-text">' . __( 'Previous page', 'corax' ) . '</span>',
// 'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'corax' ) . '</span>' . '❯',
// 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'corax' ) . ' </span>',
// ) );
// }
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
<?php else: ?>
<?php get_template_part( 'template-parts/resource/library', 'none' ); ?>
<?php endif ?>
This query returns the correct results, however when I click through to /resources/page/2
, I get a 404 "oops, that page can't be found" page.
I have the following in my functions.php
to also allow custom queries to return custom post types:
/**
* This function modifies the main WordPress query to include an array of
* custom post types instead of the default 'post' post type.
*
* @param object $query The original query.
* @return object $query The amended query.
*/
function include_custom_post_types( $query ) {
$custom_post_type = get_query_var( 'post_type' );
if ( is_archive() ) {
if ( empty( $custom_post_type ) ) $query->set( 'post_type' , get_post_types() );
}
if ( is_search() ) {
if ( empty( $custom_post_type ) ) {
$query->set( 'post_type' , array(
'post', 'webinar', 'resource'
)
);
}
}
return $query;
}
add_filter( 'pre_get_posts' , 'include_custom_post_types' );
I feel like I'm missing something really basic here, but I'm at a loss for what it might be. Help much appreciated!
Having some trouble with pagination and custom post types.
I have 3 custom post types active currently: 'webinar', 'resource', and 'faq'.
For each of these custom post types, I'm using the archive-{post-type}.php
file to show a list of the given post types.
For the /webinars
and /faqs
pages, this works as intended.
However for /resources
, I'm looking to display a list of ALL of the following post types with pagination: 'post', 'webinar', 'resource'.
To do this, I have the following custom query:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$custom_query = new WP_Query(
array(
'post_type' => array('resource', 'post', 'webinar'),
'posts_per_page' => 4,
'paged'=> $paged,
'order' => 'desc',
'orderby' => 'date') );
// Pagination fixes
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
?>
<?php if ( $custom_query->have_posts() ) : ?>
<div class='posts-ctn'>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<?php get_template_part( 'template-parts/resource/content', 'excerpt' ); ?>
<?php endwhile; ?>
</div>
<?php
// Reset postdata
wp_reset_postdata();
// if ($custom_query->max_num_pages > 1) {
// the_posts_pagination( array(
// 'prev_text' => '❮' . '<span class="screen-reader-text">' . __( 'Previous page', 'corax' ) . '</span>',
// 'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'corax' ) . '</span>' . '❯',
// 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'corax' ) . ' </span>',
// ) );
// }
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
<?php else: ?>
<?php get_template_part( 'template-parts/resource/library', 'none' ); ?>
<?php endif ?>
This query returns the correct results, however when I click through to /resources/page/2
, I get a 404 "oops, that page can't be found" page.
I have the following in my functions.php
to also allow custom queries to return custom post types:
/**
* This function modifies the main WordPress query to include an array of
* custom post types instead of the default 'post' post type.
*
* @param object $query The original query.
* @return object $query The amended query.
*/
function include_custom_post_types( $query ) {
$custom_post_type = get_query_var( 'post_type' );
if ( is_archive() ) {
if ( empty( $custom_post_type ) ) $query->set( 'post_type' , get_post_types() );
}
if ( is_search() ) {
if ( empty( $custom_post_type ) ) {
$query->set( 'post_type' , array(
'post', 'webinar', 'resource'
)
);
}
}
return $query;
}
add_filter( 'pre_get_posts' , 'include_custom_post_types' );
I feel like I'm missing something really basic here, but I'm at a loss for what it might be. Help much appreciated!
本文标签: Permalink for custom post type pages not working
版权声明:本文标题:Permalink for custom post type pages not working 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745614222a2159188.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论