1

I created a Wordpress blog over a year ago basing a child theme on the twenty twelve theme, I have made a few minor modifications. One of these modifications was to add custom portfolio content, so I can have a portfolio category page that links to individual posts within that category.

Issue: Something that has recently become evident as my post numbers have grown, is when I click through the main menu/categories page it displays the first 10 post fine, but if there are more than 10 post, the 'Older post' link at the bottom of the page offers a click through, but the same 10 posts (newest posts) are displayed again, despite there being 13 posts, so it should roll over and show the oldest 3. This issue can be seen by going to this page link below, other catergories don't have more than 10 posts mainly. coder-tronics.com/categories/c-programming

Now if I use the side bar Categories link choosing the C-Programming category, the Older and newer page links work fine displaying all 13 posts correctly, this link demonstrates the correct operation.

http://coder-tronics.com/category/c-programming

I have been trying to work out what I have done wrong, it was over a year ago I followed a tutorial to do this so not as familiar with Wordpress as I was. I have a custom-content.php which is based on the content.php, and also page templates for each portfolio category i.e. C programming etc. I have checked the custom-content.php and it appears ok as made some minor amendments and tried to remove this from being a problem, by having the portfolio page template for C programming use the content.php, this had no effect on the issue.

Not posted any code but happy to do so, just unsure where the issue is at this time and didn't want to post irrelevant code.

Thanks,

Ant

Edited added code reference Pieter comment below

    <?php
/*
Template Name: C Programming Category page
*/

get_header(); ?>

    <section id="primary" class="site-content">
        <div id="content" role="main">

        <?php query_posts('category_name = C Programming'); ?>
        <?php if ( have_posts() ) : ?>
            <header class="archive-header">


            <?php if ( category_description() ) : // Show an optional category description ?>
                <div class="archive-meta"><?php echo category_description(); ?></div>
            <?php endif; ?>
            </header><!-- .archive-header -->
            <?php
            /* Start the Loop */
            while ( have_posts() ) : the_post();

                /* Include the post format-specific template for the content. If you want to
                 * this in a child theme then include a file called called content-___.php
                 * (where ___ is the post format) and that will be used instead.
                 */
                get_template_part( 'content-custom', get_post_format() );

            endwhile;

            twentytwelve_content_nav( 'nav-below' );
            ?>

        <?php else : ?>
            <?php get_template_part( 'content', 'none' ); ?>
        <?php endif; ?>

        </div><!-- #content -->
    </section><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
  • Are you using any custom queries, like any instance of WP_Query, get_posts or, if heavan forbids, query_posts. It is almosts always custom queries that have issues with pagination – Pieter Goosen Aug 19 '14 at 9:48
  • Hi Pieter, thank you for the comment and yes I am using query_posts(), have posted the code above and the line comes just before have_posts() function. – Ant Aug 19 '14 at 11:41
  • query_posts is your problem here. If you don't find an answer, I will post one later today. Currently posting from my phone – Pieter Goosen Aug 19 '14 at 11:58
  • Pieter thanks, I have been playing around with the code for a few days, but no success so far and no immediate rush as it's more an inconvenience. But if you can help later that will be great. Cheers Ant – Ant Aug 19 '14 at 12:00
2

You should not use query_posts at all. Everything is explained in this post I have recently done. Check out all the links as well that I've given in that post

The big problem why your pagination doesn't work is that the default twenty twelve pagination function, twentytwelve_content_nav, doesn't make provision for custom queries. You can, however, override this function, as it is wrapped in a if ( ! function_exists() ) conditional statement.

First, for your custom query. As said, don't and never use query_posts, is most cases it fails outright on pagination. Here you going to use WP_Query. So change your page template to this (Just one point here, category_name uses the the category slug, not name)

<?php
/*
Template Name: C Programming Category page
*/

get_header(); ?>

    <div id="primary" class="site-content">
        <div id="content" role="main">

        <?php
        $args = array(
            'category_name' => 'c-programming'
        );

        $my_query = new WP_Query($args); ?>
        <?php if ( $my_query->have_posts() ) : ?>

            <?php /* Start the Loop */ ?>
            <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
                <?php get_template_part( 'content', 'custom' ); ?>
            <?php endwhile; ?>

            <?php twentytwelve_content_nav( 'nav-below' ); ?>

        <?php else : ?>

            <article id="post-0" class="post no-results not-found">

            <?php if ( current_user_can( 'edit_posts' ) ) :
                // Show a different message to a logged-in user who can add posts.
            ?>
                <header class="entry-header">
                    <h1 class="entry-title"><?php _e( 'No posts to display', 'twentytwelve' ); ?></h1>
                </header>

                <div class="entry-content">
                    <p><?php printf( __( 'Ready to publish your first post? <a href="%s">Get started here</a>.', 'twentytwelve' ), admin_url( 'post-new.php' ) ); ?></p>
                </div><!-- .entry-content -->

            <?php else :
                // Show the default message to everyone else.
            ?>
                <header class="entry-header">
                    <h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
                </header>

                <div class="entry-content">
                    <p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
                    <?php get_search_form(); ?>
                </div><!-- .entry-content -->
            <?php endif; // end current_user_can() check ?>

            </article><!-- #post-0 -->

        <?php endif; // end have_posts() check ?>
    <?php wp_reset_postdata(); ?>
        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar( 'front' ); ?>
<?php get_footer(); ?>

Now, copy twentytwelve_content_nav function to your child theme functions.php. You are going to modify this. Here is the modified code. Work through it and check the changes, I've commented those

function twentytwelve_content_nav( $html_id ) {
    global $wp_query, $my_query; // add your custom query variable

    $html_id = esc_attr( $html_id );

    if ( $wp_query->max_num_pages > 1 ||  $my_query->max_num_pages > 1 ) : ?> // Add your custom query to the max_num_parameter
        <nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
            <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
            <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentytwelve' ) ); ?></div>
            <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?></div>
        </nav><!-- #<?php echo $html_id; ?> .navigation -->
    <?php endif;
}

This should do it

|improve this answer|||||
  • Hi Pieter, I am reading your other post at the moment and will follow the links shortly. Adding the function 'twentytwelve_content_nav( $html_id )' no problem and created a new template with the code provided. The slug name is noted and is actually c-programming so have changed that. I am a little how to add my custom query variable to the new 'twentytwelve_content_nav( $html_id )' as per line 2 and 6 (as per comments), or is it as written? – Ant Aug 19 '14 at 15:16
  • I have already done that :-). Just copy and paste as is – Pieter Goosen Aug 19 '14 at 15:19
  • Hi Pieter, thanks didn't realise you had completed it all and can see now this line sets what $my_query is ' <?php $my_query = new WP_Query('category_name = C Programming'); ?>' . Ok so copied and pasted the code but I am still running into an issue, I have tried different slug names to see if that was the issue and the original C Programming, but no effect. From the categories page and C Programming image, nothing appears now, and from the C Programming main menu drop down I get the categories but no pagination. I know it's something I must be missing and really appreciate your time so far – Ant Aug 19 '14 at 15:45
  • From the categoris page and then clicking on the image, it looks likes its looking for an article. As the url in the ddress bar reads coder-tronics.com/articles/c-programming – Ant Aug 19 '14 at 15:47
  • Is this a category.php template or a page.php template – Pieter Goosen Aug 19 '14 at 15:52

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.