http://codex.wordpress.org/Function_Reference/get_next_post
http://codex.wordpress.org/Function_Reference/get_the_time
http://codex.wordpress.org/Template_Tags/next_post_link
something like this:
<?php previous_post_link('« %link', get_the_time('F j, Y',get_previous_post()->ID) ) ; ?> | <?php next_post_link('%link »', get_the_time('F j, Y',get_next_post()->ID) ) ; ?>
This is pretty neat, but looks a little expensive to run (I’m going to have it at the top and bottom of each blog page).
I came across an interesting article on get_adjacent_post
here.
Written out the long way for future searchers to understand, what about something like…
$in_same_cat = false;
$excluded_categories = '';
$previous = true;
$previous-post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
And then pulling the necessary info out of the returned array (simplified example):
echo $previous-post->post_date;
this is another possible way; then you need to convert the raw output of $post_date; and get the permalink to wrap around it for the link …
probably not much saved.
it would be interesting if you could run a few quantitative comparisons and report on the resources used by each method 😉
Indeed!
Yeah, you’d still have to use build a lot of it out: …the link (get_permalink($previous-post->ID)
), convert the date, a sanitized post title for the anchor tag (if desired).
I do like how I can keep all this in a variable though, for use again on the same page without querying all over again.
Thanks for your thoughts!
no problem to break the code into lines with variables; for ‘prev’ only:
<?php
$previous_post_id = get_previous_post()->ID;
$previous_time = get_the_time($previous_post_is);
$in_same_cat = false;
$excluded_categories = '';
previous_post_link('« %link', $previous_time, $in_same_cat, $excluded_categories );
?>
probably not much difference in resources, certainly easier to understand, easier to comment for educational reasons, and better to edit in the future.