List related posts in same category
-
I’ve been struggling over this for a week, but I’m just not good enough at coding PHP to do it myself. Can anyone help me? I’d like to produce a list of all the posts in the same category as the currently displayed post. For example, if the post X is in category #1, I’d like a list of all the posts in category #1 (including post X) listed in alphabetical order by name. Just the title with a permalink.
The code I came up with only uses the current “post” call, which doesn’t work unless you are already listing everythingn in a category:$blog=1;
require_once("wp-blog-header.php");
foreach ($post as $posts) { start_wp(); //start of loop
?>- " rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?>
<?
} //end of loop
?>
I’ve tried writing a function which will do a new query, but I don’t really know how to do that correctly, and I don’t know how to integrate it into the loop written above:
function post_by_cat() {
global $tableposts, $posts, $id, $post, $wpdb;
$current_category = $post->post_category;
$catposts = @$wpdb->get_row("SELECT ID,post_title FROM $tableposts WHERE post_category='$current_category' ORDER BY post_date");
}Thanks to anyone who can help!!!
I’m doing this to create a menu on a site with only a small number of posts per category, since it is more of a static site than a blog. However, it would probably be useful for bloggers to have a “related posts” plug-in that worked in a similar way. My goal, however, is to make a dynamic “menu” that will show all posts in a category, allowing me to only create one “hard-link” to the main post in each category from the main page. - " rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?>
- The topic ‘List related posts in same category’ is closed to new replies.