Posted By

trepmal on 07/31/10


Tagged

wordpress


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

osirisinternet
maxvee8
booski
srdjan


Page Template for Categories (WordPress)


 / Published in: PHP
 

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

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.

  1. <?php
  2. //Template Name: Category Listing
  3.  
  4. /*
  5. This page will grab posts from a category that matches the page title
  6. OR
  7. This page will grab posts from a category that matches the value for the 'category' custom field
  8. */
  9.  
  10. $pageID = $post->ID;
  11. $cat = get_post_meta($pageID, 'category',true);
  12. if (empty($cat)) $cat = strtolower($post->post_title);
  13.  
  14. get_header(); ?>
  15.  
  16. <div id="content" class="narrowcolumn">
  17.  
  18. <?php /* below, get the title and any content for the actual page - before we go and get the other posts */ ?>
  19. <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
  20. <div class="post" id="post-<?php the_ID(); ?>">
  21. <h2 class="clear"><?php the_title(); ?></h2>
  22. <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>
  23. </div>
  24. <?php edit_post_link('Edit this entry.', '<p class="clear editLink">', '</p>'); ?>
  25. <?php endwhile; endif; ?>
  26.  
  27. <?php /* below, change the query to find posts of the specified category */ ?>
  28. <?php $the_query = new WP_Query('category_name='.$cat); ?>
  29. <?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
  30.  
  31. <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
  32. <!--<h3 class="clear"><a href="<?php the_permalink($post->ID); ?>"><?php the_title(); ?></a></h3>-->
  33. <h3 class="clear"><?php the_title(); ?></h3>
  34. <div class="entry">
  35. <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>
  36. </div>
  37. <?php edit_post_link('edit this page', '<p class="clear">', '</p>'); ?>
  38. </div>
  39. <?php endwhile; endif; ?>
  40. <?php wp_reset_query(); ?>
  41.  
  42. </div>
  43.  
  44. <?php get_sidebar(); ?>
  45. <?php get_footer(); ?>

Report this snippet  

You need to login to post a comment.