Solved

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

Posted on 2011-03-02
13
332 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 23

Expert Comment

by:jeremyjared74
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 23

Expert Comment

by:jeremyjared74
Comment Utility
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
Comment Utility
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
Comment Utility
Hi gwkg,

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

0
 
LVL 31

Expert Comment

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

Author Closing Comment

by:rajoo_sharma
Comment Utility
Thanks
0
 

Author Comment

by:rajoo_sharma
Comment Utility
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
Comment Utility
No worries rajoo.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now