Return to Snippet

Revision: 29538
at July 31, 2010 07:03 by trepmal


Initial Code
<?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);
if (empty($cat)) $cat = strtolower($post->post_title);

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(); ?>

Initial URL
http://trepmal.com/themes/page-template-for-categories/

Initial Description
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.

Initial Title
Page Template for Categories (WordPress)

Initial Tags
wordpress

Initial Language
PHP