WordPress.org

Ready to get started?Download WordPress

Forums

WP-PageNavi
[resolved] wp_pagenavi does not work with offset? (17 posts)

  1. webipsum
    Member
    Posted 2 years ago #

    Hi.

    WP_Pagenavi: Works:

    <div id="content">
    	<?php $query = new WP_Query('cat=8&posts_per_page=6' . '&paged=' . $paged); ?>
    	<?php if ($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>
    	<div class="list_posts">
    		<?php the_content(); ?>
    	</div><!-- list_posts -->
    	<?php endwhile; ?>
    		<?php if(function_exists('wp_pagenavi')) : wp_pagenavi(array( 'query' => $query )); endif; ?>
    	<?php else : ?>
    	<?php endif; ?>
    </div><!-- content -->

    But if I include: offset, don't works:

    <?php $query = new WP_Query('cat=8&posts_per_page=6$offset=3' . '&paged=' . $paged); ?>

    Please, any tips?.

    Tks a lot.

    {ipsum}

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    pagination does generally not work with using 'offset' in the query.

    please explain what you are trying to achieve; there might be a different solution.

  3. webipsum
    Member
    Posted 2 years ago #

    alchymyth.
    Display posts: cat = 4,6,8,9 and posts_per_page = 8 and offset = 3, with pagination (wp-pagenavi).

    <?php get_header() ?>
    <div id="content">
    <?php $query = new WP_Query('cat=4,6,8,9&posts_per_page=8&offset=3' . '&paged=' . $paged); ?>
    	<?php if ($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>
    		<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Leia: <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    	<?php endwhile; ?>
    		<?php if(function_exists('wp_pagenavi')) : wp_pagenavi(array( 'query' => $query )); endif; ?>
    	<?php else : ?>
    	<?php endif; ?>
    </div><!-- content -->
    <?php get_footer() ?>

    Thank you for your attention.

    {ipsum}

  4. webipsum
    Member
    Posted 2 years ago #

    alchymyth.
    Do you have any additional information?
    Tks.

  5. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    Sidenote: Adding {ipsum} to the end of each post is really distracting. Please stop doing that. We already know your user name is 'ipsum'.

  6. webipsum
    Member
    Posted 2 years ago #

    I'm sorry.
    Thank you for your comment.

  7. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    Display posts: cat = 4,6,8,9 and posts_per_page = 8 and offset = 3, with pagination (wp-pagenavi).

    The question is: why do you want offset = 3? Are you displaying the first 3 posts differently?

  8. webipsum
    Member
    Posted 2 years ago #

    Yes.
    index.php:
    1) display 3 most recent post (highlights).
    2) display others posts, without the 3 highlights (offset=3). With pagination.

  9. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    possibly get the three last posts (although this is an additional query); then use these three post ids to exclude them in your query;

    example:

    <?php
    $last_three = get_posts('cat=4,6,8,9&numberposts=3');
    $last_three_ids = array();
    if( $last_three ) foreach( $last_three as $last_post ) { $last_three_ids[] = $last_post->ID; }
    
    $query = new WP_Query(array('cat' => '4,6,8,9', 'post__not_in' => $last_three_ids, 'posts_per_page' => 8, 'paged' => get_query_var('paged') ) );  ?>

    this is supposed to replace:

    <?php $query = new WP_Query('cat=4,6,8,9&posts_per_page=8&offset=3' . '&paged=' . $paged); ?>

    edit:
    i didn't have the latest info when i posted the above;

    if you run a query with the first three posts anyway, simply collect the post IDs in an array, to be used in the second query with the 'post__not_in' parameter.

    it might be useful, if you could paste the full code of your template into a http://pastebin.com/ and post the link to it here.

    there is also the other possibility to use only one loop with a counter to distinguish the first three posts, and use an if statement to display them differently (unsing the counter and the is_paged() conditional tag).

  10. webipsum
    Member
    Posted 2 years ago #

    Pastebin, with the suggested changes (without layout (divs, etc.), for easy viewing.).
    http://pastebin.com/pQ5v5Jnb
    Selects perfectly, but does not perform paging.
    Thank you.

  11. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try to add a 'wp_reset_query()' after the loop of the first three highlight posts (for instance in line 36 of your pastebin code);

    http://pastebin.com/XqNwsaa0

  12. overexposeddesign
    Member
    Posted 2 years ago #

    Don't even try this solution will not work due to the nature of paging. I was able to find a solution Here , just follow steps 1-3 and you should be good to go, I included my category filters by just adding a $myCat variable containing the categories I needed.. If you've gotten this far all of this should be pretty straight forward.

  13. webipsum
    Member
    Posted 2 years ago #

    try to add a 'wp_reset_query()' after the loop of the first three highlight posts (for instance in line 36 of your pastebin code);

    alchymyth: You are the Master. :-)
    Worked perfectly.
    Thank you very much.

    overexposeddesign: I'll read your tip. Tks.

  14. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    glad it worked ;-)

    this is just an example how helpful it is to share the full code of any templates with problems, as the actual cause could be somewhere else in the template.

  15. overexposeddesign
    Member
    Posted 2 years ago #

    huh? NO way. I tried that and another similar solution that i deciphered from a Google translate that was in Japanese and couldn't get to work. oi vay

  16. atomik
    Member
    Posted 2 years ago #

    Hallo,
    I've two loops in a custom page template and I was raving about pagination hating offset disgrace.

    The numberpost filter solution is very clever, works like a charme.

    Thanks!

    -m.

  17. chand_4rana
    Member
    Posted 2 years ago #

    Hello,

    I have same problem here , i have query like this

    $post_info = $wpdb->get_results($srch_sql);

    Searching works perfectly and pagination looks fine but when i navigate the next page i m facing 404 page.

    can anybody solve my problem.......?

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic