WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination of posts in page (22 posts)

  1. rkorebritscs
    Member
    Posted 4 years ago #

    Hi Guys,

    I have a page and in that page I load posts from a category:
    $posts = get_posts('category=3&numberposts=4');

    The problem i'm having here is that I can't get any pagination to work.
    I've tried the wp_paginate() function and some other pagination functions, but they don't work.

    I think it is because I don't show the post from a category page, but from a page page. I do this because of the URL, I don't want /category/categoryname/ but /pagename/

    Any advice?

    Thanks!

  2. senthilkumars
    Member
    Posted 4 years ago #

    Follow the Steps:
    1. download the paging plugin from devdevote.com/cms/wordpress-plugins/wp-paging/
    2. put this plugin inside wp-content/plugins
    3. activate the plugin
    4. then,put the below code inside your file

    $result= new WP_Query();
    $args = array(
    'base' => add_query_arg( 'paged', '%#%' ),
    'total' => $result->max_num_pages,
    'current' => $page
    );

    if (have_posts()) : while (have_posts()) : the_post();
    .....

    echo wp_paging('total'.$search->max_num_pages);

    Regards,
    Senthil

  3. rkorebritscs
    Member
    Posted 4 years ago #

    Thanks Senthil! Neat plug-in, but I haven't got it working yet. I think this is due to the way i collect the data:

    $posts = get_posts('category=3');
    foreach($posts AS $v){
     //html
    }
    
    $args = array(/*stuff*/);
    
    if(function_exists('wp_paging'))
      echo wp_paging('show_all=true');
    else
      echo "function does not exist!";

    The plugin is installed correctly, do i need to do something with: "if (have_posts()) : while (have_posts()) : the_post();"??
    I'm still a bit of a WordPress n00b..

    Thanks again!

  4. rkorebritscs
    Member
    Posted 4 years ago #

    ps.
    I have been looking at "The loop", but I don't really see how I use this in combination with getting posts from a category?

  5. senthilkumars
    Member
    Posted 4 years ago #

    <?php
    	/* Pagination */
    	    $category_id = $_GET['category_id'];
    
    	    global $wp_query;
    	    $result = new WP_Query('cat='.$category_id);
    
    		$args = array(
    		 'base' => add_query_arg( 'paged', '%#%' ),
    		 'total' => $result->max_num_pages,
    		 'current' => $page
    	    );
        ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    ...

    <?php
    /* Pagination */
    echo wp_paging('total='.$result->max_num_pages);
    ?>

    Regards,
    Senthil

  6. keesiemeijer
    moderator
    Posted 4 years ago #

    In stead of get_posts use a regular loop with WP_query.
    Put this before the loop:

    <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    <?php $my_query = new WP_Query("paged=$page&category=3&numberposts=4"); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
  7. rkorebritscs
    Member
    Posted 4 years ago #

    Hi Guys,

    I still can't get this working... This is what I have now:

    @kees:

    <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    <?php $my_query = new WP_Query("paged=$page&category=3&numberposts=4"); ?>
    <?php
    while($my_query->have_posts()){
      $my_query->the_post();
    
      echo the_title();
      echo the_content();
    }
    
    $args = array(
      'base' => add_query_arg( 'paged', '%#%' ),
      'total' => $my_query->max_num_pages,
      'current' => $page
    );
    
    if(function_exists('wp_paging'))
      echo wp_paging();
    else
      echo "does not exist!";
    
    ?>

    the_title() and the_content() show correctly, but there is no paging...

    @senthil:

    global $wp_query;
    $result = new WP_Query('cat=3');
    
    $args = array(
     'base' => add_query_arg( 'paged', '%#%' ),
     'total' => $result->max_num_pages,
     'current' => $page
      );
    
    if (have_posts()):
      while (have_posts()): 
    
        the_post();
    
        echo the_title();
        echo the_content();
    
      endwhile;
    endif;
    
    if(function_exists('wp_paging'))
      echo wp_paging();
    else
      echo "does not exist!";

    Not working at all...

  8. keesiemeijer
    moderator
    Posted 4 years ago #

    Have you tried it with wp-pagenavi
    I've done pagination of a category on a page with that plugin.
    This was my loop:

    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query("paged=$page&cat=3");
    if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
    while ($wp_query->have_posts()) : $wp_query->the_post();

    and ending of the loop with this

    <?php endwhile ?>
    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    <?php $wp_query = null; $wp_query = $temp; ?>
  9. keesiemeijer
    moderator
    Posted 4 years ago #

    Otherwise try it with next_posts_link and previous_posts_link:

    <?php next_posts_link('Older Entries »', 0); ?>
    <?php previous_posts_link('« Newer Entries', '0') ?>

    and see if that works.

  10. rkorebritscs
    Member
    Posted 4 years ago #

    Thanks Kees,

    This works perfect!

  11. keesiemeijer
    moderator
    Posted 4 years ago #

    Glad you got it working! Now, please use the dropdown at top right to mark this topic 'Resolved'.

  12. rkorebritscs
    Member
    Posted 4 years ago #

    I'm back. I've just upgraded to WP3 and the paging doesn't work anymore.

    I've tried it with WP Paging, WP Paginate and WP Pagenavi...

    The loop works fine and the posts are displayed correctly. but there is no navigation...

    $wp_query = null;
          $wp_query = new WP_Query();
          $wp_query->query("paged=$page&cat=".$this->category."&posts_per_page=".$this->postsPerPage);
    
          while ($wp_query->have_posts()):
                echo $wp_query->post->ID; //example
          endwhile;
            if(function_exists('wp_paging')){
    
              $args = array(
                'base' => add_query_arg( 'paged', '%#%' ),
                'total' => $wp_query->max_num_pages,
                'current' => $page
              );
              wp_paging();
            }
            else die("paging plug-in not installed!");
  13. keesiemeijer
    moderator
    Posted 4 years ago #

    do you have this in your query as wel:

    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $temp = $wp_query;

    and after the loop:
    <?php $wp_query = null; $wp_query = $temp; ?>

  14. rkorebritscs
    Member
    Posted 4 years ago #

    I already have that:

    [Code moderated as per the Forum Rules. Please use the pastebin]

  15. keesiemeijer
    moderator
    Posted 4 years ago #

    try putting this in your globals:
    global $wp_query, $meta ,$paged;

  16. rkorebritscs
    Member
    Posted 4 years ago #

    sorry guys, i've been really stupid. I don't have enough posts in this new install to show the paging :-(

    * going to jump off a bridge

  17. keesiemeijer
    moderator
    Posted 4 years ago #

    No problem. Glad you solved it! Now, please use the dropdown at top right to mark this topic 'Resolved'.

  18. modifiedcontent
    Member
    Posted 4 years ago #

    How would the query look for not just one category, but posts in general on a custom page?

  19. modifiedcontent
    Member
    Posted 4 years ago #

    Does this still work in WordPress 3.0...?

  20. wayneberry
    Member
    Posted 4 years ago #

    any update on this in wordpress 3.0?

    i'm using query_posts on a page, and although pagination works it simply shows a link to previous posts which always links to /page/2

    however when i do the same thing on index.php and set wordpress to display posts on homepage it works fine...

  21. mardala
    Member
    Posted 3 years ago #

    I had a similar experience and this is what I ended up doing.

    Example has 10 posts per page and is very similar to the above where you have a $paged var @ the top, then build a query.

    Note - I added a check to see if there were less than 10 posts for the page because the posts flowed in varying div blocks and needed to make sure the divs were closed properly.

    @ the top: $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    before the loop:

    $offset = ($paged*10)-10;
    $offset = ($offset <= 0) ? 0 : $offset;
    $args = array('category_name'=>'nightlife','posts_per_page'=>10,'paged'=>$paged,'offset'=>$offset);
    query_posts($args);
    $posts_remaining = $wp_query->found_posts-$offset;

    Then just loop thru the posts normally, ie. while have_posts(); the_post(); etc...

  22. mardala
    Member
    Posted 3 years ago #

    Just to clarify the above, I used this to get pagination working on pages that were categories. So the url above would be like: site.com/nightlife/page/2/, site.com/nightlife/page/3/ etc ...

    This was in conjunction with the WP-Pagination plugin http://wordpress.org/extend/plugins/wp-paginate/

Topic Closed

This topic has been closed to new replies.

About this Topic