• Resolved nelsontubaina

    (@nelsontubaina)


    Hello my name is Lucas Gabriel and I’m a webdesigner from Brazil. I’m currently developing my first wordpress theme. In it I have a custom homepage set up.

    The homepage can be seen in the link below:
    https://www.limonadaweb.com.br/home.jpg

    Well here is the deal: In the homepage I have a custom LOOP that goes through the posts in a particular category called “noticias” (news in portuguese). Ok so far so good. The loop runs perfectly using get_posts() method. Ok.

    BUT…there’s allways a but otherwise I would not have posted this problem would I? lol. But when I try to use pagination the thing goes haywire. What happens, you may ask. Well to begin with I HAVE NO IDEA how to paginate. What I’ve done is to get the WHOLE loop AND pagination in a file named noticias_home.php and include that .php inside my home.php of my theme. And then, using jQuery ajax .load method I update the section of news withouth reloading the entire page.

    Ill just post here the entire thing and try to explain later on.

    <div id="noticias_wrapper">
    	<h2 class="red title">NOTíCIAS</h2>
    	<ul class="noticias_home">
    	<?php
    		// Posts Per Page option
    		$ppp			= 4;
    		$init_offset	= 4;
    		$custom_offset	= 0;
    		if ($paged == 0) {
    			$paged = 1;
    			$custom_offset = $init_offset;
    		} else {
    			/*$paged -= 1;//I have to do this otherwise when I go to page 2 the variable $paged = 0 goes to $paged = 2 and screws up the whole thing;*/
    			if($init_offset == 0){$paged -= 1;}
    			$custom_offset = $ppp*($paged);
    		}
    		//print_r('$PAGED '.$paged.' -- ');
    		//print_r('$INIT_OFFSET '.$init_offset.' -- ');
    		//print_r('$CUSTOM_OFFSET '.$custom_offset);
    		$args		= Array('category_name' => 'noticias', 'offset' => $custom_offset, 'posts_per_page' => 4);
    		$not_home = get_posts($args);
    		$i = 0;
    		foreach($not_home as $post) : setup_postdata($post);
    	?>
    		<?php if($i%2 == 0) : ?><li class="grid_4 alpha">
    		<?php else : ?><li class="grid_4 omega">
    		<?php endif ?>
    				<h6 class="grey_light"><?php echo get_the_date('d / m / Y'); ?></h5>
    				<h5 class="grey_dark">
    					<a href="<?php the_permalink()?>">
    						<?php the_title(); ?>
    					</a>
    				</h5>
    				<?php
    					$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' =>'any', 'post_parent' => $post->ID );
    					$attachments = get_posts($args);
    					if ($attachments) {
    							$attachment = $attachments[0];
    							$img_url = wp_get_attachment_image_src($attachment->ID , 'thumbnail'); ?>
    							<a href="<?php the_permalink()?>">
    								<img src="<?php echo $img_url[0] ?>" height="<?php echo $img_url[2] ?>" width="<?php echo $img_url[1] ?>" />
    							</a>
    						<?php }
    					else{
    						echo the_excerpt();
    					}
    				?>
    				<a href=" <?php get_permalink() ?> " class="leia_tudo">LEIA TUDO</a>
    			<?php if($i%2 == 0) : ?></li>
    			<?php else : ?></li><div style="clear:both"></div>
    			<?php endif ?>
    		<?php
    			$i++;
    			endforeach;
    	?>
    	</ul>
    	<div id="home_pagination">
    		<?php
    		$pages = '';
    		$number_posts = intval(get_category_by_slug('noticias')->count);
    		$max = ceil(($number_posts - $init_offset) / $ppp);
    		//print_r($paged);
    		if (!$current 		= get_query_var('paged')) $current = 1;
    		$args['base'] 		= str_replace(999999999, '%#%', get_pagenum_link(999999999));
    		$args['total']		= $max;
    		$args['current']	= $current;
    		$args['mid_size'] 	= 3;
    		$args['end_size'] 	= 2;
    		echo paginate_links($args);
    	?>
    	</div>
    </div>

    Ok as you can see above I have the loop to display the news and the pagination function right below it. IT IS WORKING, I just don’t know why, lol. My question is: why use get_posts? I tried with query_posts and the whole thing dies off. I was also warned not to mess with the main loop. And what about the pagination? I don’t use query->max_num_pages because it gives me the wrong number of pages. Also because I keep reloading the entire thing I had to create the variable $init_offset otherwise the pagination would decrease the value of the variable $max. (if you run the code you would see that when it loads the first time #custom_offset would be 4 then the $max would be TOTAL – 4, then in the second paged $max = TOTAL – 8 and so on and so on…

    as you can see it is all very confusing…I’d like to keep things simple. I was trying before to use jQuery ajax .load method to not only load noticias_home.php but also to send it a variable that would let noticias_home.php know in wich page the navigation should be. I was unsucessfull at that.

    take a look at my ajax function wich loads noticias_home.php

    jQuery('#home_pagination a').live('click', function(e){
      e.preventDefault();
      var link = jQuery(this).attr('href');
      jQuery('#noticias_home').html('Loading...');
      jQuery('#noticias_home').load(link+' #noticias_home');
    });

    I’m sorry if it is all so confusing. I’m a newbie and trying real hard not to cry. I mean, I got the thing to work, but I think it could be better. can anyone help me?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter nelsontubaina

    (@nelsontubaina)

    hey just edited the code of the loop, commented it a lil bit to help you all understand it a lil bit better.

    <div id="noticias_wrapper">
    	<h2 class="red title">NOTíCIAS</h2>
    	<ul class="noticias_home">
    	<?php
    		// Posts Per Page option
    		$ppp			= 4;//POSTS PER PAGE
    		$init_offset	= 4;// INITIAL OFFSET
    		$custom_offset	= 0;//OFFSET THAT IS CALCULATED ACCORDING PAGINATION
    		if ($paged == 0) {
    			$paged = 1;//RESOLVING PAGINATION BUG ISSUES
    			$custom_offset = $init_offset;
    		} else {
    			//$paged -= 1;//fa?o isso por que de alguma forma o link pula de $paged = 0 para $paged = 2;
    			if($init_offset == 0){$paged -= 1;}//RESOLVING PAGINATION BUG ISSUES
    			$custom_offset = $ppp*($paged);
    		}
    		//print_r('$PAGED '.$paged.' -- ');
    		//print_r('$INIT_OFFSET '.$init_offset.' -- ');
    		//print_r('$CUSTOM_OFFSET '.$custom_offset);
    		$args		= Array('category_name' => 'noticias', 'offset' => $custom_offset, 'posts_per_page' => 4);
    		$not_home = get_posts($args);
    		//GET ALL THE POSTS FROM THE CATEGORY NOTICIAS, SHOW ONLY 4 PER PAGE AND OFFSET THE
    		//NUMBER OF POSTS I NEED TO OFFSET IN THE CURRENT PAGE IN THE NAVIGATION
    		$i = 0;
    		//BEGINNING THE LOOP...
    		foreach($not_home as $post) : setup_postdata($post);
    	?>
    		<?php if($i%2 == 0) : ?><li class="grid_4 alpha">
    		<?php else : ?><li class="grid_4 omega">
    		<?php endif ?><!-- THE IF, ELSE IS TO DIFFER THE ODD COLUMN FROM THE EVEN COLUMN-->
    				<h6 class="grey_light"><?php echo get_the_date('d / m / Y'); ?></h5>
    				<h5 class="grey_dark">
    					<a href="<?php the_permalink()?>">
    						<?php the_title(); ?>
    					</a>
    				</h5>
    				<?php
    					//IN THIS LOOP SEARCH FOR THE ATTACHMENTS OF THE GIVEN POST...
    					$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' =>'any', 'post_parent' => $post->ID );
    					$attachments = get_posts($args);
    					if ($attachments) {
    							$attachment = $attachments[0];//GET ONLY THE FIRST ATTACHMENT...
    							$img_url = wp_get_attachment_image_src($attachment->ID , 'thumbnail'); ?>
    							<a href="<?php the_permalink()?>">
    								<img src="<?php echo $img_url[0] ?>" height="<?php echo $img_url[2] ?>" width="<?php echo $img_url[1] ?>" />
    							</a>
    						<?php }
    					else{
    						//IF THERE ARE NO ATTACHMENTS (IMAGES) DISPLAY THE EXCERPT;
    						echo the_excerpt();
    					}
    				?>
    				<!-- THE 'READ MORE' LINK -->
    				<a href=" <?php get_permalink() ?> " class="leia_tudo">LEIA TUDO</a>
    			<?php if($i%2 == 0) : ?></li>
    				<!--  THE DIV CLEAR IS TO MAKE SURE ALL LINES ALIGN PROPERLY-->
    			<?php else : ?></li><div style="clear:both"></div>
    			<?php endif ?>
    		<?php
    			$i++;
    			//END OF THE LOOP...
    			endforeach;
    	?>
    	</ul>
    	<div id="home_pagination">
    		<!-- THE PAGINATION DIV AND VARIABLES AND STUFF -->
    		<?php
    		$pages = '';// I HAVE NO IDEA WHAT THIS IS DOING HERE, PRETTY SURE I CAN DELETE THIS VAR.
    		$number_posts = intval(get_category_by_slug('noticias')->count);//COUNTING THE NUMBER OF POSTS IN A GIVEN CATEGORY...
    		$max = ceil(($number_posts - $init_offset) / $ppp);//MAKING SURE THE $MAX NUMBER USED IN PAGINATION IS CORRECT...
    		//print_r($paged);
    		if (!$current 		= get_query_var('paged')) $current = 1;//I DON'T KNOW WHAT THIS DOES...
    		$args['base'] 		= str_replace(999999999, '%#%', get_pagenum_link(999999999));//I DO NOT KNOW WHAT THIS DOES...
    		$args['total']		= $max;
    		$args['current']	= $current;//I DO NOT KNOW WHAT THIS DOES...
    		$args['mid_size'] 	= 3;//I DO NOT KNOW WHAT THIS DOES...
    		$args['end_size'] 	= 2;//I DO NOT KNOW WHAT THIS DOES...
    		echo paginate_links($args);//I DO NOT KNOW WHAT THIS DOES...
    	?>
    	</div>
    </div>

    Here is an article that shows code I have used to paginate an array: https://wordpress.mcdspot.com/2010/05/24/pagination-of-array-custom-query/

    You can also use paginate_links() as shown in this article: https://wordpress.mcdspot.com/2010/11/25/pagination-using-paginate_links/

    Thread Starter nelsontubaina

    (@nelsontubaina)

    thx man, gonna go study your code, seems very helpfull and instructional. thx so much!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problems with pagination using get_posts’ is closed to new replies.