WordPress.org

Ready to get started?Download WordPress

Forums

Can anyone help me get pagination working correctly in my theme. (23 posts)

  1. djdeejay
    Member
    Posted 2 years ago #

    Hi,

    Ive been setting up a few custom page templates so I can display posts from certain categories with my own customisation on page.

    Im a little stuck though because I've just realised pagination isn't working. It reaches the maximum posts and displays the pagination navigation from the loop however click on next or one of the page numbers just reloads the same page.

    Here is a past of the code on my homepage for example.

    http://pastebin.com/BnwNH67G

    Thanks in advance

  2. esmi
    Forum Moderator
    Posted 2 years ago #

  3. djdeejay
    Member
    Posted 2 years ago #

    Thanks Esmi,

    Id seen most of those but stumped across this page http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html which seemed to be exactly what I wanted.

    So i replaced the code in the past bin with this.

    <?php query_posts( array( 'cat' => 9, 'posts_per_page' => 3, 'paged' => get_query_var('paged'))); ?>

    However I'm left with exactly the same problem, it display the first 3 posts fine and the pagination options appear, but clicking on page two just refreshes the page…

    I tried changing "paged" for "page" - as it is a page template as that website suggests, but in this case it strangely just displays one post only and pagination still doesn't work.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts( array( 'cat' => 9, 'posts_per_page' => 3, 'paged' => $paged); ?>
  5. djdeejay
    Member
    Posted 2 years ago #

    No worries, with a little bit of tweaking Ive cracked it. There was some code missing from the top of the page which seems to create the pagination upon page refresh.

    "<?php $save_post = $post;
    $post = get_post( get_option('page_for_posts') ); ?>"

    I changed it back to 'page' and it has worked.

    One question though. On my home page I don't really want pagination anyway - I just want it to display the latest 3 posts dynamically. Is there a setting to turn off and hide the pagination options literally just displaying the three posts in a grid? I can't see the option anywhere on the links provided to be able to do this...

  6. djdeejay
    Member
    Posted 2 years ago #

    esmi.

    In my custom loop here is the pagination code at the bottom.

    <?php pagination(); ?>

    Is there a way to modify this so the pagination code is remove if it is the "front page"?

  7. esmi
    Forum Moderator
    Posted 2 years ago #

    There's no pagination() function in WordPress core, so I can only assume that this is coming from your theme or a plugin. Start by finding out which it is.

  8. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try and wrap the code into a conditional statement using is_paged()

    http://codex.wordpress.org/Function_Reference/is_paged

  9. djdeejay
    Member
    Posted 2 years ago #

    esmi - the pagination() is part of my themes custom loop (loop-grid.php) and it relates to a plugin thats built into the theme (rather than on the plugin page)

    I thought there might be a simple way to wrap pagination() in some php code which tells it not to run if its on the front page…although not sure if the loop sequence would know if it was the front page or not?

    I suppose another way would be to duplicate loop-grid.php and remove the <?php pagination(); ?> bit which works and tell that specific page to use the alternative loop - however I'm stumped as to how to do that easily and quickly either...

  10. esmi
    Forum Moderator
    Posted 2 years ago #

    As per alchymyth's suggestion:

    <?php if(( is_home() || is_front_page() ) && !is_paged() ) pagination(); ?>

    should work.

  11. djdeejay
    Member
    Posted 2 years ago #

    Argh, I thought that had cracked it.

    It does work in that it removes pagination from the homepage…but only because it removes it from everywhere - as if I've just deleted the <?php pagination(); ?> line.

  12. esmi
    Forum Moderator
    Posted 2 years ago #

    <?php if(( is_home() || is_front_page() ) && !is_paged() ) echo 'Boo!';?>
    works just fine for me as a test.

  13. djdeejay
    Member
    Posted 2 years ago #

    Where do I need to put that in relation to <?php pagination(); ?> in the loop esmi?

  14. djdeejay
    Member
    Posted 2 years ago #

    Ok, I entered this…

    <?php if(( is_home() || is_front_page() ) && !is_paged() ) echo 'Boo!';
    
    		pagination();?>

    But it doesn't work. Pagination isn't broken like it was in the first one now, but instead it works everywhere, including on the home page as if it was just the standard <?php pagination(); ?> code.

  15. esmi
    Forum Moderator
    Posted 2 years ago #

    Erm - you did realise that the echo "Boo" was purely for my testing, yes? If you want to use it yourself, you need to try:

    <?php if(( is_home() || is_front_page() ) && !is_paged() :
    echo 'Boo!';
    pagination();
    endif;?>
  16. djdeejay
    Member
    Posted 2 years ago #

    No I wasn't, I don't know what the "echo" part it supposed to do in this code…I don't know PHP only enough to blag my way through…I think you're assuming a level of knowledge here with short answeres thats probably making it 5x longer to get to the finish.

    What is the 'echo' part for? Displaying text somewhere? Where and why…you need to explain to me what this extra part does and why it should work or I'm just stabbing in the dark.

    Im entering this into my loop-grid.php. I'll reiterate before, will "is_home" commands work from inside the loop?

  17. esmi
    Forum Moderator
    Posted 2 years ago #

    What is the 'echo' part for? Displaying text somewhere?

    Yep! If I'm having problems with some code block not working, I echo out text (usually "Boo"") to try and track down where the code is failing.

    will "is_home" commands work from inside the loop?

    Absolutely. And is_front_page and is_paged().

  18. djdeejay
    Member
    Posted 2 years ago #

    Ok, well I tried the code above and now I'm just getting syntax errors…

    Im not sure why it didn't work originally, this seems like a no go-er for some reason...

  19. djdeejay
    Member
    Posted 2 years ago #

    Unexpected ':' apparently.

  20. esmi
    Forum Moderator
    Posted 2 years ago #

    I am sorry - that's my fault. I managed to strip off a closing bracket. Tip: always make sure that the number of ( equal the number of ).

    Try:

    <?php if(( is_home() || is_front_page() ) && !is_paged() ) :
    echo 'Boo!';
    pagination();
    endif;?>
  21. djdeejay
    Member
    Posted 2 years ago #

    Really thought it had worked then, but its back to just demoing pagination on every page… :-(

  22. djdeejay
    Member
    Posted 2 years ago #

    "demoing" = removing

  23. djdeejay
    Member
    Posted 2 years ago #

    Ok lets see.

    Here is the code on my custom page template for the homepage that calls the posts.

    <!-- posts grid -->
    		<?php query_posts( array( 'cat' => 9, 'posts_per_page' => 3, 'paged' => get_query_var('page'))); ?>
    
    		<?php } ?>
    
    		<?php if ( of_get_option('blog_layout') == "0" ) {
    		get_template_part('loop', 'grid');
    		} else {
    		get_template_part('loop');
    		} ?>
    		<?php wp_reset_query() ?>

    Is there anything there, which would stop the code in the loop from working?

    Or rather…

    Here is the code in my OTHER pages which call the posts where I want the pagination to REMAIN. Eg these are NOT my home page. This is the code on their page.

    <?php query_posts( array( 'category_name' => writing, 'posts_per_page' => 3, 'paged' => get_query_var('paged'))); ?>
    
    			<?php } ?>
    
    			<?php if ( of_get_option('blog_layout') == "0" ) {
    				get_template_part('loop', 'grid');
    			} else {
    				get_template_part('loop');
    			} ?>
    			<?php wp_reset_query() ?>

    Basically the same, but with the category changed and the word "page" changed to "paged" in the query for the non-homepage pages. Could this be something to do with the loop?

Topic Closed

This topic has been closed to new replies.

About this Topic