• Ben

    (@benredpath)


    Hi guys, I’ve looked everywhere for a solution to this but still must be doing something wrong!

    I have set up a page template with a custom loop to show my post thumbnails on my front page which seems to work fine, however I cannot get the pagination to work correctly.

    Currently the ‘older entries’ link works but still displays even when there are no older posts, and the ‘Newer Entries’ never shows at all!

    My code is as follows:

    <?php
    /*
    Template Name: Portfolio
    */
    ?>
    
    <?php get_header(); ?>
    
    	<div id="content">
    
    <?php 
    
    $page = get_query_var( 'page' );
    
    query_posts( array('category_name' => 'portfolio',  'posts_per_page' => 2, 'paged' => $page)); 
    
    ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    
    				<div class="box">
    
    				<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    					<?php the_post_thumbnail('portfolio-thumb'); ?>
    					</a>
    
    				</div>
    
    				<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
    				<div class="entry">
    					<p>Show project details</a></p>
    				</div>
    
    			</div>
    
    		<?php endwhile; ?>
    
    		<div class="clear">
            </div>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('← Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries →') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    
    	<?php endif; ?>
    	<?php wp_reset_query(); ?>
    
        </div>
    
    <?php get_footer(); ?>

    Any help greatly appreciated!

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    try it with this:

    <?php 
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    query_posts( array('category_name' => 'portfolio',  'posts_per_page' => 2, 'paged' => $paged)); 
    
    ?>

    Thread Starter Ben

    (@benredpath)

    Hi keesiemeijer, thanks for your quick response!

    However that way still doesn’t include the ‘Newer Entries’ link and the ‘Older Entries’ link just gets stuck on the first page!

    Moderator keesiemeijer

    (@keesiemeijer)

    Can we have a link to the page in question to see what is going on.

    Thread Starter Ben

    (@benredpath)

    Hi keesiemeijer, I’ve put a link up to the theme I’m working on here:

    https://benredpath.co.uk/test/

    thanks for having a look!

    Moderator keesiemeijer

    (@keesiemeijer)

    Do you use this template as a static home page?

    Thread Starter Ben

    (@benredpath)

    Yes I do, will that affect it?

    Moderator keesiemeijer

    (@keesiemeijer)

    Yes this is a known bug.

    try it with this:

    <?php
    if ( get_query_var('paged') ) {
    			$paged = get_query_var('paged');
    		} elseif ( get_query_var('page') ) {
    			$paged = get_query_var('page');
    		} else {
    			$paged = 1;
    		}
    query_posts( array('category_name' => 'portfolio',  'posts_per_page' => 2, 'paged' => $paged));
     ?>

    Thread Starter Ben

    (@benredpath)

    keesiemeijer, that works great – thank you so much!

    I have just noticed another thing though, the link for ‘blog’ in the header bar only works when on the first page of posts, any idea why that might be?

    Thanks again!

    Ben

    Moderator keesiemeijer

    (@keesiemeijer)

    Can you put the content of your theme’s header.php in the pastebin and give a link to it here.

    Thread Starter Ben

    (@benredpath)

    Hi keesiemeijer,

    I’ve put the file here:

    https://wordpress.pastebin.com/qyxG11nM

    Thanks!

    Ben

    Moderator keesiemeijer

    (@keesiemeijer)

    these are links that are hardcoded.

    <ul id="topmenu">
             <li><a href="/contact/">contact</a></li>
             <li><a href="/c.v/">c.v</a></li>
             <li><a href="index.php/blog">blog</a></li>
            </ul>

    you can change the links to the correct path if you want. A better way is to make use of template tags to show them.
    wp_list_pages()
    wp_list_categories()
    wp_nav_menu()

    Thread Starter Ben

    (@benredpath)

    Of course, that has solved it…once again, thank you so much for your help keesiemeijer!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Custom loop pagination’ is closed to new replies.