Skip Navigation

[Resolved] Custom Post Type Loop On Template Page, Rewrite URL Breaks Pagination

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Minesh 7 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#467279
cpt-paging-rewrite.png

I am trying to use pagination in a page template (NOT archive-[post-type]) I have the pagination working until I use 'Rewrite' and 'Use a custom URL format ' option.

My CPT slug is 'news-story'
My 'custom URL format' under options Rewrite is '/parent-page/news'

This gives me exactly what I want for the single post pages:
mydomain.com/parent-page/news/title-of-news-story
However when I try to go to the next page I receive a 404.

Here is the code for my loop with paging on the template page, thank you for you help:

$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'news-story',
'nopaging' => false,
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'order' => 'DESC',
'orderby' => 'date',
'paged' => $paged
);

$news_query = new WP_Query( $args );

if ( $news_query->have_posts() ) {
while ( $news_query->have_posts() ) {
$news_query->the_post(); ?>

<article id="post-<?php the_ID(); ?>" <?php post_class('news-item'); ?>>
<div class="news-image">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('news-thumb');
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' )
. '/img/news.jpg" />';
} ?>
</div>
<h2><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h2>
<div class="news-date"><?php echo types_render_field("news-date", array('format'=>'m/d/Y')); ?></div>
<div class="news-excerpt"><?php the_excerpt(); ?></div>
</article>

<?php } ?>
<?php if ($news_query->max_num_pages > 1) { ?>
<nav class="prev-next-posts text-center pager color1txt">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older News Stories', $news_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'More Recent News' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>

<?php
} else {
_e('no posts.');
}

wp_reset_postdata(); ?>

#467397

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - this is totally custom code as single post pagination should be supported by theme itself and you are using the custom rewrite.

Still can you try this - instead of 'paged' used 'page. replace following line with your original line:

'page' => $paged

If above does not help - I'm extremely sorry, I can't do much here, as a supporter I need to work within the limits. If you need more assistance with your custom programming issue, please don't hesitate to contact our certified partners.
=> https://toolset.com/consultant/

#467418

Thanks for the quick reply. No that does not work, I have switched between page & paged in previous attempts. I understand the limits you must work within.

By 'should be supported by the theme' I assume you mean using archive-* and single-*. So if the solution to get what I need is to use archive-[post-type] and single-[post-type], could you elaborate on that and tell the best way to get those to output the desired url?

Thanks for the help.

#467425

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To paginate from one single Post to the next with a single CPT page. This needs to be provided by the Theme. If your theme does not provide this, you could use Custom Code to create Pagination links.

You should refer to the following Docs:
=> https://codex.wordpress.org/Pagination
=> https://codex.wordpress.org/Next_and_Previous_Links

For Example:

<?php
  previous_post_link( '<li class="previous">%link</li>', _x( '<span class="meta-nav">←</span> %title', 'Previous post link', 'your-theme' ) );
     
  next_post_link(     '<li class="next">%link</li>',     _x( '%title <span class="meta-nav">→</span>', 'Next post link',     'your-theme' ) );
?>
#467434

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.