0

Please start by taking a look at this quick video: http://www.screenr.com/8vSH

The problem: get_next_posts_link() disappears on page 4. That is, the "go to older posts" link disappears once I am in &paged=4 of my blog. However, I can manually type eg. &paged=5 and I do get the fifth page's posts (which means, the posts are there, but the get_next_posts_link() is just not showing up).

So..

  • Printing my total posts-number returns the correct amount (33), which tells me that all my posts are being queried properly.
  • The posts and blog-pages are working properly (i.e. I can type &paged=5 or 6 or so on), and I do get that specific blog-page with the corresponding posts.
  • I have also tried next_posts_link() to no avail.

The current code looks like this:

echo '<ul class="blog-navi">
                <li class="blog-navi-prev">'.get_next_posts_link($pagi_older_text, 0).'</li>
                <li class="blog-navi-next">'.get_previous_posts_link($pagi_newer_text).'</li>
            </ul>';

Thank you!

The posts query

get_header();

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$blogcats = $era_opts['era_opts_blog_thecats'];
if(!isset($era_opts['era_opts_blog_thecats'])) {
    $blogcats = '';
} else {
    $blogcats = implode(',', $blogcats);
}

$args = array(
    'post_type' => 'post',
    'numberposts' => '-1',
    'paged' => $paged,
    'category' => $blogcats
);
$era_blog_posts = get_posts($args);

The added pre_get_posts function in functions.php

function era_blog_cats_wpse_103587($qry) {
  if ($qry->is_page(122) && is_main_query()) {
    $era_opts = get_option('era_theme_panel');
    $blogcats = $era_opts['era_opts_blog_thecats'];

    if(!empty($era_opts['era_opts_blog_thecats'])) {
      $qry->set('category__in', $blogcats);
    }
  }
}
add_action('pre_get_posts','era_blog_cats_wpse_103587');
  • 1
    are you using a query to alter the 'posts_per_page' in that template? – Michael Jun 20 '13 at 10:08
  • @Michael Whoops, forgot about that. Updated my question! – cr0z3r Jun 20 '13 at 10:19
  • 3
    Modifying query in template is highly unreliable with native pagination functionality. For it to work reliably any changes should be done to main query, for example via pre_get_posts hook, before template is reached. – Rarst Jun 20 '13 at 10:25
  • @Rarst I see, thank you for that. I guess I'll have a read on wordpress.stackexchange.com/questions/50761/… Thank you! – cr0z3r Jun 20 '13 at 10:39
0

I don't know where $era_opts comes from but a filter on pre_get_posts should do this and preserve your pagination.

function era_blog_cats_wpse_103587($qry) {
  if ($qry->is_page('your-page') && is_main_query()) {
    $era_opts = get_option('era_opts'); // assuming that era_opts are options
    $blogcats = $era_opts['era_opts_blog_thecats'];
    if(!empty($era_opts['era_opts_blog_thecats'])) {
      $qry->set('category__in', $blogcats);
    }
  }
}
add_action('pre_get_posts','era_blog_cats_wpse_103587');

I made several assumptions about your data, but that should be the idea. I also don't know what conditions you need this to fire on. See the first line in the function. You need to alter that line to control when this filter runs and when it doesn't.

That function will effect the main query on the page, which will hopefully work for you. If you want to paginate a secondary Loop, the normal pagination functions do not work well. I would suggest you use paginate_links instead of trying to get those functions to work but you can see this question for other options. There are other similar Q/As here too.

  • Thank you for your reply. Indeed, $era_opts gets the options from the theme's options-panel. This is how my code currently looks like: pastie.org/8064040 (which I put in my functions.php file). This hasn't changed the functionality nor fixed the problem though. Did I miss something? – cr0z3r Jun 20 '13 at 19:10
  • That should work if all my assumptions are correct. I'd guess that the conditionals-- $qry->is_page('your-page') && is_main_query()-- are wrong if I had to guess. – s_ha_dum Jun 20 '13 at 19:15
  • Just edited the main question. I needn't change my initial query, right? Like I said, I simply added your function to my functions.php file. And it should work, as the page's ID is 122. I am unsure about is_main_query() though. – cr0z3r Jun 20 '13 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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