0

I am wondering if anyone could help me accomplish something like this: https://medium.com/thoughts-and-words/5ccef7b3e1fc with custom post types?

I have a custom post type called projects and custom taxonomy called project-category. I would like to show the next 3 projects in the same project-category on the single project page. One will show title and excerpt and the other two will just show titles.

2 Answers 2

1

From what I gather all you need to do is query the 3 posts and step though each one using the_post(). I have not used it like this so not 100% that is how it works.

the_post() Retrieves the next post, sets up the post, sets the 'in the loop' property to true.

$project_query = array(
    'posts_per_page' => 3,
    'post_type'      => 'projects',
    'taxonomy'       => 'project-category'
    );

query_posts ( $project_query ); while ( have_posts() ) :  

//Post 1
the_post();
the_title();
the_content();

//Post 2
the_post();
the_title();

//Post 3
the_post();
the_title();

endwhile;
2
  • Bash, thanks for the response! This is almost what I need. The only problem is that this pulls the next 3 posts in any project-category. I need to pull the next 3 post in the current project category. For example, if the current post is in the web-design and identity project-category, I only want to show 3 more posts from web-design/identity. How can I modify your code to use the taxonomy term? Commented May 17, 2013 at 13:39
  • looks like you found a good logical answer :) Commented May 20, 2013 at 4:18
0

Thanks to @Bash for getting me started, I've come up with a decent solution that's working well so far.

Here's the code...

// create array of current taxonomy term slugs
$current_project_term_slugs = wp_get_object_terms($post->ID, 'project_type', array('fields' => 'slugs'));
// convert array to comma seperated values in string to query posts with same taxonomy terms
$term_slugs = join(', ', $current_project_term_slugs);
// set up query args
$project_query = array(
    'posts_per_page' => 3,
    'post_type'      => 'projects',
    'project_type'       => $term_slugs,
    'post__not_in' => array($post->ID)
    );

query_posts ( $project_query ); while ( have_posts() ) :  

// next project primary
the_post();
the_title();
the_excerpt();

// next project secondary 1
the_post();
the_title();

// next project secondary 2
the_post();
the_title();

endwhile;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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