Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other WordPress Pagination

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #32357
    ShawnRamsey
    Member

    I’m currently working on a WP theme using version 3.1.1 and have a small issue with category.php pagination. For design/aesthetic reasons, I wish to show the ‘previous entries’ and ‘newer entries’ at all times including first and last pages. I realize that ‘newer entries’ on the first paginated page would lead to nowhere, but need it to be displayed. For the life of me I can’t solve this problem and would appreciate any help.

    Thanks,
    Shawn

    #51271
    magnuseide
    Member

    Hi
    use get_previous_post() and get_next_post(), instead of previous_post_link() and next_post_link(). The latter to do not return anything, making it hard to determine if they printed anything (e.g. are on first or last post). You can then check there is a next or previous post with empty(), and create a conditional statement for echoing out a placeholder when there is no next/previous post.

    There does not seem to be a return version of next and previous_post_link() functions.


    $prev_post = get_previous_post();
    $next_post = get_next_post();
    if (empty($prev_post)) {
    echo 'Previous placeholder';
    }
    else {
    $prev_link = get_permalink($prev_post->ID);
    echo ''.get_the_title($prev_post->ID).'';
    }

    if (empty($next_post)) {
    echo 'Next placeholder';
    }
    else {
    $next_link = get_permalink($next_post->ID);
    echo ''.get_the_title($next_post->ID).'';
    }

    ?>

    #51258
    ShawnRamsey
    Member

    Almost what I’m looking for, magnuseide, but not quite. Thank you for responding.

    I tried the above code and it lists the next and previous posts, rather than the next and previous pages. Maybe I should rephrase the question.

    I have a category.php page that lists all posts filed under a specific category. This page lists the 9 most recent posts and then kicks in the pagination for page 2, displaying posts 10-18, etc. It is on this category.php page that I wish to display ‘previous entries’ and ‘newer entries’ at all times, especially the first and last pages.

    #51261
    magnuseide
    Member

    Ahh, i see, then my solution won’t work for you.
    This should do the trick:


    global $wp_query;
    //get current page
    $page = get_query_var('paged');
    //gets total pages
    $total_pages = absint( $wp_query->max_num_pages );

    //if it's the first page, echo placeholder
    if ($page == '0') {
    echo 'Previous placeholder';
    //and set variable to 1 to make it work in next function
    $page = 1;
    }
    //or echo link to previous page
    else {
    echo 'Previous page';
    }

    //if it's current page is the same as total number of pages, echo placeholder
    if ($page == $total_pages) {
    echo 'Next placeholder';
    }
    // else echo link to next page
    else {
    echo 'Next page';
    }
    ?>
    #51262
    ShawnRamsey
    Member

    Worked perfectly! Thank you very much, magnuseide. You clearly know your stuff. I couldn’t find much documentation on this and resorted to this forum as a last-ditch effort. This is my first time in here and I’m thoroughly impressed with the outcome. Again, thank you.

    #51234
    magnuseide
    Member

    Thanks Shawn
    Just fyi I did some edits to my post, added some comments and made some minor adjustments.

    This is actually my first time on these forums myself. Been a long time reader of css-tricks though. I wrote a function for some advanced page navigation in wordpress for a theme a few months ago and learned a lot about how WP handles pagination. It’s not well documented, and most of it I learned from reading the source files of the WP-PageNavi plugin.

    #51205
    ShawnRamsey
    Member

    Thanks for the tweaks and for commenting the code. I used the WP-PageNavi plugin on an older blog for a good part of a year. I never thought about peaking at the code and making use of it. I really appreciate your help.

    #88268
    wanderson
    Member

    Hi guys!
    After a week trying everything, trying lots of tutorials and advices, read meny forum and support I canĀ“t put the wp page navi to work. When I click the numbers I got “Not Found”. Can you help me? Thanks in advance.

    Here is my code.



Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Other’ is closed to new topics and replies.