WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Limit number of post on archive page (20 posts)

  1. Jay
    Member
    Posted 2 years ago #

    Hello,

    I keep getting an end syntax error. I am trying to limit the number of post displayed within a archive page. Thanks in advance.

    here is the code

  2. Jay
    Member
    Posted 2 years ago #

    I just tried using this same query on another page and it seems to work fine. Not sure why it will not work on the archive.php file..

    here is the error unexpected $end on line 56

    Thanks in advance.

  3. Jay
    Member
    Posted 2 years ago #

    Okay,

    I tried changing this

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("posts_per_page=1&paged=".$paged);
    
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    to this

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($paged . "&posts_per_page=1&paged=' .$paged);
    ?>

    but now I'm getting an T_endwhile error.

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    is the code in the pastebin the full code of the template file?

  5. Jay
    Member
    Posted 2 years ago #

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with this: http://pastebin.com/1TS5Wt31

  7. Jay
    Member
    Posted 2 years ago #

    That definitely fixed the error message. Thank you, but now I'm getting a 404 when I select the next link. It appears the pagination is not working properly.

    Its always something. :)

  8. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with this: http://pastebin.com/XpSc0JV6

  9. keesiemeijer
    moderator
    Posted 2 years ago #

    Sorry, that one contains an error. Try it with this one: http://pastebin.com/56UDXh4B

  10. Jay
    Member
    Posted 2 years ago #

    Thanks again, but that took the page navigation away and showed all the post instead of the 1. I even removed the reset since the global was in there and still no luck..

  11. keesiemeijer
    moderator
    Posted 2 years ago #

    Use the last pastebin (one above your last post).

  12. Jay
    Member
    Posted 2 years ago #

    Well that one brought back the page nav, but still getting the 404 error.

  13. Jay
    Member
    Posted 2 years ago #

    Maybe it has something to do with the structure. So here it is.

    -> parent category

    --> children category

    All of the children are displayed on a certain page. You select the category which then displays all the post within that category. This is where I want to limit the number displayed. Hopefully that gives a better picture.

    url for page that displays childern

    localhost/?page_id=5

    url for category selected

    localhost/?cat=4

    url for next post link which I get the 404 error

    localhost/?cat=4&paged=2

  14. keesiemeijer
    moderator
    Posted 2 years ago #

    I have the same 404 error on my testsite. Pagination on archive pages is always difficult (don't know why). I will test further.

  15. Jay
    Member
    Posted 2 years ago #

    Thanks a lot for your help, I really appreciate it.

  16. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it without a query on the loop with this: http://pastebin.com/M2jdLS1X

    and put this in your theme's functions.php

    function my_archive_per_page( &$q ) {
      if ( !is_admin() && $q->is_archive ){
        if ( isset( $q->query_vars['category_name']) ) {
          $catslug = get_category_by_slug($q->query_vars['category_name']);
          // change 3 to the parent category of the child categories you only want 1 post per page
          if(cat_is_ancestor_of(3, $catslug->term_id)){
            $q->set( 'posts_per_page', 1 );
          }
        }
      }
      return $q;
    }

    Change "3" to the parent category ID of the child categories.

    I couldn't get it to work with a normal post query.

  17. keesiemeijer
    moderator
    Posted 2 years ago #

    I forgot the last line here is the full code:

    function my_archive_per_page( &$q ) {
      if ( !is_admin() && $q->is_archive ){
        if ( isset( $q->query_vars['category_name']) ) {
          $catslug = get_category_by_slug($q->query_vars['category_name']);
          // change 3 to the parent category of the child categories you only want 1 post per page
          if(cat_is_ancestor_of(3, $catslug->term_id)){
            $q->set( 'posts_per_page', 1 );
          }
        }
      }
      return $q;
    }
    add_filter('parse_query', 'my_archive_per_page');
  18. Jay
    Member
    Posted 2 years ago #

    Hello,

    First, I want to thank you for all of your help, I really appreciate it.

    Using the code with the function you gave me just displays all the post again. If I used it with the query, I still get the 404 error. I tried some variations myself, but with no luck. I might need to walk away from this one for a few. :)

  19. Jay
    Member
    Posted 2 years ago #

    Im not sure what I did, but it is working now. I check back if I can figure it out.

    Thanks again for the help.

  20. keesiemeijer
    moderator
    Posted 2 years ago #

    You're welcome. I hope it keeps working. Glad you got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic