0
votes

Ok, I have searched and searched through stackoverflow and google and couldn't find what I was looking for. I'm looking for a way to have my next and previous buttons to link to my posts that are in the same post type term.

I have a post type named "Projects" and the taxonomy is "projects_category". I have several terms under that taxonomy and when I'm viewing a single project post, I want to be able to click next and previous within that term. Is there a way to do this?

Update (11/13/13) (again):

This is what I have, now.

<?php
$projectCat = wp_get_post_terms($postid,"projects_category");
if(count($projectCat)){
$projectC = $projectCat[0]->slug;
}
else{
$projectC = "";
}
$args = array(

'post_type'=>'project_post',
'showposts' => -1,
'tax_query' => array(
array(
        'taxonomy' => 'projects_category',
        'terms' => '{$projectC}',  //define your category id or slug here
            'field' => 'slug',
        )
),
'order'=>'ASC', 
);

$postlist = get_posts($args);
$posts = array();
foreach ($postlist as $post) {
$posts[] += $post->ID;
}

$prev_post = get_previous_post(); 
$prevID = $prev_post->ID ;
?>
<?php 
$next_post = get_next_post();
$nextID = $next_post->ID ;
?>          

<?php if (!empty($prevID)) { ?>

<li><a href="<?php echo get_permalink($prevID); ?>"
title="<?php echo get_the_title($prevID) ." ". $projectC; ?>" 
class="p_prev widget_anim"><span></span>Previous</a>
</li>
<?php }

if (!empty($nextID)) { ?>
<li><a href="<?php echo get_permalink($nextID); ?>" 
title="<?php echo get_the_title($nextID) ." ". $projectC;; ?>"  
class="p_next widget_anim">Next<span></span></a>
</li>
<?php }  

wp_reset_postdata();
?>

I'm getting the current term that is found on the single-project_post page and the query seems to catch it, but the query is not following that it needs to stay in the term. It's extracting posts from other terms I have. Even if I put the exact term in replace of $projectC in the query, it still gives me the wrong posts for next and previous. Can anyone help me? I appreciate the help so far from the previous answers.

3
you can not pass direct category on argument for custom taxonomy use tax_query.Ravi Patel

3 Answers

0
votes

Create functions.php in a child theme so it doesn't get overwritten by an update. Then write a function that returns an array of posts. The Codex tells you exactly how to do what your asking <?php $project_posts_array = get_posts( $args ); ?>. Then call the function from your custom post type in a link tag.

This is on the same Codex page.

    <div class="alignleft">    
    <a href="<?php echo get_permalink($prevID); ?>"
    title="<?php echo get_the_title($prevID); ?>">Previous</a>
    </div>
    <?php }
    if (!empty($nextID)) { ?>
    <div class="alignright">
    <a href="<?php echo get_permalink($nextID); ?>" 
     title="<?php echo get_the_title($nextID); ?>">Next</a>
    </div>
0
votes
<?php
                 $args = array(
                'tax_query' => array(
                    array(
                            'taxonomy' => 'projects_category',
                            'field' => 'id',
                            'terms' => '2'  //define your category id hear
                            )
                    ),
                    'post_type'=>'project_post',
                    'order'=>'ASC',
                    'posts_per_page'=>-1
            );

                        $postlist = get_posts($args);
$posts = array();
foreach ($postlist as $post) {
   $posts[] += $post->ID;
}
                <?php
        $prev_post = get_previous_post(); 
        $prevID = $prev_post->ID ;
?>
<?php 
        $next_post = get_next_post();
        $nextID = $next_post->ID ;
?>          

    <?php if (!empty($prevID)) { ?>

    <li><a href="<?php echo get_permalink($prevID); ?>"
            title="<?php echo get_the_title($prevID); ?>" 
            class="p_prev widget_anim">Previous</a>
            </li>
    <?php }

    if (!empty($nextID)) { ?>
        <li><a href="<?php echo get_permalink($nextID); ?>" 
            title="<?php echo get_the_title($nextID); ?>"  
            class="p_next widget_anim">Next</a>
            </li>
    <?php }  

    wp_reset_postdata();
?>
0
votes

had the same issue, I found my solution here: http://bucketpress.com/next-and-previous-post-link-in-same-custom-taxonomy

Hope it works for you!