Ask your WordPress questions! Pay money and get answers fast! (more info)

Exclude Categories from Archives list

  • SOLVED

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>

Answers (2)

2010-03-23

Sidd answers:

Hi Web Designer,
Simply<strong> exclude the categories</strong> 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');


WP Answers comments:

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>


Sidd comments:

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


WP Answers comments:

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.



<strong>Here's the content in the main content area</strong>

<?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><strong><?php the_time(get_option('date_format')); ?></strong><br /><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>




<strong>Here's the code in the sidebar that generates the archive drop down select box.</strong>

<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>


Sidd comments:

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


WP Answers comments:

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?


Sidd comments:

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"


WP Answers comments:

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.


Sidd comments:

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


WP Answers comments:

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


<strong>(heres the code from archive.php)</strong>

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


Sidd comments:

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


Sidd comments:

You may also use Simply Exclude plugin by Paul Menard.


WP Answers comments:

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?


Sidd comments:

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


Sidd comments:

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 !


WP Answers comments:

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


WP Answers comments:

<strong>Got it to work</strong>

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



2010-03-23

Edouard Duplessis answers:

use the wp_dropdown_categories( $args );

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

[[LINK href="http://codex.wordpress.org/Template_Tags/wp_dropdown_categories"]]http://codex.wordpress.org/Template_Tags/wp_dropdown_categories[[/LINK]]


WP Answers comments:

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)


Edouard Duplessis comments:

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


WP Answers comments:

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


Edouard Duplessis comments:

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