• Resolved vtxyzzy

    (@vtxyzzy)


    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:

    1. Make a copy of index.php and name it onecategory.php
    2. Paste these lines at the start of the file:
      <?php
      /*
      Template Name: onecategory
      */
      ?>
    3. Paste these lines at the end of the file:
      <?php function bmc_show_category ($nav = true) {
      <?php } // End of Function bmc_show_category ?>
    4. Cut all lines between <div id="content"> and the corresponding </div> and paste between the last two lines.
    5. 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.";
         } ?>
    6. Create a page to hold your category.
    7. Create a custom field ‘category-to-show’ with the value of the category name.
    8. Assign the onecategory template to the page.

    That should do it!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Sounds good, but you might consider posting the whole page template in a pastebin to make it easier for people to try.

    Thanks for the neat idea.

    So basically this shows stickies on the first page, and regular posts on further pages.

    Is there a particular reason you put the function right in the template and why the empty heading tags? ..

    Also you’re missing a closing php tag on this line btw.. ??

    <?php function bmc_show_category ($nav = true) {

    ..assuming you’re expecting HTML to follow..

    Thread Starter vtxyzzy

    (@vtxyzzy)

    @michaelh Thanks for the review. I am not sure there is a ‘whole template’ because this starts with a copy of the user’s index.php and modifies it. Can you explain what I should post?

    @t31os_ Thank you also for the review. I only put the function in the template because the user would already be editing the template and it kept everything in one place. No overriding reason to do so.

    Thanks for spotting the missing tag. I will add it. This is probably going to be one of the weakest points of the method because there is so much variability in themes that will be hard to give explicit directions about where php tags are needed. But, having the tag will handle most cases, I think.

    Had a feeling that’s why you placed the function there.. ??

    Empty heading tags?

    Thread Starter vtxyzzy

    (@vtxyzzy)

    Good eye! Left over from testing! Should be removed or filled in.

    vtxyzzy – suggesting taking the wp-content/themes/default/index.php and using that as your ‘example’.

    Thread Starter vtxyzzy

    (@vtxyzzy)

    Guys, I just found a bug = duplicates posts when no stickies. I will work on a new version and post under a new topic.

    Thanks for your help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘HOWTO: One category on a page redux’ is closed to new replies.