0

First of all: I don't understand the meaning or the action that http://myurl/page/2 does?

I checked the Net tab in Firebug and saw that my page takes more than 3 seconds to load /page/2... but I don't need pagination at my front page at all. I don't get where it comes from either.

My question: how do I disable / remove this and show it only when pagination is needed?

This is the query I am using to get posts:

<?php
query_posts(array('orderby' => 'rand', 'showposts' => 10));
if (have_posts()) :
while (have_posts()) : the_post(); 
?>
18
  • Is this a pre-built Theme you are using or did you make this yourself? It's important to know that to help you on this one. Commented Feb 10, 2016 at 18:43
  • This is a pre-built theme that I am using.
    – Johan
    Commented Feb 10, 2016 at 18:43
  • Does the Theme have auto-loading on for posts? "Infinite Scroll" sometimes they call it. Commented Feb 10, 2016 at 18:45
  • Nope, it doesn't... It's the theme "Responsive" btw.
    – Johan
    Commented Feb 10, 2016 at 18:50
  • /page/2 is typically for pagination, like having more than one set of results to go through -- like Posts or a list of Pages. That query, normally would be saying "get the next set of pages". Anywhere on your page that might be doing that? Commented Feb 10, 2016 at 18:51

1 Answer 1

1

If the front page is set to display posts then the query for those posts will run regardless of wether your theme shows these posts or not. That's why you're getting pagination with no posts. You can test this by temporarily removing your themes front-page.php (or whatever it is). You should see all your posts with correct pagination.

You have 2 options to get around this:

Use a static front page

If you set the front page to static (this can be an empty page) the original query for the homepage will now be just this page - therefore, no pagination.

Alter the main query

Instead of creating a new query using query_posts (which you shouldn't be doing anyway - but that's another question), alter the original query. You should be able to do this using the pre_get_posts hook and a combination of is_home() and is_front_page(). Something like this:

/**
 * Alters the font-page main query
 */
add_action( 'pre_get_posts', 'wpse_217284_alter_front_page_query' );
function wpse_217284_alter_front_page_query( $query ) {

    // don't run on the backend
    if ( is_admin() )
      return;

    // Only run on the front page main query
    if ( $query->is_main_query() && is_front_page() ) {

        $query->set( 'orderby', 'rand' );
        $query->set( 'posts_per_page', 10 );
    }

    return;
}

Note, this isn't tested and is just an example, so may not work as is, but it should give you an idea of what to do.

5
  • Thanks man. I will check this out and let you guys know. Appreciated.
    – Johan
    Commented Feb 10, 2016 at 21:11
  • The code snippet does not work indeed. But I don't get your explanation... so if I use a static front page, than it's impossible to have pagination? And what does this main query alter function do to be precise?
    – Johan
    Commented Feb 21, 2016 at 21:30
  • If you use a static front page it's not impossible to get pagination - you just won't get pagination unless its needed. The pre_get_posts docs tells you what it does. It alters the main query before it is run which means you don't need to use query_posts which you were before. The is_front_page makes sure it only runs on the home page. The rest should be self explanatory or easy enough to figure out.
    – Cai
    Commented Feb 21, 2016 at 21:38
  • The code I posted should work as long as you don't use query_posts or set up any other query and set the front page to display posts.
    – Cai
    Commented Feb 21, 2016 at 21:39
  • No problem. Happy to help :)
    – Cai
    Commented Feb 21, 2016 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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