Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
3  
Why the aversion to plugins? –  chrisguitarguy Oct 19 '11 at 20:06
    
Because if it is a plugin, it's not built into the theme. –  AndrettiMilas Oct 19 '11 at 20:26
4  
@Lucas Wynne If you want it built in, copy/paste some plugin code into your themes functions.php file. –  kaiser Oct 19 '11 at 20:50

2 Answers 2

up vote 4 down vote accepted

If 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.

share|improve this answer
    
They aren't working in my theme. –  AndrettiMilas Oct 19 '11 at 20:26
2  
Okay. Well, without seeing any of your code, it's hard to say why. Are there any PHP errors or warnings? Do you have multiple posts inserted for the functions to fetch link for? –  chrisguitarguy Oct 19 '11 at 20:46
    
I have updated my question above. –  AndrettiMilas Oct 19 '11 at 20:57
    
Try it with the third TRUE argument and let us know. –  chrisguitarguy Oct 19 '11 at 21:11
    
Third true argument? –  AndrettiMilas Oct 19 '11 at 21:13
<?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=" ">&laquo; Previous post<br /><strong>&quot;'. $prev_title . '&quot;</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 &raquo;<br /><strong>&quot;'. $next_title . '&quot;</strong></a>' . "\n";
                }
?>
share|improve this answer
1  
Please format your codes/answer and add explanation to it. –  Maruti Mohanty Nov 20 '13 at 10:35
    
Anyway I can limit that to the taxonomy the CPT is in? –  gil hamer Apr 8 at 9:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.