Problems with pagination using get_posts
-
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.jpgWell 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?
- The topic ‘Problems with pagination using get_posts’ is closed to new replies.