1

My site is enerating some code for older/newer posts on the category page, but when you click "older" the link does not work. it is generating /blog/page/2 from /blog/

  1. Tried a few plugins (WPnavi, Category pagination fix, and a few others that were the first and 2nd rated under "pagination" search) but it didn't work

  2. Tried a bunch of the codes I found on wordpress.org

  3. Only one post category, not a custom post.

But nothing seems to be working...

Here is the code

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

 <A href="<?php the_permalink() ?>" class="noborder"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('thumbnail'); } ?></a></div>

<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?> "rel="bookmark"><?php the_title(); ?></a>
<section class="entry-content">
<?php $content = get_the_content();
      $content = strip_tags($content);
      echo substr($content, 0, 250);
?>... <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" class="button">Full Post</a>

<?php endwhile; endif; ?>

</article>

<nav>
<div class="6u"><?php next_posts_link(sprintf( __( '%s', 'blankslate' ), '<span class="button">&larr;  Older Posts</span>' ) ) ?>
<div class="6u"><?php previous_posts_link(sprintf( __( '%s', 'blankslate' ), '<span class="button">&rarr; </span>' ) ) ?></div>
</nav>
  • Do not use a custom query. Just a tip, get_posts is not meant to paginated. I'm not saying it cannot be paginated, it can, but it requires a lot of unnecessary overheads. – Pieter Goosen Dec 20 '15 at 5:03
  • Thanks for your response. I'm kind of new to the codex, so I'm not 100% I'm following what your saying. What would be the better way to get a category page that allows users to see 10 posts before a new page would need to be clicked to? – Cdunn Dec 20 '15 at 5:18
  • Take a look at this post – Pieter Goosen Dec 20 '15 at 5:20
  • Thanks, I updated the code back to the original version, this code originally on here was from Wordpress codex for getting pagination on the Category page - so does that just not work? – Cdunn Dec 20 '15 at 5:26
  • Some info in the codex are nothing more than crap, like the one you are refering to. The issue with the codex is, everyone with an account on wordpress.org can change anything in the codex, so total novices can add info in the codex, correct or not, and unfortunately someone like you that does not know better (NO OFFENSE MEANT) accepts that as correct as it is, for that matter, the codex. I think the page you are refering to is also recommending query_posts, which is a pure evil function as it break everything. I personally think there are much better info here than in the codex........ – Pieter Goosen Dec 20 '15 at 6:48
2

Try this code, not much different from your's but with proper nesting. Let me know if that works.

<?php if ( have_posts() ) :
    while ( have_posts() ) : the_post(); ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

        <a href="<?php the_permalink() ?>" class="noborder"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('thumbnail'); } ?></a>
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a>

            <section class="entry-content">
                <?php   $content = get_the_content();
                        $content = strip_tags($content);
                        echo substr($content, 0, 250) . '...';
                ?>
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" class="button">Full Post</a>
            </section>

    </article>

    <?php endwhile; ?>
        <nav>
            <div class="6u"><?php next_posts_link(sprintf( __( '%s', 'blankslate' ), '<span class="button">&larr;  Older Posts</span>' ) ) ?>
            <div class="6u"><?php previous_posts_link(sprintf( __( '%s', 'blankslate' ), '<span class="button">&rarr; </span>' ) ) ?></div>
        </nav>          

<?php else : ?>

    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>

<?php endif; ?>
  • Everything loads correctly but the next/previos link just goes to a 404 page. – Cdunn Dec 20 '15 at 5:59
0

Found this to fix my problem, hope it helps someone:

  add_action('init','yoursite_init');
function yoursite_init() {
  global $wp_rewrite;
        //add rewrite rule.
                add_rewrite_rule("author/([^/]+)/page/?([0-9]{1,})/?$",'index.php?author_name=$matches[1]&paged=$matches[2]','top');
                add_rewrite_rule("(.+?)/page/?([0-9]{1,})/?$",'index.php?category_name=$matches[1]&paged=$matches[2]','top');
                $wp_rewrite->flush_rules(false);
}
  • 2
    Flushing the rewrite rules on every request makes your very slow. Don't do that! – fuxia Dec 20 '15 at 13:32
  • @toscho is right, it just won't make your site very slow to load, but also increase server load by a mile. Use the action after_switch_theme to hook your flush rules callback. If you ever want to test it, just deactivate and reactivate the theme. – Abhik Dec 23 '15 at 5:02

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.