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();
  'post_parent' => get_the_ID(),
  'post_type' => 'topic-reply',
  'posts_per_page' => 10,
  'order' => 'ASC',
  'paged' => $paged,
  while ($wp_query->have_posts()):

$wp_query = $temp;

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


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


instead of


(A live example here)

Does anyone know the rules I should add to set paged permalinks for the "topic-reply" post type?

1 Answer 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).


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;
  } ?>

I'm pretty sure that'd get you the same styles and the 'current' style for the links.

  • nice, it works :) the only thing is that I need to make my own paging function, because wp_page_navi checks for page. thanks Feb 16, 2011 at 21:17
  • have you had a look at paginate_links()? I wrote about it on my blog here: jbl.me/2h Feb 16, 2011 at 21:22
  • I tried the example here: codex.wordpress.org/Function_Reference/paginate_links (changed page/paged with tpage), and I get links like .../tpage/2/tpage/2, and even if I change the page manually the current page is not seen as active... Feb 16, 2011 at 21:51
  • replacing get_pagenum_link(1) with get_permalink() fixes the links, but I still get the 1st page as active, regardless of what page I'm actually on Feb 16, 2011 at 22:00
  • fixed by passing the current parameter from my loop function. I guess it has something to do with the global variable mess... Feb 16, 2011 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.