0

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?

1 Answer 1

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.

6
  • nice, it works :) the only thing is that I need to make my own paging function, because wp_page_navi checks for page. thanks Commented 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 Commented 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... Commented 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 Commented 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... Commented 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.