Solved

WordPress 3 - Where is the template that displays posts of the selected category?

Posted on 2011-03-02
13
334 Views
Last Modified: 2012-05-11
Hi,
When I click on a category called services, the URL becomes:
http://localhost/mysite/category/services

and all the posts under this category are displayed.

I've two questions
1. How to create a page to change the default presentation of this page?

2. Can we have paginated results? e.g. show only 10 posts under this category per page.

Please guide.

Thanks
0
Comment
Question by:rajoo_sharma
  • 6
  • 4
  • 3
13 Comments
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 35019995
I'm not sure what you mean by creating a page to change the presentation. WordPress uses either index.php, cateroy.php, or the archive.php. If you want to change the output you will need to modify one of those. If your theme doesn't have an archive.php then it is using index.php. If it has an archives.php it is more than likely using it.

One way to be certain is to delete the categories.php, or remove the contents from the page editor. To do that just go to Appearance>Edit, and click the archives.php. Then select all the code and paste it into a text editor. Then delete the code from the theme editor, and click save. Now view the page and see if it changes. If it does then you know it was using the archives.php. You can now begin tweaking "the Loop".

Another solution is to view the category ID for that category, then make a custom file called category12.php (12 is just an example, you will use whatever the id for your cat is). Then you can add whatever loop you want to it and save the file then upload it to the theme directory.

If you are unsure of what I am talking about, read this page from the WordPress Codex:
http://codex.wordpress.org/Category_Templates
0
 
LVL 31

Expert Comment

by:gwkg
ID: 35020895
jeremey made a typo that might screw you up...

its category-12.php not categoy12.php

Its usually easier to use the category slug.  in your case it's category-services.php.

So duplicate category.php or archive.php, rename the duplicate file category-services.php and edit away.  It will only change the display of your services category.
0
 

Author Comment

by:rajoo_sharma
ID: 35024096
Thanks guys,

It helped me, I created category.php and its working fine now.

The only thing now I want to understand is how to paginate it? I'm currently doing the following and its listing all the posts under a category:

while (have_posts()) : the_post(); ?>
		
					<h1><a href="<?php the_permalink(); ?>" title="<?php the_title();//printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
					<!--<p class="post_author"><em>by</em> <?php the_author(); ?></p>-->
					<div class="format_text">
<?php the_content('<p>Read the rest of this entry &raquo;</p>'); ?>
<?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
					</div>
		
				
		
				<?php comments_template(); ?>
<?php 
					endwhile;

Open in new window


How do I get paginated results e.g. 10 posts per page and then a navigation bar at the bottom of the page / below last post of the page.

Thanks
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 23

Expert Comment

by:jeremyjared74
ID: 35024479
To add newer/older post buttons, place this at the bottom of your functions.php file (make sure it isn't already there, or something like it):
<?php
function show_posts_nav() {
global $wp_query;
return ($wp_query->max_num_pages > 1);
}
?>

Open in new window

This can be place just after the closing ?> for your current functions.php


Then you add this code just before the <?php comments_template(); ?>:
<?php if (show_posts_nav()) : ?>
<div class='navigation'>
<span class='older'><?php next_posts_link('&laquo; Older Entries'); ?></span>
<span class='newer'><?php previous_posts_link('Newer Entries &raquo;'); ?></span>
</div>
<?php endif; ?>

Open in new window

0
 

Author Comment

by:rajoo_sharma
ID: 35025439
Hi Jeremey,
Thanks. Unfortunately the navigation bar (Pagination) not appearing in my page, am I missing something?
I've attached the file category.php for your reference.

Thanks category.php
0
 
LVL 31

Accepted Solution

by:
gwkg earned 500 total points
ID: 35028939
Try this plugin for true Pagination http://wordpress.org/extend/plugins/wp-paginate/

If you are only trying to limit the number of posts per page, set it in Admin > Settings > Reading
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 35029504
Hello, rajoo sharma:

Here is a better way, I tested this using the category template you posted. This will show the next/previous entries with the titles displayed:

THE CODE FOR THE CATEGORY.php:
<div class="align-left">
<?php previous_post_link('&laquo; %link','%title'); ?></div>
<div class="align-right"><?php next_post_link('%link &raquo;','%title'); ?>
       </div>

Open in new window


One mistake I made was telling you to place the code inside the loop, but it needs to go directly after the loop. So in your theme for the category.php, it would look like this:

<?php $post_count = 1; ?>

<?php get_header(); ?>

	<div id="content_box">
		<!--<div id="promotion-area">-->
			<!-- SideBar 7 for One line on top -->
			<!--<div id="top_line">
				<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(7) ) : ?>
				<?php endif; ?>
			</div>-->
			
			<!-- SideBar 5 for Widget Book -->
			<!--<div id="book_block">
				<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(5) ) : ?>
				<?php endif; ?>
			</div>-->
			
			<!-- SideBar 6 for Widget Lead Video -->
			<!--<div id="lead_video_block">
				<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(6) ) : ?>
				<?php endif; ?>
			</div>
		</div>-->
		
		<div id="middle_area">
