- This topic is empty.
-
AuthorPosts
-
April 14, 2011 at 1:11 pm #32357
ShawnRamsey
MemberI’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,
ShawnApril 14, 2011 at 2:19 pm #51271magnuseide
MemberHi
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).'';
}
?>
April 14, 2011 at 2:48 pm #51258ShawnRamsey
MemberAlmost 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.
April 14, 2011 at 3:32 pm #51261magnuseide
MemberAhh, 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';
}
?>
April 14, 2011 at 3:41 pm #51262ShawnRamsey
MemberWorked 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.
April 14, 2011 at 3:49 pm #51234magnuseide
MemberThanks 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.
April 14, 2011 at 3:54 pm #51205ShawnRamsey
MemberThanks 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.
September 30, 2011 at 8:05 am #88268wanderson
MemberHi 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.
-
AuthorPosts
- The forum ‘Other’ is closed to new topics and replies.