Wordpress : List posts in category on '.com/categoryname' and display post on '.com/categoryname/post-name'
-
18-09-2019 - |
Question
I have a permalink structure of /%catergory%/%postname%/.
When I go to blah.com/categoryname I want all posts in that specific category to be listed. When I go to blah.com/categoryname/post-name I want just the specific post to be displayed.
I have made a category specific template (category-5.php) and have got as far as...
// Display all post titles in category loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php endwhile; ?>
<?php endif; ?>
// Display specific post in category loop
<?php if ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="storycontent">
<?php the_content(); ?>
</div>
<?php endif; ?>
...but obviously I only want the first loop to display when the url is blah.com/categoryname, and the second loop to display when the url is blah.com/categoryname/post-name.
Any thoughts? thanks
Solution
You can't have two loops running on the same page as you do here.
I believe you need to separate out the two things you're trying to do. To have a unique look/feel for the category, create a category-1.php file. To create a unique look/feel for the posts within that category, create a separate 'single' template.
This WP support thread explains how to create the 'single' template: http://wordpress.org/support/topic/266638
There are also a few "post template" plugins that help accomplish the same thing, if you prefer to go that route, for instance: http://wordpress.org/extend/plugins/post-template/
Full list here: http://wordpress.org/extend/plugins/search.php?q=post+templates
Good luck!