HOWTO: One category on a page redux
-
I have a method I think will work to show one category on a page with these features:
- Honor the theme’s appearance.
- Provide pagination.
- Honor sticky posts.
- Allow easy assignment of the category to the page.
- Use one template for many different category pages.
I would like for someone to try it out and provide feedback. Here are the steps to implement:
- Make a copy of index.php and name it onecategory.php
- Paste these lines at the start of the file:
<?php /* Template Name: onecategory */ ?>
- Paste these lines at the end of the file:
<?php function bmc_show_category ($nav = true) { <?php } // End of Function bmc_show_category ?>
- Cut all lines between
<div id="content">
and the corresponding</div>
and paste between the last two lines. - Copy the code below and paste between the content div where you just cut the lines for the function. Then save the file in your theme folder.
<div class="post";?><h2><?php the_title(); ?></h2></div> <?php $this_cat=get_post_meta($post->ID,'category-to-show',TRUE); if ($this_cat != "") { $paged = get_query_var('paged'); $stickies = get_option('sticky_posts'); $args = array( 'category_name' => $this_cat, 'paged' => $paged,); if (!$paged) { // Only show stickies on first page $args['post__in'] = $stickies; query_posts($args); $found_posts = 0; // Use this to not show warning if only stickies if (have_posts()) {++$found_posts; bmc_show_category($nav=false);}; unset($args['post__in']); } $args['post__not_in'] = $stickies; query_posts($args); if (have_posts) { bmc_show_category($nav=true); } elseif (!$found_posts) { echo "<h4></h4>There are no posts in this category $this_cat<h4></h4>"; } } else { echo "<h4></h4>This page is missing the Custom Field 'category-to-show'". "whose value is the name of the category to be shown." . "<br />Please have the page corrected."; } ?>
- Create a page to hold your category.
- Create a custom field ‘category-to-show’ with the value of the category name.
- Assign the onecategory template to the page.
That should do it!
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘HOWTO: One category on a page redux’ is closed to new replies.