Solved

post loop excluding category

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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 update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

708 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

17 Experts available now in Live!

Get 1:1 Help Now