Exclude Categories from Feed, Archives, Search, and Home page in WordPress

Thursday, January 14th, 2010

We often find ourselves looking for the best and easiest way to exclude categories or pages from the navigation menu, the WordPress loop, the category list, the feed, the search, and/or the archive. It’s so easy to forget one of these elements and accidentally let that category slip into the blogstream. We actually wrote about a few WordPress plugins that exclude categories two years ago, but it’s definitely time to revisit this issue.

Recently, Miriam found an AMAZING plugin, Simply Exclude by Paul Menard, which is basically excluding heaven… if there is such a thing.

Exclude categories WordPress

Advanced Category Excluder vs. Simply Exclude

You may have come across the Advanced Category Excluder plugin, and wonder why we didn’t use that one. Here’s why:

Advanced Category Excluder (ACE) vs.  Simply Exclude:

  • Advanced Category Excluder conflicts with the Exclude Pages plugin (described below) which excludes pages  from the nav bar. Simple Exclude does not. When I activated it ACE all the pages that had been excluded with the Exclude Pages plugin appeared on the nav bar.
  • ACE also offers you to exclude Pages, which is so annoying since you have to go through a long list of all the pages on your site to see which you want to exclude, and they appear in long lists without any indication of parent pages and sub-pages. Also, there’s no easy way to exclude pages when you are adding or editing a page.
  • With ACE you have to first publish a post in a category, and only then will that category appear in the list of categories for exclusion. This means that if you want a category to not appear in the RSS feed etc., your first post in that category has to.

How to manually exclude categories in the theme files

And here are a few tips on how to exclude categories manually from the feed and the loop if you still prefer some good ol’ fashioned manual labor.

How to Manually Exclude a Category from a RSS feed

Place the following code in your functions.php file replacing 12 with your category and commas for additional categories. Tip from Web Kreation.

