WordPress.org

Ready to get started?Download WordPress

Forums

CODE - Except category (3 posts)

  1. LiviutzuAs
    Member
    Posted 8 months ago #

    I have this code:

    /**
    	 * Display Latest posts
    	 */
    	static function showLatest( $posts = 5, $size = 45 ) {
    		global $post;
    		$latest = get_posts( array( 'suppress_filters' => false, 'ignore_sticky_posts' => 1, 'orderby' => 'post_date', 'order' => 'desc', 'numberposts' => $posts ) );
    
    		ob_start();
    		$count = 1;
    		$date_format = get_option('date_format');
    		foreach($latest as $post) :
    			setup_postdata($post);
    			if( $count++ % 2 ){
    				$class = 'odd';
    			} else {
    				$class = 'even';
    			}
    		?>
    			<li class="<?php echo $class; ?>">
    				<?php if ($size <> 0){
    					$imageArgs = array(
    						'width' => $size,
    						'height' => $size,
    						'image_class' => 'thumbnail',
    						'format' => 'array',
    						'default_image' => self::getWidgetUrl() . 'default.png'
    					);
    					$postImage = pptwj_get_the_image($imageArgs);
    				}
    				?>
    				<?php if( !empty( $postImage['src'] ) ):
    					$css = "width: {$size}px; height: {$size}px;";
    				?>
    				<a class="post-thumb" href="<?php the_permalink(); ?>"><img src="<?php echo $postImage['src']; ?>" alt="<?php echo $postImage['alt']; ?>" width="<?php echo $postImage['width']; ?>" height="<?php echo $postImage['height']; ?>" style="<?php echo $css; ?>"/></a>
    				<?php endif; ?>
    
    				<a class="item-title" title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    
    				<?php echo self::item_meta(get_the_time($date_format, $post)); ?>
    
    				<div class="fix"></div>
    			</li>
    		<?php endforeach;
    		$contents = ob_get_contents();
    		ob_end_clean();
    		return $contents;
    	}

    I am trying to make that to display all the articles excepting a category with id 12. Do you know where do i have to add something in my code ?

    Thanks for your help!

  2. alchymyth
    The Sweeper & Moderator
    Posted 8 months ago #

    add the exclude into this line:

    $latest = get_posts( array( 'suppress_filters' => false, 'ignore_sticky_posts' => 1, 'orderby' => 'post_date', 'order' => 'desc', 'numberposts' => $posts ) );

    for example:

    $latest = get_posts( array( 'suppress_filters' => false, 'ignore_sticky_posts' => 1, 'orderby' => 'post_date', 'order' => 'desc', 'numberposts' => $posts, 'category__not_in' => array( 12 ) ) );

    http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

  3. LiviutzuAs
    Member
    Posted 8 months ago #

    Thanks very very much. I tryied to edit that line until now but no success until u told me 'category__not_in' => array( 12 ) !

    Thanks very much!

Reply

You must log in to post.

About this Topic