This seems like the most simple task but after searching for the last 40 minutes I cannot find a way to do this!!
All I would like to do is show the title / description / and posts of the current category when on the single.php and viewing a post.
On the category page (archive.php) it was easy!
<!-- category-title -->
<?php if (is_category()) { single_cat_title(); } ?>
<!-- category-summary -->
<?php echo category_description($category); ?>
<!-- category-posts -->
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
Even if I altered the title request to say if(is_single) and use that same code on the single.php it returns nothing.
I'm getting a bit closer but still no luck with the category description and minor progress with the other two:
I was able to retrieve the title with this scary hack:
<!-- category-title -->
<?php global $post; $category = get_the_category($post->ID); echo $category[0]->name; ?>
And to retrieve the posts, this kind of works but it's returning all posts instead of only the posts in the current category:
<!-- category-posts -->
<?php query_posts($category); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
All I would like to do is show the title / description / and posts of the current category when on the single.php and viewing a post.
The conditional tag is_category is used for category pages, while the in_category tag is used for posts.
I don't really need a conditional statement though. I don't need to find out if the post is in a category nor do I need to know what category it is.
I just want to show the category description and other posts in the same category of the post I am currently viewing.
I can't believe this is that difficult! Nobody ever wants to show other posts from the current category!?
Here is an example of a code block that does what I want it to do with the exception of the fact that I to hardcode the ID of the category to get it to work:
<?php query_posts('cat=4'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
I want the ID to be defined by whatever category I am currently in. I've tried $category but then it returns all posts from all categories!
Nobody ever wants to show other posts from the current category!?
Have you considered using a plugin for this sort of thing? Here's just one of them:
http://wordpress.org/extend/plugins/similar-posts/
Try get_the_category
Thanks, but this is not happening in The Loop, it's in the sidebar. get_the_category can only be used in the loop.
Have you considered using a plugin for this sort of thing? Here's just one of them:
http://wordpress.org/extend/plugins/similar-posts/
Yes, I tried that but it doesn't quite get me where I want to go. I want to also include the category description. I figure if I can at least get that to work I may be able to then just get the posts as well....
shalako
Member
Posted 3 years ago #
Was this resolved? I'm trying to display category description on single.php
shalako
Member
Posted 3 years ago #
How I solved it:
<?php $category = get_the_category(); echo $category[0]->category_description; ?>
wasnotwas
Member
Posted 2 years ago #
Ok I have answer to this problem. If I understand the problem correctly it is this. the_catagory does not work inside the loop on single.php
so I solved it with this piece of code.
foreach((get_the_category()) as $cat) {
$categoryname = $cat->cat_name;
}
?>
<?php the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="page-title"><?php echo $categoryname; ?></h2>