Skip to content

Instantly share code, notes, and snippets.

@blbwd
Created Sep 9, 2015
Embed
What would you like to do?
Show related posts on your WordPress blog for custom post type
<?php
//Register product categories
$labels = array(
'name' => _x( 'Product Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Product Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Product Categories' ),
'all_items' => __( 'All Product Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Product Category' ),
'update_item' => __( 'Update Product Category' ),
'add_new_item' => __( 'Add Product Category' ),
'new_item_name' => __( 'New Product Category' ),
'menu_name' => __( 'Product Categories' )
);
register_taxonomy('product_categories',array('products'), array(
'hierarchical' => true,
'labels' => $labels,
'query_var' => true,
'rewrite' => false,
'capability_type' => 'post',
'show_ui' => true
));
?>
<?php
global $post;
$terms = get_the_terms( $post->ID , 'product_categories');
$do_not_duplicate[] = $post->ID;
if(!empty($terms)){
foreach ($terms as $term) {
query_posts( array(
'product_categories' => $term->slug,
'showposts' => 4,
'caller_get_posts' => 1,
'post__not_in' => $do_not_duplicate ) );
if(have_posts()){
while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID;
?>
<div class="entry">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<?php the_content();?>
</div>
<?php endwhile; wp_reset_query();
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment