WordPress.org

Ready to get started?Download WordPress

Forums

Excluding a specific category in a photo gallery (4 posts)

  1. cvcreative
    Member
    Posted 1 year ago #

    Hi there,

    I am working with a Wp theme on a site and I am having a problem excluding categories from the photo gallery. This is the work in progress site. I am trying to exclude 2-3 categories from the catch all part of the photo gallery under the 'shop' nav. For example I want to exclude the colors from the shop but still allow them to show up under the 'colors' tab.

    I am thinking it is somewhere in the bit of code below. I have been searching everywhere and tried a few things but I am not very savvy at php. Any insight would be awesome.

    <?php
    		//get portfolio categories
    		$cats = get_terms('portfolio_cats');
    		//show filter if categories exist
    		if($cats[1] !='') { ?>
    
            <!-- Portfolio Filter -->
            <ul class="filter clearfix">
            	<li class="sort"><?php _e('Sort Items','workz'); ?>:
                <li class="cat-item active"><a href="#all"><span><?php _e('All', 'workz'); ?></span></a>
                <?php
                foreach ($cats as $cat ) : ?>
                <li class="cat-item"><a>slug; ?>" class="<?php echo $cat->slug; ?>"><span><?php echo $cat->name; ?></span></a>
                <?php endforeach; ?>
    
    	<?php } ?>

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code has been permanently damaged/corrupted by the forum's parser.]

  2. leonmagee
    Member
    Posted 1 year ago #

    $cats = get_terms('portfolio_cats) assigns the variable $cats with an array of all of the term objects of the portfolio_cats custom taxonomy. So once you do the loop (foreach ($cats as $cat )), the variable $cat will be assigned, one at a time, with the different term objects. That term object's values are obtained by referencing the object's properties, so echo $cat->name will echo the name property of the current term object. So in order to skip some of the custom taxonomy terms, you could, within the foreach loop, do something like:

    if ($cat->name == 'this_term_should_be_skipped')
    {
         continue;
    }
    else
    {
         // the code that is already in the foreach loop...
    }
  3. naveedsukhera
    Member
    Posted 1 year ago #

    Hi,
    I need to exclude specific category posts from this loop but same getting all posts of all categories..
    This is my code:

    <?php
    				$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    				$wp_query = new WP_Query('posts_per_page='.get_theme_mod('home_postnum').'&paged=' . $paged);
    				if (have_posts()) {
    					while (have_posts()) : the_post();
    						global $post;
    						include(TEMPLATEPATH. '/includes/loop.php');
    					endwhile;
    				} else {
    					include(TEMPLATEPATH. '/includes/not-found.php');
    				}
    
    				wp_reset_query();
    			?>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

    Please help me regarding this Issue..
    Thanks
    ---------------

  4. Jeff Clark
    Member
    Posted 1 year ago #

    try something like this

    $args = array(
    'category__not_in' => some_id
    'more_args' => ''
    );
    
    $loop = new WP_QUERY($args)
    
    while($loop->have_posts()) : $loop->the_post() ;
    // your content for the loop
    endwhile;
    wp_reset_query();

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

    Also check out the codex here https://codex.wordpress.org/Class_Reference/WP_Query

Topic Closed

This topic has been closed to new replies.

About this Topic