Pretty paged permalinks in custom post type loop

I must be annoying your with all these permalink questions šŸ™‚

The code I’m using for the loop is:

// hijack stupid WP globals to get pagination working...
global $wp_query;
$temp = $wp_query;

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$wp_query = new WP_Query();
$wp_query->query(array(
  'post_parent' => get_the_ID(),
  'post_type' => 'topic-reply',
  'posts_per_page' => 10,
  'order' => 'ASC',
  'paged' => $paged,
));
if($wp_query):
  wp_pagenavi();
  while ($wp_query->have_posts()):
    $wp_query->the_post();
    get_template_part('topic-reply');
  endwhile;
  wp_pagenavi();
endif;

$wp_query = $temp;
wp_reset_query();

The pagination works fine if permalinks are set to defaults, when clicking on page 2 link the URL I get is like:

http://localhost/wp/?topic=sometopictitle&paged=2

The problem comes when I set the permalinks to a custom structure; when clicking on page 2, I get the first page URL:

http://localhost/wp3/forum/general-discussion/topic/sometopictitle/

instead of

http://localhost/wp3/forum/general-discussion/topic/sometopictitle/page/2/

(A live example here)

Does anyone know the rules I should add to set paged permalinks for the ā€œtopic-replyā€ post type?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

When you use paged on a single post, it’s checking for a paginated post, not pages of posts. Because your topics don’t have paginated content, it’s assuming it’s a mistake and redirecting to the ā€˜first’ page of the topic, long before your custom loop is ever touched. So, in this instance, paged is always going to return as 1. As a workaround, I’d register an EP Permalink for the topic. Something like this:

add_rewrite_endpoint( 'tpage', EP_PERMALINK );

Then, instead of checking for get_query_var( 'paged' );, check for get_query_var( 'tpage' ); and pass the value for that along to the custom query. Setting up that rewrite endpoint means that all permalinks compatible with the EP_PERMALINK bitmask (your topics are compatible) will accept a /tpage/XXXX structure added to the end of the url, where XXXXX can be anything you want (in this instance, you’d want to typecast it as an integer, probably an absolute one at that).

EDIT

Something like this should work to get you an array of paginated links:

$links = paginate_links(array(
  'base' => trailingslashit( get_permalink( $temp->post->ID ) ) . '%_%',
  'format' => 'tpage/%#%',
  'type' => 'array'
));

From there, you could do something like this:

<div class="page-navi  clear-block">
  <?php foreach( $links as $link ){
    if( false !== strpos( $link, " class='page-numbers'" ) )
      $link = str_replace( " class='page-numbers'", " class='page page-numbers'", $link );
    echo $link;
  } ?>
</div>

I’m pretty sure that’d get you the same styles and the ā€˜current’ style for the links.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x