• Resolved WorthyWP

    (@worthywp)


    Hello,

    I’m trying to set pagenation on a specified page template that I use, however when I added it to my functions.php — it crashed. I’m not sure what I’m doing wrong. Any help would be greatly appreciated!

    Thanks!

    add_action('pre_get_posts', 'myprefix_query_offset', 1 );
    function myprefix_query_offset(&$query) {
    
        //Before anything else, make sure this is the right query...
        if ( is_page_template( 'archives2.php' ) {
            return;
        }
    
        //First, define your desired offset...
        $offset = 7;
    
        //Next, determine how many posts per page you want (we'll use WordPress's settings)
        $ppp = get_option('posts_per_page');
    
        //Next, detect and handle pagination...
        if ( $query->is_paged ) {
    
            //Manually determine page query offset (offset + current page (minus one) x posts per page)
            $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
    
            //Apply adjust page offset
            $query->set('offset', $page_offset );
    
        }
        else {
    
            //This is the first page. Just use the offset...
            $query->set('offset',$offset);
    
        }
    }
    
    add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
    function myprefix_adjust_offset_pagination($found_posts, $query) {
    
        //Define our offset again...
        $offset = 7;
    
        //Ensure we're modifying the right query object...
        if ( is_page_template( 'archives2.php' ) {
            //Reduce WordPress's found_posts count by the offset...
            return $found_posts - $offset;
        }
        return $found_posts;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • a bracket is missing in this line near the top and in the same line near the bottom:

    if ( is_page_template( 'archives2.php' ) {

    should be:

    if ( is_page_template( 'archives2.php' ) ) {
    Thread Starter WorthyWP

    (@worthywp)

    Thanks! Its so obvious! I can’t believe I didn’t see that! I was thinking it was a problem with calling the parameter is_page_template!

    Thread Starter WorthyWP

    (@worthywp)

    What would be the reason my home page would now be offset by 7 on each category?

    Thread Starter WorthyWP

    (@worthywp)

    I was able to fix the issue a bit differently. I found a code on another site that I was able to implement in front of the loops on the template page.

    I added this code to my functions.php file —

    function my_post_limit($limit) {
    	global $paged, $myOffset;
    	if (empty($paged)) {
    			$paged = 1;
    	}
    	$postperpage = intval(get_option('posts_per_page'));
    	$pgstrt = ((intval($paged) -1) * $postperpage) + $myOffset . ', ';
    	$limit = 'LIMIT '.$pgstrt.$postperpage;
    	return $limit;
    }

    Then I simply added this code on my page I was running the loop on —

    <?php add_filter('post_limits', 'my_post_limit'); ?>
    <?php
    global $myOffset;
    $myOffset = 7;
    $temp = $wp_query; $wp_query = null;
    $wp_query = new WP_Query();  $wp_query->query('cat=1,3' . 'offset='.$myoffset . '&paged='.$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    That solved my issue with page pagination and offsets on an individual template page.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Trying to Add Offset into Template Pages’ is closed to new replies.