function myFeedExcluder($query) {

if ($query->is_feed) {

$query->set(‘cat’,'-12′);

}

return $query;

}

add_filter(‘pre_get_posts’,'myFeedExcluder’);

How to Manually Exclude a Category in Feedburner

Under Edit Feed Details add a ?cat=-93 or whatever the category ID number is so it comes out like:  http://domain.com/feed?cat=-93

How to Manually Exclude a Category from the Loop

Add this code BEFORE the loop and change 52 to whatever category you want to exclude

<?php query_posts(’showposts=2&cat=-52′); ?>

Bonus: Easily Exclude Pages

Since we’re discussing excluding stuff from your WordPress blog, here’s a related plugin that we love called Exclude Pages by Simon Wheatley. It’s easy to use and great to give clients for excluding pages from their navigation menu. Exclude Pages adds a checkbox on the edit Page page that users can click to select to exclude the Page they are working on from any place that lists all Pages, like the navigation bar.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Use WP-Sticky WordPress plugin to easily create feature or “sticky” posts

Tuesday, February 19th, 2008

The WP-Sticky WordPress plugin allows you to easily choose to “stick” certain posts at the top of your blog. This can also be done via code, as I explained in a post a few months ago where I wrote about some code that you can add to your index.php template file that will keep the latest post in your feature category at the top of your homepage. However, both the plugin and the code have advantages and disadvantages.

The coding method has the following advantages:

  1. By putting all your best posts in one category, you can easily highlight these posts in a number of places, like in the sidebar or on its own category page, and of course as a feature article.
  2. You can give the feature post its own styling, like a different background color, which helps it stand out even more.
  3. You don’t need a plugin.

However, this method also has the following disadvantages:

  1. You need to create a unique category for your feature posts. Sometimes you just want to highlight certain posts without putting them in their own category. Also, your features posts will end up needing two instead of one: “Feature,” and another category that really describes the type of article.
  2. With the code I gave, the feature post will appear at the top of every main page on your blog. What I mean is that if you click on “previous entries” or something like that at the bottom of the first page of your blog, the feature post will still appear at the top on the second page. This can be confusing to readers who will wonder if they have actually browsed to a new page.
  3. You need to hack your template file, which isn’t for everybody.

So I decided to try out another option, and settled on WP-Sticky by Lester Chan. The explanation on the plugin page and in the readme file is not clear, but I eventually came across a better explanation here.

After installing the plugin, it creates an additional box on the Write Post page at the bottom of the right-hand sidebar called Post Sticky Status. There, you have three options: Announcement, Sticky, and Normal.

WP-Sticky WordPress plugin

If you select Announcement, that post will stay at the top of your blog forever, or at least until you define another post as an Announcement or change the post’s status to Normal. If you select Sticky, the post will stay at the top of your blog for the duration of the day on which you posted it, after which it will return to its place in the chronological order. This is good if you post multiple posts on one day, but want to highlight one of them by sticking it to the top of your blog.

Update February 20, 2008: Announcements can be styled! Here’s how:

You can style your feature posts as follows:

  1. Find the line in your index.php file that says the following:
    <div class="post">
    Note that it may say something a bit different, like it may have a different class name. Basically, it’s the code that appears before all the content related template tags in the loop, somewhere around line 13.
  2. Replace that line with the following:
    <div class="post"<?php if (is_announcement()) {echo " id="announcement"";} ?>>
    That code tells WordPress that if the post is an Announcement post, it should have a class of “post” and an id of “announcement.” This way the post keeps the styling of “post” and gets additional styling of “announcement.
  3. Add the following style to your style.css file:
    #announcement {}
    Put in the squiggly brackets whatever styles you want. Make the title a different size, color, give it a different background color, even add images like a “Feature” badge.

The disadvantages to the WP-Sticky WordPress plugin are:

  1. You cannot give Announcement or Sticky posts their own styling.
  2. You need to use a plugin.

WP-Sticky Plugin

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Display WordPress categories in a horizontal dropdown menu

Thursday, January 17th, 2008

Anthology of Ideas shares his method for creating a horizontal drop-down menu for categories, involving the use of CSS and Javascript.

Displaying WordPress categories in a horizontal dropdown menu

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Apply different themes to different categories with Wordpress Themed Categories Plugin

Friday, January 11th, 2008

I’ve never come across a situation where I would need this feature, but I’d like to have this on record in case it ever comes up. Mike has developed a WordPress plugin, Themed Categories that allows the user to select different uploaded themes for different categories. According to the comments, his version doesn’t work with WordPress version 2.3, so someone named Kalle made the necessary changes and the modified plugin can be downloaded here (direct link to zip download).

Wordpress Themed Categories Plugin
WordPress Themed Categories Plugin for 2.3 (direct link to zip download)

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Exclude categories from certain pages with WordPress plugins

Wednesday, January 9th, 2008

There are many situations where you want to make sure that certain categories only appear on some pages, and not on others. Here are a few WordPress plugins that can help you do this without having to hack your template files:

Front Page Excluded Categories WordPress plugin – this plugin enables you to exclude posts from certain categories from your front page. This is possible by hard coding your template files as well, but for those who want to give others the ability to do this easily, or who just don’t want to mess with their template files, this is a good solution.

Advanced Category Excluder WordPress plugin – this plugin gives you more flexibility over where categories appear. Here, you have access to modifying categories for five different pages on your blog: archive, home, rss posts, rss comments and search. It currently does not work for tag pages.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Display posts from specific categories on a Page

Thursday, November 8th, 2007

You may want to display posts from a specific category on the homepage or other pages of your site. For example, you might want to have a box that displays the latest three posts from your News category, or have links to your latest podcasts.

In a previous post, I gave the code that would display the latest posts from a specific category in the sidebar. However, that code will not work in the main content part of the page. In order to display posts from a specific category in the main content section of a page, you need to use a variation on the WordPress Loop, which is as follows:
<?php query_posts('category_name=special_cat&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do special_cat stuff... -->
<?php endwhile;?>


To use this code, change the category name to whatever the name is of the relevant category, and change the number of posts displayed from 10 to whatever number you want. Where it says <!-- Do special_cat stuff... -->, put in all the code you usually use to display the title, date, category, etc. – whatever you need.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Creating a feature post or sticky post in a WordPress blog

Monday, October 29th, 2007

To get a post from a specific category to appear at the top of the page above the rest of the posts, and to only change when a new post from that particular category is added, use the following code:

<?php /*?><?php $my_query = new WP_Query('category_name=featured&showposts=1');

while ($my_query->have_posts()) : $my_query->the_post();

$do_not_duplicate = $post->ID;?>

<div class="featureItem">

<h1>Feature: <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
<p><?php the_time('F j, Y'); ?> • By <?php the_author_firstname(); ?> <?php the_author_lastname(); ?><br />Tags: <?php STP_PostTags(); ?> | <?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?></p>
<?php the_content('Read more»'); ?>
</p>

<!--

<?php trackback_rdf(); ?>

-->

</div>

<?php endwhile; ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate )

continue;?><?php */?>

Obviously, many things above can be changed, like how the titles, categories, tags, etc. are displayed.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Display a selected number of posts from one category in the sidebar

Monday, October 29th, 2007

<?php$postlist = get_posts('category=6&numberposts=5');
foreach ($postlist as $post) :? ><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php endforeach; ?>

Use this in the sidebar to display a defined number of posts from a specified category. Where it says “category=6″ replace the 6 with the relevant category ID number, and where it says “numberposts=5″ replace the 5 with the number of posts that you want to appear.

Update Nov. 7, 2007:

The above code snippet can also be used to display a page in the sidebar. This is is handy if you want to create an editable chunk of text in the sidebar without using widgets. So, for example, if you want to show an excerpt of your About page in your sidebar, use the following code:

<?php$postlist = get_posts('id=54');
foreach ($postlist as $post) :? ><?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">read more</a>

Where it says id=54, replace 54 with the id number of the page you want to display.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Giving different styles to different WordPress categories

Friday, September 7th, 2007

WordPress allows you to give different styles to different categories in a range of ways. You can style a category page: this means that every category page can have its own unique style. For example, let’s say you have a site about vegetables, where every vegetable is a category. You can style the Tomato category page so that the headers are all in red, the Carrot category page so that the headers are all orange, etc.

You can also make sure that when you visit a single post page in a specific category, that page also has a unique style. So if someone clicks on your post about Delicious Tomato Sauce, the headers on that page can be styled red, while the single post page for the article on Creamy Carrot Soup will have orange headers.

And finally, I just found out that you can style posts on the index, home page, search results page, or any page with posts from multiple categories, with their own style depending on their category.

Let’s go through each styling method one-by-one:

Styling Category pages

  1. Find out what the id number is of the category you want to style. To do so, go to Manage > Categories, find the category in question, and look at the left-hand column. Let’s say that your Tomato category has an id of 5.
  2. Create a file called category-x.php, where x is the id number of the category. In the case of the Tomato category, your file will be called category-5.php. The best way to do this is to copy you index.php file, and rename it category-5.php.
  3. Now modify your file at will. For example, if you want all h2 text to be red, find where it says <h2> in your file, and replace it with something like <h2 class="tomato-red">. Then, in your style.css file, create a class called tomato-red, and give it the right styles. In this case, you would add the following to your style sheet:
    .tomato-red {
    color: #FF0000;
    }
  4. The method described above in Step 3 is ok if you want to customize the styles for one or two categories. But if you have more than that, it can become annoying to manage a zillion category-x.php files. Ryan Hellyer, a devoted WordPressGargage reader and most frequent commenter, gave the following more efficient way of doing this. He said to create one category.php file, and put the following code in it:
    <h2
    <?php
    if (is_category('tomato'))
    {echo 'class="tomato-red"';}
    if (is_category('cucumber'))
    {echo 'class="cucumber-green"';}
    ?>

    Etc. Brilliant!

Styling single post pages for different categories

See my previous post titled “Displaying single post pages differently in specific categories” for detailed instructions on styling single post pages for different categories. It’s all there.

Giving posts from different categories different styles on a multi-category page

I love how there’s always something new to learn about WordPress! Abhijit Nadgouda has written a post on Lorelle’s blog explaining how you can apply unique styles to posts in different categories. He explains how you can use the category slugs to create styles that can be dynamically called to give different styles to posts from certain categories.

Why is this useful? Let’s take our vegetable site example. By giving each category its own style, a reader on the home page could easily identify all posts about tomatoes by picking out the posts with red headers, and carrot posts would have orange headers. This could also create a very colorful and eye-catching page.

Another way to use is if you have a category of feature articles. You want these articles to stand out in some way, either by giving them some kind of badge, or a background color, for example. By using this method, you can ensure that your feature articles are always more prominent, and don’t get lost in the crowd.

So there you have it – three ways to make sure your posts in different categories stand out all across your site. Hurray for WordPress!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Displaying single post pages differently in specific categories

Sunday, July 15th, 2007

You may have certain categories in your WordPress blog that you want to appear differently than other categories. For example, what if you have a category called “Tomatoes” and a category called “Cucumbers,” and you want the titles of every post in the Tomatoes category to be red, and the titles in the Cucumbers category to be green?

There are a few plugins that can help you with this, but they didn’t work so smoothly. The best solution I have found is one provided by Lorelle on her Taking Your Camera on the Road blog. In her post titled “Different Category – Different Look: Creating Multiple Single Posts Looks for Different Categories,” she gives the following directions:

We began by making two back up copies of the single.php page called single1.php and single.2.php.

Inside of the original single.php, delete everything and replace it with this:

<?php
$post = $wp_query->post;
if ( in_category('9') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>

In the most simple terms, the PHP code issues a query that says “Check the post. If the post is in category ID number 9, display single2.php. If not in category ID number 9, display single1.php.”

In the in_category(), we set the category ID number to 9, the one that holds all of my web page design articles and experiments.

So basically, your main single.php file becomes a conditional file that tells WordPress which template file to use in certain categories. Lorelle also gives the following code you can put in the single.php file that will allow you to create multiple styles for multiple categories:

<?php
$post = $wp_query->post;
if ( in_category('9') ) {
include(TEMPLATEPATH . '/single9.php');
elseif ( in_category('12') ) {
include(TEMPLATEPATH . '/single12.php');
elseif ( in_category('42') ) {
include(TEMPLATEPATH . '/single42.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>

Now you have to get the styles you want to apply to the correct category. Do the following for every category you want to have a certain style for:

  1. Create a new style sheet and call it style-9.css (9 is the category ID, but your category ID will likely differ. You can still call it style-9.css, but this is confusing!) If the changes you are making are minor, like changing certain colors or fonts, you should probably just copy style.css and rename it to style-9.css. In that style sheet, create the styles that you want, so for example if you want all h2 items to be red, make those changes in your new style sheet.
  2. Style sheets are called in your header.php file. Which means that we need to create a header file that will call your new style sheet. So copy your header.php and rename the copy header-9.php (or whatever you want).
  3. In your new header-9.php file, find the lines that look something like this:
    <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/style.css" type="text/css" media="screen" />
    and change where it says style.css to style-9.css.
  4. Then, in your single2.php file, replace the following line (which is probably the first line):
    <?php get_header(); ?>
    with this
    <?php include ('header-hebrew.php'); ?>

Do this for every category. If you have a lot of categories you want to appear with different styles, there’s probably some conditional PHP that you can use. Being of little PHP knowledge, I don’t know what this code would be, but the above works quite nicely for people like me with little-to-no knowledge of PHP and only one or two categories that need modifying.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Premium News Themes