WordPress.org

Support

Support » Themes and Templates » Loop Categories Showing Posts in Cat ONLY for Custom Post Type?

Loop Categories Showing Posts in Cat ONLY for Custom Post Type?

  • I’m having a devil of a time with this. I hope someone can help.

    I have several custom post types in use around a single category system. In other words, CustomA, CustomB, and CustomC all use the same categories for identification. They’re easy to query except in the below, which shows every category, even if it doesn’t have any posts in the identified post_type. Can you help me exclude those categories that don’t include posts in the specified post_type?

    <?php
                // get all the categories from the database
                $cats = get_categories(); 
    
    					// loop through the categries
    					foreach ($cats as $cat) {
    						// setup the cateogory ID
    						$cat_id= $cat->term_id;
    						// Make a header for the cateogry
    						echo "<h3>".$cat->name."</h3>";
    						// create a custom wordpress query
    						query_posts("cat=$cat_id&post_type=resource&post_per_page=-1"); //-1 shows all posts per category.
    						// start the wordpress loop!
    						if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    								<?php // create our link now that the post is setup ?>
    								<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
    								<?php echo '<hr/>'; ?>
    
    						<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
                <?php } // done the foreach statement ?>

    Thank you in advance.

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

    <?php
                // get all the categories from the database
                $cats = get_categories(); 
    
    					// loop through the categries
    					foreach ($cats as $cat) {
    						// setup the cateogory ID
    						$cat_id= $cat->term_id;
    						// Make a header for the cateogry
    						echo "<h3>".$cat->name."</h3>";
    						// create a custom wordpress query
    						query_posts(array('cat' => $cat_id, 'post_type' => 'resource', 'post_per_page' => -1)); //-1 shows all posts per category.
    						// start the wordpress loop!
    						if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    								<?php // create our link now that the post is setup ?>
    								<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
    								<?php echo '<hr/>'; ?>
    
    						<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
                <?php } ?>

    Same result. All categories appear, even those without posts. You can see it here.

    So your doing a parent category and trying to display all the child category?

    Yes, but also excluding any categories that don’t contain any “resource” custom post type posts.

    Change your:
    $cats = get_categories();
    to:
    <?php $cats = get_categories(array('hide_empty' => 1));?>

    Still no luck. The empties won’t hide.

    http://codex.wordpress.org/Function_Reference/get_categories

    The link to get_categories is above, you need to take what I gave you add other params and values to get it to what you want. I’m not doing the whole thing exactly for you just pointing you to the right place. I can’t see your whole code either so pretty much we will be going around in circles. So with <?php $cats = get_categories(array('hide_empty' => 1));?> add other parameters to filter it out.

    I appreciate the sentiment, potentweb, but I have the get_categories code right, as you can see from my complete script inserted in the first post (and quoted by you in the second). The codex doesn’t address this situation.

    There are no “other parameters to filter it out.” There is type, childof, and hide_empty, all of which I’ve used yet the categories that don’t contain the given post-type still show up in the list. (Yes, I could use exclude, but that defeats the entire purpose of the page by not allowing for future growth into other categories).

    If you don’t know how to solve a problem, Chris, say so; don’t try to make it appear as if you do know the solution by saying “I’m not going to write the code for you” and then point to an irrelevant codex article.

    Hi guys,

    Can anyone help me out with that to make the category names a anchor. I mean, they are already anchors but they don`t link to appropriate category link.

    I don`t have any experience with PHP, but I try from my HTML knowledge to input href=”<?php the_permalink();?> on a anchor, but without success.

    Tnx,

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Loop Categories Showing Posts in Cat ONLY for Custom Post Type?’ is closed to new replies.
Skip to toolbar