logo

$5
Exclude Categories from Archives list

Hi,

I am trying to include only one post category in my archives list. I can't use any plugins for this as it will be part of a theme, and needs to be 'built in'.

I am using the code below to display the archives list in a dropdown select box. Everything works fine as-is, but it archives posts from all categories, rather than just the one that I want.

The slug of the category that I want it to only include is pressreleases and the category ID is 14.

<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo attribute_escape(__('Select Month')); ?></option>
<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> </select>

WP Answers | 03/23/10 at 3:34pm | Edit


(2) Possible Answers Submitted...

  • avatar
    Last edited:
    03/24/10
    2:59pm
    Sidd says:

    Hi Web Designer,
    Simply exclude the categories by inserting this exclude=1,2,3 etc code in the wp_get_archives function.
    For example I have excluded all except cat 14 (assuming that there were a total of 1-15 categories here)
    <?php wp_get_archives('type=monthly&exclude=1,2,3,4,5,6,7,8,9,10,11,12,13,15&format=option&show_post_count=1');

    Previous versions of this answer: 03/23/10 at 4:03pm

    • 03/23/10 4:24pm

      WP Answers says:

      Hi,

      Thank you for your response. It still doesn't appear to be working. Here is the code I am using. The categories go from 1-16. Please let me know if I have done something incorrectly. Thanks.


      <select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>
      <option value=""><?php echo attribute_escape(__('Select Month')); ?></option>
      <?php wp_get_archives('type=monthly&exclude=1,2,3,4,5,6,7,8,9,10,11,12,13,15,16&format=option&show_post_count=1'); ?> </select>

    • 03/23/10 10:01pm

      Sidd says:

      Place the code below just after <?php while (have_posts()) : the_post(); ?>
      This code is working for me already :)


      <?php
      if ( $wp_query->is_archive ){
      $wp_query->query_vars["cat"] = 14; // only the category that you want to inlcude
      $wp_query->query_vars["posts_per_page"] = 10; // for number of posts you want
      $wp_query->get_posts();}
      ?>

    • 03/23/10 10:34pm

      WP Answers says:

      Hi,

      Thank you for the code. I think I may be doing something wrong, or perhaps I put the code in the wrong place. Please see this staging site as a reference. You will notice that the archives dropdown on the right includes 'March 2010 (36)'. Posts in there are from a bunch of random categories that I do not want included in the archives list. http://www.vfes.net/wordpress/news-events/archived-news-events

      I appreciate your feedback.



      Here's the content in the main content area


      <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
      <?php
      if ( $wp_query->is_archive ){
      $wp_query->query_vars["cat"] = 14; // only the category that you want to inlcude
      $wp_query->query_vars["posts_per_page"] = 10; // for number of posts you want
      $wp_query->get_posts();}
      ?>
      <?php the_content(); ?>
      <?php endwhile; endif; ?>

      <ul class="archivedlist">
      <?php
      global $post;
      $myposts = get_posts('numberposts=1000&category_name=pressreleases');
      foreach($myposts as $post) :
      setup_postdata($post);
      ?>
      <li><?php the_time(get_option('date_format')); ?><br /><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php endforeach; ?>
      </ul>




      Here's the code in the sidebar that generates the archive drop down select box.

      <select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>
      <option value=""><?php echo attribute_escape(__('Select Month')); ?></option>
      <?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> </select>

    • 03/23/10 10:51pm

      Sidd says:

      And this you placed in that file which is working as your Archive.php file ?

    • 03/23/10 11:04pm

      WP Answers says:

      No, the code I pasted was from the custom page template I created that this page uses:
      http://www.vfes.net/wordpress/news-events/archived-news-events

      Should I put your code in the archives.php file?

      Will that hide the 'March 2010 (36)' from the dropdown list that's on the custom page template?

    • 03/23/10 11:18pm

      Sidd says:

      Yes place the code in the file called archive.php. do not put it in archives.php [ notice the difference in the files archive and archives].
      Then simply use the Archives Widget and un check "Show post counts"

    • 03/23/10 11:23pm

      WP Answers says:

      Ok thanks. I'm going to have to widgetize that sidebar. I will try to implement your solution tonight, but if not I will do it first thing in the AM. Thank you very much for your help - I will let you know how it turns out.

    • 03/23/10 11:27pm

      Sidd says:

      Great !
      It is working fully well in my test site.
      All the best.

    • 03/24/10 12:12pm

      WP Answers says:

      Hi,

      Its getting closer but its not quite there. I added the code to my archive.php file and widgetized the sidebar and included the archives widget. In the dropdown list, 'March 2010 (36)' still shows up, but when you click on it, only 1 post shows instead of all 36. I need the 'March 2010 (36)' to not show up at all since all the posts within that month are from other categories that I do not want listed on this page.

      I'm looking into the plugin route right now. Any more advice you might have is greatly appreciated.

      Thanks


      (heres the code from archive.php)


      <?php get_header(); ?>

      <?php if (have_posts()) : ?>

      <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
      <?php /* If this is a category archive */ if (is_category()) { ?>
      <h2>Archive for the &#8216;<?php single_cat_title(); ?>&#8217; Category</h2>
      <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
      <h2>Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h2>
      <?php /* If this is a daily archive */ } elseif (is_day()) { ?>
      <h2>Archive for <?php the_time('F jS, Y'); ?></h2>
      <?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
      <h2>Archive for <?php the_time('F, Y'); ?></h2>
      <?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
      <h2>Archive for <?php the_time('Y'); ?></h2>
      <?php /* If this is an author archive */ } elseif (is_author()) { ?>
      <h2>Author Archive</h2>
      <?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
      <h2>Blog Archives</h2>
      <?php } ?>


      <?php next_posts_link('&laquo; Older Entries') ?> | <?php previous_posts_link('Newer Entries &raquo;') ?>

      <?php while (have_posts()) : the_post(); ?>
      <?php
      if ( $wp_query->is_archive ){
      $wp_query->query_vars["cat"] = 14; // only the category that you want to inlcude
      $wp_query->query_vars["posts_per_page"] = 10; // for number of posts you want
      $wp_query->get_posts();}
      ?>

      <div <?php post_class() ?>>
      <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
      <p><?php the_time('l, F jS, Y') ?></p>
      <?php the_content() ?>
      <p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
      </div>

      <?php endwhile; ?>

      <?php if (next_posts_link() || previous_posts_link()): ?>
      <?php next_posts_link('&laquo; Older Entries') ?> | <?php previous_posts_link('Newer Entries &raquo;') ?>
      <?php endif ?>

      <?php else :

      if ( is_category() ) { // If this is a category archive
      printf("<h2>Sorry, but there aren't any posts in the %s category yet.</h2>", single_cat_title('',false));
      } else if ( is_date() ) { // If this is a date archive
      echo("<h2>Sorry, but there aren't any posts with this date.</h2>");
      } else if ( is_author() ) { // If this is a category archive
      $userdata = get_userdatabylogin(get_query_var('author_name'));
      printf("<h2>Sorry, but there aren't any posts by %s yet.</h2>", $userdata->display_name);
      } else {
      echo("<h2>No posts found.</h2>");
      }
      get_search_form();

      endif;
      ?>

      <?php get_sidebar(); ?>
      <?php get_footer(); ?>

    • 03/24/10 12:59pm

      Sidd says:

      Yes you are almost there now.
      In the Archives widget, make sure you uncheck " show post count "
      That's all.

    • 03/24/10 1:05pm

      Sidd says:

      You may also use Simply Exclude plugin by Paul Menard.

    • 03/24/10 1:24pm

      WP Answers says:

      It's still not working. Sorry to be such a pain.

      Please check out the staging page:
      http://www.vfes.net/wordpress/news-events/archived-news-events

      March 2010 still shows up in the dropdown, even though every post within that month is not in category 14.

      I tried the Simply Exclude widget without any luck. It still listed March 2010 in the list, but when you clicked it gave you the 404 Page not Found error.

      Is what I'm trying to do impossible with Wordpress?

    • 03/24/10 1:30pm

      Sidd says:

      When you use the Simply Exclude Plugin then leave the archive.php file untouched

    • 03/24/10 1:41pm

      Sidd says:

      OK.
      Check my site.
      http://www.siddatwork.com/projects/test/

      I only want to display category "cc" in my archives. And in archives it shows only "cc" category posts !

    • 03/24/10 2:28pm

      WP Answers says:

      I've done everything that you said, but still have the same result. I must be doing something wrong?

    • 03/24/10 3:02pm

      WP Answers says:

      Got it to work

      Had to change the post date of the posts that were in categories that I didn't want to display to be in the same month of posts that were in the category i DID want to display. This got rid of the useless month from the archives dropdown box.

      Then, I added the code below to archive.php to filter out the random posts I didn't want.

      Thanks so much.


      <?php
      if ( $wp_query->is_archive ){
      $wp_query->query_vars["cat"] = 14; // only the category that you want to inlcude
      $wp_query->query_vars["posts_per_page"] = 10; // for number of posts you want
      $wp_query->get_posts();}
      ?>



  • avatar
    Last edited:
    03/23/10
    4:28pm
    Edouard Duplessis says:

    use the wp_dropdown_categories( $args );

    it's make a dropdown list with all the arguments you need

    http://codex.wordpress.org/Template_Tags/wp_dropdown_categories

    • 03/23/10 4:35pm

      WP Answers says:

      Thanks, but I am trying to display the Post Archives. Does the function you mentioned display archives or categories?

      Currently, the dropdown I'm using displays this:

      March 2010 36)
      February 2010 (2)
      January 2010 (2)
      December 2009 (2)
      November 2009 (1)
      September 2009 (3)
      July 2009 (1)
      December 2008 (1)
      August 2008 (1)

    • 03/23/10 4:41pm

      Edouard Duplessis says:

      ok so you want to display a dropdown list of the category x in a archives style

    • 03/23/10 4:47pm

      WP Answers says:

      Yes, exactly. The category ID is 14 and the slug is 'pressreleases'.

    • 03/23/10 5:06pm

      Edouard Duplessis says:

      i don't see a built in function for that but I gonna check

This question has expired.





Current status of this question: Completed