Solved

post loop excluding category

Posted on 2011-09-14
5
359 Views
Last Modified: 2012-08-13
In wordpress how can I do a post loop, but exclude a category called "blog"?

i.e. Change the below...

<?php while ( have_posts() ) : the_post(); ?>

xxx

<?php endwhile; ?>
0
Comment
Question by:petewinter
5 Comments
 
LVL 4

Assisted Solution

by:h4hardy
h4hardy earned 150 total points
ID: 36535476
is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) )

    Returns true when the category of posts being displayed is either term_ID 9, or slug "blue-cheese", or name "Stinky Cheeses".

in_category( '5' )

    Returns true if the current post is in the specified category id. read more in_category( array( 1,2,3 ) )
    Returns true if the current post is in either category 1, 2, or 3.

! in_category( array( 4,5,6 ) )

    Returns true if the current post is NOT in either category 4, 5, or 6. Note the ! at the beginning.
0
 
LVL 3

Expert Comment

by:garykristen
ID: 36535685
Hi petewinter,

you can use like this

<?php wp_list_categories('exclude=4,7&title_li='); ?>
0
 

Author Comment

by:petewinter
ID: 36535782
h4hardy - Thanks for the reply, but my knowledge of php / wordpress is not great.

How can I combine with the below...

<?php while ( have_posts() ) : the_post(); ?>

<?php get_template_part( 'content', get_post_format() ); ?>

<?php endwhile; ?>

I want to show all posts except in the category with the slug name "blogs".

How do I do that?
0
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 350 total points
ID: 36539271
From: http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category

To exclude a category, just add query_posts above the loop and put the category ID to be excluded with a -

 <?php query_posts($query_string . '&cat=-3,-8'); ?>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

 <div class="post">
 
 <!-- Display the Title as a link to the Post's permalink. -->
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

 <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
 <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
 
  <div class="entry">
    <?php the_content(); ?>
  </div>

  <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
 </div> <!-- closes the first div box -->

 <?php endwhile; else: ?>
 <p>Sorry, no posts matched your criteria.</p>
 <?php endif; ?>

Open in new window

0
 

Author Closing Comment

by:petewinter
ID: 36541866
Many thanks
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question