/ Published in: PHP
By default, this template will use the page title as the category to pull from – but you can also create a custom field with ‘category’ as the name and the preferred category name as the value.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php //Template Name: Category Listing /* This page will grab posts from a category that matches the page title OR This page will grab posts from a category that matches the value for the 'category' custom field */ $pageID = $post->ID; $cat = get_post_meta($pageID, 'category',true); get_header(); ?> <div id="content" class="narrowcolumn"> <?php /* below, get the title and any content for the actual page - before we go and get the other posts */ ?> <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?> <div class="post" id="post-<?php the_ID(); ?>"> <h2 class="clear"><?php the_title(); ?></h2> <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?> </div> <?php edit_post_link('Edit this entry.', '<p class="clear editLink">', '</p>'); ?> <?php endwhile; endif; ?> <?php /* below, change the query to find posts of the specified category */ ?> <?php $the_query = new WP_Query('category_name='.$cat); ?> <?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> <!--<h3 class="clear"><a href="<?php the_permalink($post->ID); ?>"><?php the_title(); ?></a></h3>--> <h3 class="clear"><?php the_title(); ?></h3> <div class="entry"> <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?> </div> <?php edit_post_link('edit this page', '<p class="clear">', '</p>'); ?> </div> <?php endwhile; endif; ?> <?php wp_reset_query(); ?> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
URL: http://trepmal.com/themes/page-template-for-categories/