php - query_posts( $args) exclude a category -


i trying exclude category shortcode shows latest posts. have searched while solution cant seem different options work.

this code, (unfortunately didn't write need edit it):

function blogcuts_homepage_sc($atts, $content = null) {     $blogh = '';     $last = '';     $counter = 1;     $args = array(         'post_type' => 'post',         'order' => 'desc',         'posts_per_page' => 4     );     query_posts( $args );     if ( have_posts() ) : while ( have_posts() ) : the_post();     if ($counter == $args['posts_per_page']) { $last = ' last-column'; }         $img_array = wp_get_attachment_image_src(get_post_thumbnail_id(), 'small');         $comments = get_comments_number();         $custom = get_post_custom();          $blogh.= '<article class="blog-excerpt'.$last.'">';         $blogh.= '<h3><a class="bg-transition" href="'.get_permalink(get_the_id()).'">'.get_the_title().'</a></h3>';         $blogh.= '<a href="'.$img_array[0].'" rel="prettyphoto" title="'.get_the_title().'">';         $blogh.= get_the_post_thumbnail($post->id, 'item', array('title'    => get_the_title()));         $blogh.= '<div class="meta"><span class="date">'.get_the_date().'</span><div class="alignright"><span class="number-of-comments">'.$comments.'</span><a class="comments" href="'.get_permalink(get_the_id()).'#comments"></a>';          if (get_option('op_likeit') == 'enabled') {             $blogh.= '<span class="number-of-likes likes-'.get_the_id().'">'.likes(get_the_id()).'</span><a id="likeit-'.get_the_id().'" class="liked-after-'.get_the_id().' likes likeit'.liked(get_the_id()).'" href="#"></a>';         }         $blogh.= '</div></div><p class="excerpt">'.$custom['desc'][0].'</p></article>';         $counter++;         endwhile; endif;         wp_reset_query();         return $blogh;     } } add_shortcode("blogcuts_homepage", "blogcuts_homepage_sc"); 

the cat id 23. have tried adding 'exclude'=> '23', array not work. suggestions or able point me in right direction appreciated!

edit: get_posts() preferred way this, think, won't interfere the_loop() (see below) sure want use query_posts() , not get_posts()?

query_posts alters main loop, might cause unexpected behavior when user adds shortcode page , page "breaks".

that being said, code use query_posts exclude categories 1, 2, , 3:

edit: removed "array_merge" , unecessary verbage 'post', 'order' => 'desc', 'cat' => '-1,-2,-3', 'posts_per_page' => 4 ); query_posts( $args ); ?>

if chose use get_posts instead, this: 'post', 'order' => 'desc', 'cat' => '-1,-2,-3', 'posts_per_page' => 4 );

$myposts = get_posts( $args ); foreach ( $myposts $post ) : setup_postdata( $post ); ?>     ....  can operate in the_loop() endforeach;  wp_reset_postdata(); ?> 

http://codex.wordpress.org/class_reference/wp_query

exclude posts belonging category  display posts except category prefixing id '-' (minus) sign.  $query = new wp_query( 'cat=-12,-34,-56' ); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -