[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

post loop excluding category

Posted on 2011-09-14
5
Medium Priority
?
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Assisted Solution

by:h4hardy
h4hardy earned 600 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 1400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
This article discusses how to implement server side field validation and display customized error messages to the client.
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 Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
Suggested Courses

650 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