Solved

post loop excluding category

Posted on 2011-09-14
5
352 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The viewer will learn how to dynamically set the form action using jQuery.

867 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now