Paging does not work with Offset Wordpress argument

Asked

Viewed 157 times

-2

I need to list the posts on index php. and I want to skip three recent posts. But when browsing the pagination it does not advance the numbers nor change the posts. Or rather, paging doesn’t work when I try to skip these 3 recent posts.

In the functions.php:

function wp_pagination($pages = '', $range = 9)
{  
    global $wp_query, $wp_rewrite;  
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;  
    $pagination = array(  
        'base' => @add_query_arg('paged','%#%'),  
        'format' => '',  
        'total' => $wp_query->max_num_pages,  
        'current' => $current,  
        'show_all' => true,  
        'type' => 'plain'  
    );  
    if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );  
    if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );  
    echo '<div class="wp_pagination">'.paginate_links( $pagination ).'</div>';
}

In the index php.:

<?php global $query_string; parse_str( $query_string, $my_query_array ); 
$paged = ( isset( $my_query_array['paged'] ) && !empty( $my_query_array['paged'] ) ) ? $my_query_array['paged'] : 1; 
query_posts("posts_per_page=10&offset=3paged=$paged"); ?> 

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>


<a href="<?php the_Permalink(); ?>"> <?php the_post_thumbnail(); ?> </a>
<a href="<?php the_Permalink(); ?>"> <h2><?php the_title(); ?></h2> </a>

    <?php endwhile; ?>
<p><?php wp_pagination();?></p>

<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

See that I’m using the argument offset=3 to skip these three posts and I’m trying to keep the pagination working but so far without any success.

  • Jr17, Portuguese, por favor;

  • Excuse me @Andersoncarloswoss I do not know how to use this forum right and also do not like to keep asking questions like these but as I have already searched the solution of this and did not find, and I thought here I could get this help.

  • @Jr17 Paging works normally without Offset?

  • @Heathcliff yes it works normally without offset

1 answer

0


Jr17, the error is found in the index.php file

Right here oh:

query_posts("posts_per_page=10&offset=3paged=$paged");

The error there is that you are using & to join the offset in posts_per_page, however, you are not using & to connect paged with Ofsset, for this reason paged does not work.

Exchange the previous line for:

query_posts("posts_per_page=10&offset=3&paged=$paged");

OBS: The Offset logic is that once the page opens, it will skip 3 posts, ie the 3 first posts on page 1, the 3 first posts on page 2 and etc, then it will always skip 3 posts on all pages if you want it to skip only the 3 first posts from FIRST page, you must do an if to launch a variable offset

Something like that:

<?php global $query_string; parse_str( $query_string, $my_query_array ); 
$paged = ( isset( $my_query_array['paged'] ) && !empty( $my_query_array['paged'] ) ) ? $my_query_array['paged'] : 1; 
// Define o offset como 0
$offset = "offset=0";
// Caso seja a página 1, define o offset como 3
if ($paged == 1) {
$offset = "offset=3";
}

query_posts("posts_per_page=10&$offset&paged=$paged"); ?>

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>


<a href="<?php the_Permalink(); ?>"> <?php the_post_thumbnail(); ?> </a>
<a href="<?php the_Permalink(); ?>"> <h2><?php the_title(); ?></h2> </a>

    <?php endwhile; ?>
<p><?php wp_pagination();?></p>

<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
  • thank you so much for your help, I was very happy to have helped me, However I made the modifications that you say and in the pagination /1/ it skips the last three posts but when I go to pagination /2/ and the others it goes back to show the first three prosts and all the others of the page /1/ no matter how far I go, So I took advantage of his logic of using if as a conditional page /1/ and it worked. And it doesn’t repeat on the other pages. I changed this snippet like this: if ($paged == 1) {&#xA;$offset = "offset=3";&#xA;}&#xA;query_posts("posts_per_page=10&$offset&paged=$paged"); ?> can be ?

  • @Jr17 Running for you is what it’s all about, buddy, don’t worry Haha! I will edit my answer with your changed code, if the answer is correct, please indicate it as the right xD answer

  • all right can edit yes I indicate.

  • Ready to go @Jr17 ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.