WordPress.org

Support

Support » Themes and Templates » [Resolved] Incorporating “category__in” into category.php

[Resolved] Incorporating “category__in” into category.php

  • I have a site that is comprised of categories and sub-categories. I’d like to have only the category’s child posts display on the category.php page. I’m using the following to order posts, so I think there’s a way to add the category__in parameter, but I haven’t been able to figure out how.

    <?php
      $cat_title = single_cat_title('', false);
      $cat_id = get_cat_ID($cat_title);
    
        $wp_query->set('orderby', 'category');
        $wp_query->set('order', 'ASC');
        $wp_query->get_posts();  
    
        get_posts('orderby=category&order=ASC'); ?>
    <?php if (have_posts()) : ?>
    etc.

    $cat_id is to obtain the current category’s ID to use limiting to that category, but as I said, I can’t figure out how to do it. If there’s another approach to accomplish this, that would be fine, too.

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try this

    <?php
    
    query_posts("order_by=category&category__in=array(1,2,3)")
    
    ?>

    I changed the first bit of code to:

    $wp_query->set('orderby', 'menu_order');
        $wp_query->set('order', 'ASC');
        $wp_query->set('category__in', array(4));
        $wp_query->get_posts();
        get_posts("orderby=menu_order&order=ASC&category__in=array(4)"); ?>
    	<?php if (have_posts()) : ?>

    But I get the same results: all posts from all sub-categories of category 4 get displayed.

    <?php
    get_posts("orderby=menu_order&order=ASC&child_of=4");
    ?>

    Try above code.

    I figured out how to accomplish my purpose: listing posts for categories that have them and listing sub-categories for parent categories that have no posts. Here is the whole template for anyone else that might benefit from it.

    <?php // Category.php, template for listing sub-categories or category posts ?>
    
    <?php get_header(); ?>
    <?php
      $cat_title = single_cat_title('', false);
      $category_id = get_cat_ID($cat_title);
        $wp_query->set('orderby', 'menu_order');
        $wp_query->set('order', 'ASC');
        $wp_query->get_posts();
        get_posts("orderby=menu_order&order=ASC"); ?>
    <?php if ( in_category($category_id) ) : ?>
    	<?php if (have_posts()) : ?>
    		<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
    		<h2 class="pagetitle">
    			<?php if (is_category()) { ?>
    				Index for ‘<?php single_cat_title(); ?>’
            <?php } ?>
    		</h2>
    		<div class="list-page">
    			<?php while (have_posts()) : the_post(); ?>
    				<div <?php post_class() ?>>
    					<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    				</div>
    			<?php endwhile; ?>
    		</div>
    <?php
    endif;
    ?>
    	<?php else :
    		if ( is_category() ) {  // If this is a category archive ?>
    		<h2 class="pagetitle">
    				Index for ‘<?php single_cat_title(); ?>’
            <?php $category_link = get_category_link( $category_id ); ?>
    		</h2>
    		<div class="list-page">
          <ul>
          <?php wp_list_categories('orderby=name&child_of=4&title_li='); ?>
          </ul>
    		</div>
    		<?php }
        endif;
    ?>
    	<?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Hope that helps

    The whole magic is actually here: if ( in_category($category_id) ) :. Thank you man! I lost almost a day in searching for a solution and you saved my day!

    I don’t know why but query_posts(‘category__in=’ . $category_id); actually doesn’t work. So the workaround is putting the above if statment before the Loop.

    Great for sharing it!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Incorporating “category__in” into category.php’ is closed to new replies.