0

I want to exclude posts from one category. These posts should not show in the sidebar on the homepage only when I explicitly don't want that particular posts to show. In all other cases these posts will be displayed.

I have tried with plugin "WP Hide Post", but that seems not to work with sidebars, only for regular loops. I've checked on hide this post from homepage, and nothing happens.

This is the code I use to display post in homepage sidebar:

<?php query_posts('category_name=events&showposts=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" accesskey="s"><?php the_title(); ?></a>
</h2>
<div class="myclass3"><?php wpe_excerpt('wpe_excerptlength_index', 'wpe_excerptmore'); ?>                        </div><BR>
<?php endwhile; ?>
0

2 Answers 2

1

You can add this code to a new file named wpsites.php and then use the template tag directly in any file or hook it in using a custom function with theme specific or WordPress hooks.

Note: All code can be used in a child theme.

<?php 

// Your Custom Query Arguments
$args = array(
'category__not_in' => array( 007 ) 
) );

$wpsites_catposts = new WP_Query( $args ); 

// Your Custom Loop
if ( $wpsites_catposts->have_posts() ) {

    echo '<div class="primary-sidebar"><ul>';

while ( $wpsites_catposts->have_posts() ) {

    $wpsites_catposts->the_post();

    echo '<li><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . get_the_title() . '</a></li>';
}
    echo '</ul></div>';
} else {
  echo '<div class="primary-sidebar">No posts found for this query.</div>';
}   

wp_reset_postdata();

Get Template Part

Add this code in any template file. For this question, one of your sidebar.php files.

<?php get_template_part( 'wpsites' ); ?>

Custom Function

Or hook it in if your theme includes action hooks:

add_action ( 'your_themes_before_sidebar_hook', 'exclude_posts_in_sidebar_loop' );
function exclude_posts_in_sidebar_loop() {
if ( is_home() ) {
get_template_part( 'wpsites' ); 
}}

Change the conditional tag in the above custom function to suit your own needs.

Alternative Method For Query

Rather than use the arrays of $args, you can replace Your Custom Query Arguments with this line:

$wpsites_catposts = new WP_Query( array( 
'category__not_in' => array( 007 ) 
) );
0

I'm not sure if this is what you're looking for, but you could just wrap everything INSIDE the while/endwhile with a

<?php if(! in_category( array( [category_id] ) )) ?>

   ....your code here to display the rest of the categories.....

<?php endif; ?>

Just replace [category_id] with the category you are looking to exclude

1
  • Tnx for answer, but this not what I want.Im searching for solution to manualy enable post from certain category to show or not on sidebar, eg. checkbox in edit post screen say:do not include this on homepage.Plugin "wp hide post" does that, but not work on sidebar, only for regular loop.
    – user4490
    Mar 7, 2012 at 9:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.