Forum Replies Created

Viewing 15 replies - 1 through 15 (of 44 total)
  • Thread Starter a_mulg

    (@a_mulg)

    I believe that’s working perfectly now! Thanks again for all your help on this, hopefully it’s the last issue I’ll have trouble with!

    Thread Starter a_mulg

    (@a_mulg)

    Ah apologies, that code is still there, I just didn’t paste it in. So having moved $paged above offset, it now looks like this, though the end result hasn’t changed…

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
    $first_page_post_count = 11;
    $subsequent_pages_post_count = 10;
    $paged = $paged ? $paged : 1;
    
    if($paged > 1){
    // not first page
    $posts_per_page = $subsequent_pages_post_count;
    
    if($paged == 2){
    // second page
    $offset = $first_page_post_count;
    } else {
    // subsequent pages
    $offset = $first_page_post_count + ($subsequent_pages_post_count * ($paged - 2));
    }
    
    } else {
    // first page
    $offset = 0;
    $posts_per_page = $first_page_post_count;
    }
    
    $query_args = array(
        'post_type' => array ('post' , 'reviews' , 'trailer' , 'feature'),
        'posts_per_page' => $posts_per_page,
        'offset' => $offset
    );
    
    $the_query = new WP_Query( $query_args );

    I wonder if the problem is something to do with my pagination code, which of course also uses the $paged variable. It doesn’t seem to be causing problems on the other pages that use it though.

    I also have this code in my loop, not sure if any of this could confuse things…

    <?php $counter = 0; if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div class="col push--bottom <?php if(!is_paged() && ($counter==0)) { echo 'xsmall-12 featured-article'; $counter++; } else { echo 'xsmall-12 small-6 article-preview'; } ?> ">
    Thread Starter a_mulg

    (@a_mulg)

    Yeah, I’ve been using $offset and removed $paged from my query, so it now just looks like this…

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
      $query_args = array(
       'post_type' => array ('post' , 'reviews' , 'trailer' , 'feature'),
       'posts_per_page' => $posts_per_page,
       'offset' => $offset
      );
    
     $the_query = new WP_Query( $query_args );

    But unfortunately it’s still returning the same posts! That’s currently with no offset function in my functions file, just the pagination code from my original post.

    Thread Starter a_mulg

    (@a_mulg)

    I think that’s what my current code is supposed to be doing, but could be wrong.

    $first_page_post_count = 11;
    $subsequent_pages_post_count = 10;
    $paged = $paged ? $paged : 1;
    
    if($paged > 1){
    // not first page
    $posts_per_page = $subsequent_pages_post_count;
    
    if($paged == 2){
    // second page
    $offset = $first_page_post_count;
    } else {
    // subsequent pages
    $offset = $first_page_post_count + ($subsequent_pages_post_count * ($paged - 2));
    }
    
    } else {
    // first page
    $offset = 0;
    $posts_per_page = $first_page_post_count;
    }

    I think this is saying the second page should be offset by the first page post count (11), but this part I’m not entirely clear on…

    $offset = $first_page_post_count + ($subsequent_pages_post_count * ($paged - 2));

    Obviously the first part is saying the offset should be 11 + 10, but I’m not sure what the rest of that equation is supposed to do? I feel like that is the part that’s preventing the offset from working correctly, as the rest of it looks like it should work?

    Thread Starter a_mulg

    (@a_mulg)

    Ok, so I’ve reverted back to my original code and what I’m not quite understanding is what $page_offset is, particularly if I don’t include the previous code in my functions file?

    So at the moment I have the following query on my front page and have removed the function, but I’m receiving an Undefined variable error for page_offset…

    <?php
    
        $first_page_post_count = 11;
        $subsequent_pages_post_count = 10;
        $paged = $paged ? $paged : 1;
    
        if($paged > 1){
        // not first page
        $posts_per_page = $subsequent_pages_post_count;
    
        if($paged == 2){
        // second page
        $offset = $first_page_post_count;
        } else {
        // subsequent pages
        $offset = $first_page_post_count + ($subsequent_pages_post_count * ($paged - 2));
        }
    
        } else {
        // first page
        $offset = 0;
        $posts_per_page = $first_page_post_count;
        }
    
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $query_args = array(
        'post_type' => array ('post' , 'reviews' , 'trailer' , 'feature'),
        'posts_per_page' => $posts_per_page,
        'offset' => $page_offset,
        );
    
        $the_query = new WP_Query( $query_args );
    ?>

    So I guess what I’m wondering is where/how to define the page_offset variable? I tried something like this $offset = $first_page_post_count + ($subsequent_pages_post_count * ($query->query_vars['paged']-1)); but not sure if I’m even on the right track with that!

    Thread Starter a_mulg

    (@a_mulg)

    Hey bcworkz!

    I’ve had a look at the article you linked and tried out the code in there. I stripped out what I had in my front page template too, other than the query that defines the post types.

    So after that, with the front page set to static page, it still returns the same posts on every page. If I set the front page to latest posts, it shows the first page correctly, but returns a 404 on any subsequent page.

    So my loop currently looks like…

    <?php
    
                            $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                            $query_args = array(
                            'post_type' => array ('post' , 'reviews' , 'trailer' , 'feature'),
                            'posts_per_page' => 10,
                            'paged' => $paged
                            );
    
                            $the_query = new WP_Query( $query_args );
                        ?>
    
                        <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    //content 
    
    <?php if (function_exists("pagination")) {
      pagination($the_query->max_num_pages);
     } ?>
    
                        <?php wp_reset_postdata(); ?>

    And my function looks like pretty much like the article…

    add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
    function myprefix_adjust_offset_pagination($found_posts, $query) {
    
        //Define our offset again...
        $offset = 1;
    
        //Ensure we're modifying the right query object...
        if ( $query->is_home() ) {
            //Reduce WordPress's found_posts count by the offset... 
            return $found_posts - $offset;
        }
        return $found_posts;
    }

    I’m not quite sure which parts of my own code I need to keep and which parts of this code to modify? The counter and offset I was using appear to work correctly but getting them to work in conjunction the pagination seems to be the hard part!

    Thread Starter a_mulg

    (@a_mulg)

    Ah ok. So if it’s a case of just having them appear the same, I think the simplest option is to match up the markup for my custom pagination to the posts pagination as that layout is suitable. Thanks again for the help!

    Thread Starter a_mulg

    (@a_mulg)

    Ok, the good news is that the pagination you posted works perfectly on my archive page! However, I’m concerned about having different pagination on this page and the other pages that require custom queries.

    At the moment, my archive.php, search.php and index.php files use the standard query if ( have_posts() ) : while ( have_posts() ) : the_post(); and the pagination you suggested works perfectly.

    But I have a number of templates for my custom taxonomies and my front-page which use a variation of a custom query that looks like this, with the post type changed where necessary…

    <?php
    
       $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
            $query_args = array(
            'post_type' => array ('reviews'),
            'posts_per_page' => 10,
            'paged' => $paged
        );
    
        $the_query = new WP_Query( $query_args );
    ?>
    
    <?php $counter = 0; if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    With those pages, I’m using the pagination I posted yesterday, with this code which connects to a function…

    <?php if (function_exists("pagination")) {
         pagination($the_query->max_num_pages);
     } ?>

    That works on those custom taxonomy pages, though I don’t currently have a working solution for my front-page.php which uses a different custom query, code to offset the number of posts and a counter to style the first post differently.

    The pagination you have suggested seems to be the cleanest and easiest option and works great on the archive pages I mentioned above but is there a way to apply that to my custom taxonomies and front page so it is consistent across the site?

    Thread Starter a_mulg

    (@a_mulg)

    Hi @bcworkz,

    Apologies for dragging this up again but I’ve just stumbled upon something I hadn’t thought of before: pagination. I think this will be a fairly easy fix as I think I’ve almost got it but if you could point me in the right direction, that would be great.

    I’ve got pagination working on other pages using this function…

    function pagination($pages = '', $range = 4)
    {  
         $showitems = ($range * 2)+1;  
     
         global $paged;
         if(empty($paged)) $paged = 1;
     
         if($pages == '')
         {
             global $wp_query;
             $pages = $wp_query->max_num_pages;
             if(!$pages)
             {
                 $pages = 1;
             }
         }   
     
         if(1 != $pages)
         {
             echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
             if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
             if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
     
             for ($i=1; $i <= $pages; $i++)
             {
                 if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                 {
                     echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                 }
             }
     
             if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";  
             if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
             echo "</div>\n";
         }
    }

    And I output it on the page using…

                                <?php if (function_exists("pagination")) {
                                    pagination($the_query->max_num_pages);
                                } ?>

    That seems to work fine on my other page templates for custom posts and I’ve got it working for my index.php too. However, I’ve done that using a custom query instead of the standard loop that we’re using on the archive. So I’ve tried adding these lines to the arrays we built…

    'posts_per_page' => 10,
    'paged' => $paged

    But while the posts are displaying correctly, I’m getting these errors instead of my pagination:

    Notice: Undefined variable: the_query in localhost\archive.php on line 62

    Notice: Trying to get property of non-object in localhost\archive.php on line 62

    So ultimately I’m wondering if it’s possible to add my numbered pagination to the query we previously built?

    Thread Starter a_mulg

    (@a_mulg)

    Hi @nuancedesignstudio,

    Thanks for your reply.

    You’re right about the content, so I have added that in and the posts display. However, it has had ultimately the same result as the code I posted above in that I can move back to older pages, but they still just show the same posts as the first page.

    Thread Starter a_mulg

    (@a_mulg)

    Ok I now have numbered pagination displaying and it changes the page url (eg. localhost/page/2 etc.) however the same posts just load on each page. My current loop is…

    <?php
    
                            $first_page_post_count = 11;
                            $subsequent_pages_post_count = 10;
                            $paged = $paged ? $paged : 1;
    
                            if($paged > 1){
                            // not first page
                            $posts_per_page = $subsequent_pages_post_count;
    
                            if($paged == 2){
                            // second page
                            $offset = $first_page_post_count;
                            } else {
                            // subsequent pages
                            $offset = $first_page_post_count + ($subsequent_pages_post_count * ($paged - 2));
                            }
    
                            } else {
                            // first page
                            $offset = 0;
                            $posts_per_page = $first_page_post_count;
                            }
    
                            $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                            $query_args = array(
                            'post_type' => array ('post' , 'reviews' , 'trailer' , 'feature'),
                            'posts_per_page' => $posts_per_page,
                            'offset' => $offset,
                            'paged' => $paged
                            );
    
                            $the_query = new WP_Query( $query_args );
                        ?>
    
                        <?php $counter = 0; if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                        <div class="col push--bottom <?php if(!is_paged() && ($counter==0)) { echo 'xsmall-12 featured-article'; $counter++; } else { echo 'xsmall-12 small-6 article-preview'; } ?> ">
    
                            //content
    
                        <?php $counter++;
                            if ($counter % 2 == 0) {
                                echo '</div><!-- end of row -->
                                <div class="row js-equal-height">';
                            }
                        endwhile; ?>
    
                        <?php else: ?>
                            <article>
                                <h1>Sorry...</h1>
                                <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
                            </article>
                        <?php endif; ?>
    
                        <?php if (function_exists("pagination")) {
                            pagination($the_query->max_num_pages);
                        } ?>
    
                        <?php wp_reset_postdata(); ?>

    And my pagination function in my functions file looks like this…

    function pagination($pages = '', $range = 4)
    {  
         $showitems = ($range * 2)+1;  
     
         global $paged;
         if(empty($paged)) $paged = 1;
     
         if($pages == '')
         {
             global $wp_query;
             $pages = $wp_query->max_num_pages;
             if(!$pages)
             {
                 $pages = 1;
             }
         }   
     
         if(1 != $pages)
         {
             echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
             if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
             if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
     
             for ($i=1; $i <= $pages; $i++)
             {
                 if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                 {
                     echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                 }
             }
     
             if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";  
             if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
             echo "</div>\n";
         }
    }

    Any guidance on why it’s just returning the same posts would be much appreciated!

    Thread Starter a_mulg

    (@a_mulg)

    Hi @nuancedesignstudio,

    I tried your suggestion too, replacing my own query with your code. The pagination did actually work in as far as I could see the ‘older entries’ link and use it to go back a page, however none of my posts were actually visible. I think I’m probably putting it in the wrong place.

    Thread Starter a_mulg

    (@a_mulg)

    Hi @dipakcg ,

    I tried your suggestion and couldn’t get it to work, though wasn’t entirely sure how to implement it. I tried pasting it before my loop in place of my current query and it didn’t return any posts. Where specifically in my template should I put your code?

    Thread Starter a_mulg

    (@a_mulg)

    I think it works perfectly! I’ve tried my custom taxonomies, tags, author and search results and all appear to be working as intended. Thanks again for all your help and patience, apologies that it ended up taking so long!

    Thread Starter a_mulg

    (@a_mulg)

    I’ve discovered something that might make the author situation a lot easier! Since we changed the archive page and it started working correctly, I left the function as it was. However, I just tried removing the author line and everything still works as intended without it! So it looks like we can scrap that line?

    For what it’s worth, at the moment when I visit the author’s name on the front end, it returns the archive page and only displays posts, not any of the custom taxonomies. I’m sure that is expected behavior but thought it might be worth mentioning!

    So as it stands, my custom taxonomies (actors, writers, directors) display all posts from custom post types, but not ‘posts’. Tag and author return only the ‘posts’ type and none of the custom post types. I also tried the search and it uses the search.php template, which returns both my custom post types and ‘posts’.

    I think you’re right about the page post type though, which doesn’t seem to appear in any search results. While I’ll only be using it for generic content (about us etc), it may be useful if that displays in the search. But yeah, they won’t be needed in any other query.

Viewing 15 replies - 1 through 15 (of 44 total)