I have a custom post type called portfolio. I need a previous/next link WITHOUT a plugin. Anybody have a solution?
Example post:
<?php get_header(); ?>
<!-- Begin wrap -->
<div class="clear">
<div id="full_container">
<div id="content2">
<div id="content">
<!-- Grab posts -->
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<!-- Post title -->
<h1>
<?php the_title(); ?>
</h1>
<!-- The post -->
<?php the_content(); ?>
<!-- Tags -->
<h3 class="tags">
<?php the_tags('Tags ',' / ','<br />'); ?>
</h3>
<!-- End wrap -->
</div>
<!-- Next/Previous Posts -->
<div class="mp_archive2">
<div id="more_posts">
<div class="oe">
<?php previous_post_link('%link', '« Previous post', TRUE); ?>
</div>
<div class="re">
<?php next_post_link('%link', 'Next post »', TRUE); ?>
</div>
</div>
</div>
<?php endwhile; else: ?>
<p>No matching entries found.</p>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
I have a custom post type called portfolio. I need a previous/next link WITHOUT a plugin. Anybody have a solution?
Example post: http://themeforward.com/demo2/archives/portfolio/boat
<?php get_header(); ?>
<!-- Begin wrap -->
<div class="clear">
<div id="full_container">
<div id="content2">
<div id="content">
<!-- Grab posts -->
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<!-- Post title -->
<h1>
<?php the_title(); ?>
</h1>
<!-- The post -->
<?php the_content(); ?>
<!-- Tags -->
<h3 class="tags">
<?php the_tags('Tags ',' / ','<br />'); ?>
</h3>
<!-- End wrap -->
</div>
<!-- Next/Previous Posts -->
<div class="mp_archive2">
<div id="more_posts">
<div class="oe">
<?php previous_post_link('%link', '« Previous post', TRUE); ?>
</div>
<div class="re">
<?php next_post_link('%link', 'Next post »', TRUE); ?>
</div>
</div>
</div>
<?php endwhile; else: ?>
<p>No matching entries found.</p>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
Share
Improve this question
edited Oct 19, 2011 at 20:56
AndrettiMilas
asked Oct 19, 2011 at 19:25
AndrettiMilasAndrettiMilas
1,0516 gold badges26 silver badges55 bronze badges
4
- 3 Why the aversion to plugins? – chrisguitarguy Commented Oct 19, 2011 at 20:06
- Because if it is a plugin, it's not built into the theme. – AndrettiMilas Commented Oct 19, 2011 at 20:26
- 4 @Lucas Wynne If you want it built in, copy/paste some plugin code into your themes functions.php file. – kaiser Commented Oct 19, 2011 at 20:50
- 2 @kaiser assuming of course that you keep within the license and IP conditions, which isn't that easy if you are producing a theme for sale. – Phill Healey Commented Jul 24, 2016 at 11:20
3 Answers
Reset to default 15If you need next/previous links for single posts, there is the built in next_post_link
function and matching previous_post_link
, both of which should probably be used within the loop.
For archives, use next_posts_link
and previous_posts_link
.
All of these will work fine with custom post types.
<?php
$prev_post = get_previous_post();
if($prev_post) {
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" class=" ">« Previous post<br /><strong>"'. $prev_title . '"</strong></a>' . "\n";
}
$next_post = get_next_post();
if($next_post) {
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" class=" ">Next post »<br /><strong>"'. $next_title . '"</strong></a>' . "\n";
}
?>
You can use get_adjacent_post
to get the previous or next post object in the loop.
You can change the third parameter to true or false to get the next or previous post object. get_adjacent_post( false, '', true);
With this knowledge we can use get_the_permalink
to get the URL of the posts and create your own links, without needing to strip any junk that Wordpress adds in the other methods.
This method is most useful if you want to style the hyperlinks yourself and have full control over the formatting.
<?php
$next_post = get_adjacent_post( false, '', false);
$next_post_url = get_the_permalink($next_post);
$previous_post = get_adjacent_post( false, '', true);
$previous_post_url = get_the_permalink($previous_post);
?>
<a href="<?php echo $next_post_url;?>">Next post</a>
<a href="<?php echo $previous_post_url;?>">Previous post</a>