<?php include (TEMPLATEPATH . '/left_bar.php')?>
			<div id="content">
			<h1><?php
					echo single_cat_title( '', false );
				?></h1>
<?php 		if (have_posts()) : ?>
		
				<div id="content_inner" class="without-right"<?php //echo (is_active_sidebar(2))? 'class="with-right"' : 'class="without-right"' ?>>
<?php
					while (have_posts()) : the_post(); ?>
		
					<h1><a href="<?php the_permalink(); ?>" title="<?php the_title();//printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
					<!--<p class="post_author"><em>by</em> <?php the_author(); ?></p>-->
					<div class="format_text">
<?php the_content('<p>Read the rest of this entry &raquo;</p>'); ?>
<?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
					</div>
		
				<?php comments_template(); ?>
<?php 
					endwhile;
				else: ?>
	
					<h1>Uh oh.</h1>
					<div class="format_text">
						<p>Sorry, no posts matched your criteria. Wanna search instead?</p>
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
					</div>
				
				</div>
<?php 
				endif; ?>
<?php //get_sidebar(); ?>
<div class="align-left">
<?php previous_post_link('&laquo; %link','%title'); ?></div>
<div class="align-right"><?php next_post_link('%link &raquo;','%title'); ?>
       </div>
			</div>
		</div>

		
<?php get_footer(); ?>
	</div>

Open in new window


Remove the code I posted from the functions.php file as it isn't needed.

Then you can add this to your style.css to align the post links:
.align-left {
float: left;
}
.align-right {
float: right;
}

Open in new window

0
 

Author Comment

by:rajoo_sharma
ID: 35058000
Hi  Jeremy,
Thanks again, I think either I didn't explain my requirement about pagination properly or I didn't follow you correctly.

1. I want my category template to list 10 posts per page.
2. At the bottom I want a pagination list.

I've attached what I could achieve by pasting your code, unfortunately it takes me directly to the previous or next post. I guess this is appropriate for Single Post template and not for Category template.

 What is appearing after Jermey suggested. What is appearing after Jermey suggested. This is what I want.
0
 

Author Comment

by:rajoo_sharma
ID: 35058099
Hi gwkg,

I'll try the plugin u suggested.
http://wordpress.org/extend/plugins/wp-paginate/

0
 
LVL 31

Expert Comment

by:gwkg
ID: 35089946
Remember you can set the posts per page in Settings > Reading
0
 

Author Closing Comment

by:rajoo_sharma
ID: 35294122
Thanks
0
 

Author Comment

by:rajoo_sharma
ID: 35294189
Hi Moderator,
I made a mistake, please help me correcting it.

Jeremy's first comment helped me understanding how to create template page for a category, it was a great help. I would like to award points to it. So basically I would like to share the points, gwkg's plug-in link was a great help.

How can I award both of them equal points?

Secondly I marked my question as a help, please remove it.

Jermy, apologies for the point distribution and not giving credit to your solution (by mistake).
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 35295561
No worries rajoo.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question