• Login
  • Become a blogger
  • Exclude categories from wordpress frontpage

    Posted by thinkdj on November 12th, 2007
    19 Comments | 15,799 pageviews

    I stumbled upon this when Blogulate.com’s first post was made. I didnt want the Blog Reviews to appear on the homepage.

    This is actually very simple. All you have to do is modify some PHP code.

    First, goto Manage > Categories and get the ID of the Category you wish to exclude. Then, go to WP admin and selet Presentation > Theme Editor ..

    For your current theme, edit the loop where the posts are displayed. (It varies according to the theme… It was theloop.php for my K2 theme. )

    Here is the sample from K2 Theme’s post display loop.

    <?php /* Start the loop */ while ( have_posts() )

    {

    the_post();

    ?>

    MODIFIED CODE :

    <?php /* Start the loop */ while ( have_posts() )

    {

    the_post();

    if (is_home()) if (in_category(’14′)) continue;
    if (is_home()) if (in_category(’47′)) continue;

    ?>

    The modified code doesnt show posts that belong to Categories 14 and 47 in homepage.

    19 Responses »

    1. Gangster95 says:

      It would be condemnable on consequentialist grounds, if there are feasible alternative arrangements that would bring about more mixing of people, a better fit of complementary types in productive schemes of cooperation, and better outcomes for people according to the right standard for assessing outcomes. ,

    2. Arnold96 says:

      Organised by Management Forum. ,

    3. joe says:

      why not just use these plugins, find them in google by typing: “wordpress exclude category plugin”

    4. Matias says:

      Excelent… simple and efective. Thank you thinkdj

    5. To exclude a category from the loop on the front page, find this code:

      and change it to this:

      This will exclude posts in category number four from being displayed on the front page.

    6. Peter says:

      Hey,

      I don’t know if it is still usefull for you or that it is just a feature of the TMA theme, but with the code beneath the two categories Asides and Featured are removed from the resulting list.


      get_var("SELECT term_ID FROM $wpdb->terms WHERE name='Asides'"); ?>

      get_var("SELECT term_ID FROM $wpdb->terms WHERE name='Featured'"); ?>

      <?php $the_query = new WP_Query('cat=-' .$catid. ',-' .$catid2. '&showposts=1&orderby=post_date&order=desc');

    7. Fendy Ahmad says:

      Although it’d work skipping the unwanted category but it will not change the number of “Previous Post” pagination at the bottom.

      Have any idea how to modify that?

    8. Rachel Buddeberg says:

      Thanks, Rafael! I’ll try that…

    9. Trying again:

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

      <?php while (have_posts()) : the_post(); ? >
      <?php if (is_home()) {
      $categories = get_the_category();
      if (count($categories) == 1 && $categories[0]->cat_ID == ‘12′) continue;
      } ?>

    10. Rachel, this worked for me:

      cat_ID == ‘12′) continue;
      } ?>

      The result is that any posts that have only category 12 will be ignored.

    11. pixie says:

      Do you know how this would work for Wordpress 2.5.1, in my K2 theloop.php I have

      and so have made it

      to no avail….

      I have tried countless plug-ins, hacks and code but yours is the only site actually implementing it, I’d be very grateful for any help.

      Cheers, Emma :-)

    12. Jasmine says:

      Thanks for the tip. I was looking for a plugin to do the job, but now seems it’s easier to just hack the index.php file. Quicker and cleaner. :)

    13. stim says:

      Hi,

      I’ve tried every which way and it won’t work for me.

      Here’s what I did:

    14. Rachel says:

      Hi, thinkdj,

      I think I might’ve found a solution for #2:

      http://blue-anvil.com/archives/create-a-wordpress-recent-posts-widget

      :-)

      Thanks!

      Rachel

    15. Rachel says:

      Hi, thinkdj,

      I have a couple of questions:
      1) Do you know if there’s a way to only include posts if they have categories in addition to the excluded one(s)?
      2) How do I exclude the posts from my “Recent Posts” list on the sidebar? (I am using the My_Link widget for that, see this page for more info on that).

      Thanks so much!

      Rachel

    16. thinkdj says:

      U’re welcome Rachel :)

    17. Rachel says:

      Thank you so much, thinkdj! This code actually works in WP 2.3.2, whereas all the other tricks I’ve tried – those published in the codex – do not. They reordered all the posts on my home page, displaying the oldest first – not exactly what I wanted…

      Thanks!

      Rachel

    Trackbacks/Pingbacks

    1. [...] Exclude categories from wordpress frontpage [...]

    2. [...] to Blogulate for the tip. RSS Trackback URL Jason | February 16, 2008 (4:01 [...]

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    More from "PHP"

    Find what you are looking for

    Recently added

    Categories

    Blogulate

    Keeping you updated of the latest fads and developments of the industry via amalgamated web content.

    Join us

    Post articles on blogulate and monetize them. Learn more »

    Stay updated

    Subscribe now and stay aware of the latest trends and happenings on the www. Get notified via email when we update.

    Enter your email address:

    Blogulate is based on Magnus premium theme by WizeDesigns
    © 2009 Blogulate. Some rights reserved.