List related posts of same categories.
-
So I am using the Custom Post Type UI Plugin
I have added Categories to my Custom Posts. Everything fine.
Now I want to display related posts to the categories.
I am using this code (which I took from wordpress forum) in my single.php for my normal Posts.<?php if ( is_single() ) { $categories = get_the_category(); if ($categories) { foreach ($categories as $category) { // echo " <pre>"; print_r($category); echo "</pre> "; $cat = $category->cat_ID; $args=array( 'orderby' => 'date', 'order' => 'DESC', 'cat' => $cat, 'post__not_in' => array($post->ID), 'posts_per_page'=>5, ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { echo '<h2>More Posts from'?> <?php echo get_cat_name($cat);?> <?php echo '</h2>';?> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <p><a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php endwhile; } //if ($my_query) } //foreach ($categories } //if ($categories) wp_reset_query(); // Restore global post data stomped by the_post(). } //if (is_single()) ?>
It works perfect in my single.php .. But not in my single-guides.php Which I created for my custom posts (guides).
Even if I take the ‘if(is_single())’ out. It does not do anything.
What is wrong :/ ?
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘List related posts of same categories.’ is closed to new replies.