WordPress.org

Ready to get started?Download WordPress

Forums

Pagination on a generated page not blog post (7 posts)

  1. kidanim3d
    Member
    Posted 10 months ago #

    Hi I have created template which creates a summary of all the pages of a particular category however it has become rather long and I am looking to use pagination to automatically create pages. I have looked at wp_link_pages though it does not seem to work.

    <?php while ( have_posts() ) : the_post(); ?>
    	<div id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
    
    			<?php the_content();/* The loop */ ?>
    			<?php
    			  	global $wpdb, $image, $feat_image, $category, $content, $args;
    
    				$posts = $wpdb->get_results ("SELECT id, post_title, post_date_gmt, post_content, guid, post_type, comment_count, post_excerpt, post_status
    												FROM $wpdb->posts wposts
                                         			WHERE ((wposts.post_type = 'page') and (wposts.post_status='publish'))
    ORDER BY wposts.post_date_gmt DESC");
    				if(empty($posts)){
    			?>
    				<p>Bad</p>
    
    			<?php
    				}else{
    					foreach($posts as $post){
    						$category = get_the_category( $post->id );
    						if(isset($category) && !empty($category)){
    							if( $category[0]->cat_name=="Blog" ){
    
    			?>
    								<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    /* Summary creation code */								</article><!-- #post -->
    
    								<!---?php comments_template(); ?>-->
    			<?php
    							}/*End of If Statement - Cateegory Check*/
    
    						}/*End of If Statement - Isset*/
    
    					}/* End of For Each */
    
    				}/* End of If  Statement*/
    			?>
    		</div><!-- #content -->
    	</div><!-- #primary -->
    <?php endwhile; ?>
  2. wpismypuppet
    Member
    Posted 10 months ago #

    You won't be able to paginate using a $wpdb query. Your query looks simple enough, however, to just use get_posts(). Here is how I would do it:

    <?php
    	$posts_per_page = 6; // how many posts you want per page
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args = array(
    		'post_type' => 'page',
    		'post_status' => 'publish',
    		'paged' => $paged
    	);
    	$postslist = get_posts( $args );
    	$max_num_pages =  ceil( sizeof( $postlist ) / $posts_per_page );
    	$args['posts_per_page'] = $posts_per_page;
    	$postslist = get_posts( $args );
    	if( ! empty( $postlists ) ) :
    		echo '<div id="primary" class="content-area">';
    		echo '<div id="content" class="site-content" role="main">';
    		foreach( $postslist as $post) :
    			setup_postdata($post);
    			the_content();
    			$category = get_the_category();
    			if( isset( $category ) && ! empty( $category ) ) :
    				if( $category[0]->cat_name == "Blog" ) :
    					echo '<article id="post-' . get_the_ID() . '" ';
    					post_class();
    					echo '></article>';
    				endif;
    			endif;
    		endforeach;
    		$big = 999999999;
    		$pagelinks = paginate_links( array(
    			'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    			'format' => '?paged=%#%',
    			'current' => max( 1, get_query_var('paged') ),
    			'total' => $max_num_pages
    		));
    		if( ! empty( $pagelinks ) ) :
    			echo '<div class="postnav clear">' . $pagelinks . '</div>';
    		endif;
    		echo '</div></div>';
    	else :
    		header( 'Location: ' . esc_url( home_url( '/' ) ) . '/404' );
    	endif;
    ?>

    Note that we need to do get_posts() twice. The first time get ALL the pages you are looking for, so we can obtain a total number of posts since $wp_query->max_num_pages usually doesn't work for me (even when you call it globally). Then we call it again to get the first 6 posts (or pages).

    This code isn't fully tested, but I've used very similar code on many of our websites, so I know it "should" work. The only thing I can't guarantee is your code where you try to get the category. However it looks right to me. Let me know how it pans out.

  3. kidanim3d
    Member
    Posted 10 months ago #

    Could you elaborate on a few things, first paged is not a parameter that can be sent through get_pages according the the function reference below, how is it use by the function?
    http://codex.wordpress.org/Function_Reference/get_pages

    What does this line do? $args['posts_per_page'] = $posts_per_page;

  4. wpismypuppet
    Member
    Posted 10 months ago #

    You are correct... you cannot pass paged as a parameter for get_pages(). But you'll note that I use get_posts(), not get_pages(). Don't be confused by the word "post" as EVERYTHING in WordPress is actually a "post". These "posts" have labels such as attachment, post, page, and then custom post types. So using get_posts() and looking for "post_type" of "page", you'll only return pages, and exclude everything else.

    And the line $args['posts_per_page'] = $posts_per_page; simply adds the parameter of "posts_per_page" to the arguments so when get_posts() is called the second time, you return only x number of posts (where x = $posts_per_page variable which is assigned as the first line of code in the block I sent you). By using a variable (which you can see is used more than once throughout the block of code), you can just change the variable from 6 to however many posts (or pages in your case) you want to show per page without having to scan all the code for the number 6 and having to change it a few different times. It reduces user error if you miss one :)

    We don't want to add "posts_per_page" the FIRST time we call get_posts() because we need to get ALL results, not just 6 at a time. This is critical for making the pagination work as the pagination needs to know the total number of posts so it can paginate properly.

  5. kidanim3d
    Member
    Posted 10 months ago #

    I am not confused :) . Get Pages and Get Posts are two separate functions see below, http://codex.wordpress.org/Function_Reference/get_pages, http://codex.wordpress.org/Function_Reference/get_posts.

    The same is true for get_posts if you look at the reference, you cannot pass in paged.

    Btw You didn't have to go as far as explain what a variable is, I have been an IT professional for several years. ;)

  6. wpismypuppet
    Member
    Posted 10 months ago #

    I apologize if I explained too much. I assumed from your question that you might not have know since you asked what the line did. I didn't mean to insult if indeed I did.

    As far as get_posts()... if you look at the reference, you can pass "paged" because get_posts() uses WP_Query(). I find it better than WP_Query because it won't alter the main loop. There is a line on the reference page that says:

    For full parameters list see WP_Query.

    On that page, it indicates the use of the "paged" parameter. Please try the code... I have used the exact same code on 5 different websites we've used and it works. The only part that may not work is where I implemented your code, because I haven't tested that piece, but I'm sure it works.

  7. kidanim3d
    Member
    Posted 10 months ago #

    No that is fine, I misinterpreted the get_posts documentation.

    I have implemented the solution but the final page has no pagination links and featured image cannot be pulled using $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->id) ); .

    Any Ideas?

Reply

You must log in to post.

About this Topic

Tags