Solved

post loop excluding category

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help in good tutorials for PHP, HTML and CSS 6 40
Split wordpress loop 35 56
Log in through ID 5 17
Moving to a new hosting provider 8 26
In Part I (http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_8410-Getting-Started-In-WordPress-Part-I.html), I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. 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 : Go t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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