WordPress.org

Ready to get started?Download WordPress

Forums

custom pagination problem (12 posts)

  1. roelof
    Member
    Posted 2 years ago #

    Hello,

    I try to make a custom pagination work.
    I put this in my functions.php

    function pagination_april( $query ) {
    if ( $query->monthnum=04&year=2005 ) {
    $query = new WP_Query( 'posts_per_page=1' );
    }
    }

    add_action( 'pre_get_posts', 'pagination_april' );

    But it won't work.
    Can anyone tell me what went wrong here.

    Roelof

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

  3. roelof
    Member
    Posted 2 years ago #

    Oke,

    So it must be :

    function pagination_april( $query ) {
     if ( $query->monthnum==04&&year==2005 ) {
     $query = new WP_Query( 'posts_per_page=1' );
     }
     }
    
    add_action( 'pre_get_posts', 'pagination_april' );
  4. roelof
    Member
    Posted 2 years ago #

    Hello,

    I tried it but still no luck.
    Still all the articles are displayed.

    Anyone a idea ?

    Roelof

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

    try to treat year and month seperately in the if statement:

    function pagination_april( $query ) {
     if ( $query->monthnum==04 && $query->year==2005 ) {
     $query = new WP_Query( 'posts_per_page=1' );
     }
     }
    
    add_action( 'pre_get_posts', 'pagination_april' );
  6. roelof
    Member
    Posted 2 years ago #

    Hello,

    So i tried this :

    function pagination_april( $query ) {
     if ( $query->year==2005) {
        if ( $query->monthnum==04) {
     $query = new WP_Query( 'posts_per_page=1' );
     }
     }
    }
    
    add_action( 'pre_get_posts', 'pagination_april'

    But now I get a 500 error on my site.

    Roelof

  7. roelof
    Member
    Posted 2 years ago #

    Hello,

    The 500 error is solved.
    But still the pagination is not working.

    But I did some research and found out this.

    The variable month and year are still 0.
    But there is a variable m which contains 200504.

    So I changed it to this :

    function pagination_april( $query ) {
     if ( $query->m==200504) {
           $query = new WP_Query( 'posts_per_page=1' );
     }
     }
    
    add_action( 'pre_get_posts', 'pagination_april'
    But then again a 500 error.
    It seems that WordPress cannot do what I need.

    Roelof

  8. Helen Hou-Sandi
    WordPress Dev
    Posted 2 years ago #

    You shouldn't need to set $query to a new WP_Query, but rather just do $query->set( 'posts_per_page', 1 );.

  9. roelof
    Member
    Posted 2 years ago #

    Thank you
    But I still get 500 error.

    Roelof

  10. Helen Hou-Sandi
    WordPress Dev
    Posted 2 years ago #

    What did you do before that got rid of the 500 error?

    This may not be the cause of the problem, but you might try:

    function pagination_april( $query ) {
        // in 3.3, use is_main_query() instead
        global $wp_the_query;
        if ( $query->m == 200504 && $query === $wp_the_query ) {
            $query->set( 'posts_per_page', 1 );
        }
     }
    
    add_action( 'pre_get_posts', 'pagination_april' );
  11. roelof
    Member
    Posted 2 years ago #

    I changed the /> to ?>

    Still a 500 error. It looks like WordPress does not like the use of the variable m .If I change it to month no 500 error but then the script does not work as expected.

    Roelof

  12. roelof
    Member
    Posted 2 years ago #

    Oke,

    I found out if I change the permalinks to this : http://test.tamarawobben.nl/2005/04/

    year and month have the good values of 2005 and 04.
    But still this is not workimg.

    function pagination_april( $query ) {
    
    if ( $query->year==2005) {
        if ( $query->monthnum==04) {
                  $query->set( 'posts_per_page', 1 );
    }
     }
    }
    
    add_action( 'pre_get_posts', 'pagination_april');

    Roelof

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.