WordPress: create a page template that displays posts from a category that has the same name as the page.

Question:

How to create a page template that displays all the posts from a category that has the same name as the page itself?

Answer:

Here’s the code:

<?php /*
Template Name: ListPostsInCategoryThatHasSameNameAsPage
*/ ?>

<?php get_header(); ?>

<div id="content">
<div id="main">

	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
	<h1><?php the_title(); ?></h1>
		<?php the_content(); ?>
	<?php endwhile; else: endif; ?>

	<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
	<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
	<p><?php the_content(); ?>
	<?php endwhile; else: endif; ?>

</div>
</div>

<?php get_footer(); ?>

Note: remember to apply this new template for your page.

=========================================

Credits: mores

Source: http://wordpress.org/support/topic/241049?replies=27

=========================================

Another option is to have the category’s slug the same as the page’s permalink. This will solve the problem of the slightly difference in the category’s name and the page’s name. Here it compares the slug and the permalink, rather than the names.

Replace the line:

<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>

With this line:

<?php query_posts('category_name='.get_permalink().'&post_status=publish,future');?>

=========================================

For child theme:

If you’re creating a child theme (i.e. a child theme of Thematic framework), create a new function in the file function.php of your child theme, to replace the indexloop in the theme’s index.php

Here’s the code to put in function.php :

// Create a new indexloop function for the Categories page template.
function categories_page_indexloop() {
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts(‘category_name=’.get_permalink().’&post_status=publish,future’);?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h2>
<p><?php the_content(); ?>
<?php endwhile; else: endif;
}

Copy index.php in thematic folder, and create a new file called index_newtemplate.php in your child theme folder.

To call the new function, replace the line:

<?php thematic_indexloop() ?>

With this line:

<?php categories_page_indexloop() ?>

=========================================

Question: Thematic child theme: How to Can I replace the loop with a filter?

Answer: put the code below in your child theme’s function.php


function remove_index_loop() {

remove_action('thematic_indexloop', 'thematic_index_loop');

}

add_action('init', 'remove_index_loop');

function my_index_loop() {

<!-- Your code -->

}

add_action('thematic_indexloop', 'my_index_loop');

Source: http://themeshaper.com/forums/topic/can-i-replace-the-loop-with-a-filter

Advertisements

One Comment

  1. Pictrix
    Posted June 3, 2010 at 8:31 pm | Permalink

    Thanks so much! I really needed this.
    Your explanation was very simple and elegant.

One Trackback

  1. […] (ID#13), and three respective pages that display all the posts of each category. (See how I do that here.) Now I want to display a list of posts that belong to the same category in each […]

%d bloggers like this: