StudioPress Community Forums
  StudioPress Community Forums > Forums > General Discussion
For help and support, access to your downloads, or to manage your account please log into My StudioPress.

These forums have been set to read-only so you can browse the existing topics for any questions you may have.

For general discussion on WordPress, CSS and design (NOT for support) visit the new Community Forums.
 
 
Thread Tools Display Modes
  #1  
Old 10-23-2012, 05:47 PM
ckarasiewicz ckarasiewicz is offline
Registered User
Pro Plus Member
 
Join Date: Jun 2011
Posts: 75
Default How to exclude a category from page

I have a Wordpress custom page template created listing all of my blog posts. What I am trying to do is display a blog page, which lists all posts with 10 per page, with navigation, and exclude category 12, category 20, and category 50 from the list.

For some reason, I cannot get it to exclude these categories. I have even tried to exclude just one category and it still won't.

I am including the code that I am using for this page. If there is a better way to do this, I am open to it. What I am doing is showing the page title, the post title, an excerpt, and a read more link.

Thanks for your help.

Code:
PHP Code:
<?php  /*Template Name:Blog*/ ?>

<?php get_header(); ?>
<!--HEADER END HERE-->

<!--CONTENT STARTS HERE-->
<div class="modern-content-blog">
<div class="left">
<h2> <?php the_title(); ?> </h2>

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1query_posts('cat=-12,'); 
$args = array( 'post_type' => 'post''posts_per_page' => 10'paged' => $paged );
$wp_query = new WP_Query($args);
while ( 
have_posts() ) : the_post(); ?>

    <h2 class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    Posted by: <?php the_author() ?> on <?php the_time('F jS, Y'); ?>
    <?php the_excerpt(); ?>
    <a href="<?php the_permalink(); ?>" title="Read the rest of <?php the_title(); ?>" class="button">Read the rest of this entry »</a> 

<div class="hr"></div>
<?php endwhile; ?>

<!-- PAGINATION STARTS HERE -->
<div class="button"><?php next_posts_link'<< Older posts' ); ?></div> <div class="button"><?php previous_posts_link'Newer posts >>' ); ?></div>

</div>

<div class="right">

<ul>

<?php if ( !function_exists('dynamic_sidebar') ||  !dynamic_sidebar('social-sidebar') ||  !dynamic_sidebar('blog-sidebar') ) { ?>

<?php ?>

</ul>

</div>
</div>

<!--FOOTER START HERE-->
<?php get_footer(); ?>
  #2  
Old 10-24-2012, 09:09 AM
NicktheGeek's Avatar
NicktheGeek NicktheGeek is offline
Forum Manager
 
Join Date: Feb 2010
Posts: 62,650
Default

you need to pass that as part of your WP_query args
http://codex.wordpress.org/Class_Reference/WP_Query
__________________
Nick "Fred and/or George Weasley" Croft
Designs by Nick the Geek
@Nick_theGeek on Twitter

Make web design easier, get FireBug for FireFox

Want to learn more about Genesis? Check out my Genesis Explained Series

  #3  
Old 10-24-2012, 11:56 AM
ckarasiewicz ckarasiewicz is offline
Registered User
Pro Plus Member
 
Join Date: Jun 2011
Posts: 75
Default

Can you elaborate a little more on how to pass that part of WP_query args?
  #4  
Old 10-24-2012, 11:58 AM
ckarasiewicz ckarasiewicz is offline
Registered User
Pro Plus Member
 
Join Date: Jun 2011
Posts: 75
Default

So I tweaked my code and moved the query_posts around.

PHP Code:
<?php  /*Template Name:Blog*/ ?>

<?php get_header(); ?>
<!--HEADER END HERE-->

<!--CONTENT STARTS HERE-->
<div class="modern-content-blog">
<div class="left">
<h2> <?php the_title(); ?> </h2>

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;  
$args = array( 'post_type' => 'post''posts_per_page' => 10'paged' => $paged );
$wp_query = new WP_Query($args);
$query = new WP_Query'cat=-12' );
while ( 
have_posts() ) : the_post(); ?>

    <h2 class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    Posted by: <?php the_author() ?> on <?php the_time('F jS, Y'); ?>
    <?php the_excerpt(); ?>
    <a href="<?php the_permalink(); ?>" title="Read the rest of <?php the_title(); ?>" class="button">Read the rest of this entry »</a> 

<div class="hr"></div>
<?php endwhile; ?>

<!-- PAGINATION STARTS HERE -->
<div class="button"><?php next_posts_link'<< Older posts' ); ?></div> <div class="button"><?php previous_posts_link'Newer posts >>' ); ?></div>

</div>

<div class="right">

<ul>

<?php if ( !function_exists('dynamic_sidebar') ||  !dynamic_sidebar('social-sidebar') ||  !dynamic_sidebar('blog-sidebar') ) { ?>

<?php ?>

</ul>

</div>
</div>

<!--FOOTER START HERE-->
<?php get_footer(); ?>
  #5  
Old 10-24-2012, 01:49 PM
NicktheGeek's Avatar
NicktheGeek NicktheGeek is offline
Forum Manager
 
Join Date: Feb 2010
Posts: 62,650
Default

you need to add the category argument to your list of $args. You do not need two WP_Query() instances. Just put your 'cat' => '-x' into the array for your $args.

You also are calling your query wrong. with new WP_Query() you don't use have_posts() you will need to use $wp_query->have_posts() because you are calling the value for that one instance.
__________________
Nick "Fred and/or George Weasley" Croft
Designs by Nick the Geek
@Nick_theGeek on Twitter

Make web design easier, get FireBug for FireFox

Want to learn more about Genesis? Check out my Genesis Explained Series

  #6  
Old 10-24-2012, 02:09 PM
ckarasiewicz ckarasiewicz is offline
Registered User
Pro Plus Member
 
Join Date: Jun 2011
Posts: 75
Default

Nick, got it! Thanks so much for clarifying this.

Just so I understand this, could I also exclude individual posts using this query - while I'm also excluding certain categories at the same time? I know it probably makes more sense to put everything into a category and just exclude the category.

What would be the best way to do this, concatenate the post id with the category?

$args = array( 'post_type' => 'post', 'posts_per_page' => 40, 'cat' => '-12, -11, -10, -9, -8, -5, -3' , 'paged' => $paged );
  #7  
Old 10-24-2012, 02:49 PM
NicktheGeek's Avatar
NicktheGeek NicktheGeek is offline
Forum Manager
 
Join Date: Feb 2010
Posts: 62,650
Default

you should be able to do that but you cannot include and exclude posts at the same time.
__________________
Nick "Fred and/or George Weasley" Croft
Designs by Nick the Geek
@Nick_theGeek on Twitter

Make web design easier, get FireBug for FireFox

Want to learn more about Genesis? Check out my Genesis Explained Series

 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
exclude blog page category s2aebelt General Discussion 3 10-09-2009 11:23 AM
Exclude category from home page violet General Discussion 9 03-04-2009 11:38 AM


All times are GMT -5. The time now is 06:54 AM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.