WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Pagination of posts in page

[Resolved] Pagination of posts in page

  • rkorebritscs
    Member

    @rkorebritscs

    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!

Viewing 15 replies - 1 through 15 (of 21 total)
  • senthilkumars
    Member

    @senthilkumars

    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

    rkorebritscs
    Member

    @rkorebritscs

    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!

    rkorebritscs
    Member

    @rkorebritscs

    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?

    senthilkumars
    Member

    @senthilkumars

    <?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

    keesiemeijer
    Moderator

    @keesiemeijer

    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(); ?>

    rkorebritscs
    Member

    @rkorebritscs

    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…

    keesiemeijer
    Moderator

    @keesiemeijer

    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; ?>

    keesiemeijer
    Moderator

    @keesiemeijer

    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.

    rkorebritscs
    Member

    @rkorebritscs

    Thanks Kees,

    This works perfect!

    keesiemeijer
    Moderator

    @keesiemeijer

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

    rkorebritscs
    Member

    @rkorebritscs

    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!");
    keesiemeijer
    Moderator

    @keesiemeijer

    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; ?>

    rkorebritscs
    Member

    @rkorebritscs

    I already have that:

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

    keesiemeijer
    Moderator

    @keesiemeijer

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

    rkorebritscs
    Member

    @rkorebritscs

    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

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘[Resolved] Pagination of posts in page’ is closed to new replies.