3

I have 8 custom post types (which are actually for 8 different blogs and used as "blog landing pages"). Each custom post type is also a Blog Category and each has several sub categories to it. For example, there is a custom post type ("blog") named Business without Borders. Within it are several subcategories such as: Banking, Intellectual Property, etc.

I can create category pages for each of the 8 post types, but I'm having a big issue matching up the posts that are being displayed in the main content loop with the sub categories that should be displayed on the sidebar navigation menu. What I want to have is a custom post type category page that displays, for example, all of the Business without Borders custom posts by date descending and then have a sidebar consisting of links to all of the sub category listing pages that Business without Borders custom posts have been sorted into.

For the subcat sidebar menu I'm using a the following function defined in my functions.php:

//for menus on Post Type + Category Archive pages
function wp_list_categories_for_post_type($post_type, $args = '') {
$exclude = array();

// Check ALL categories for posts of given post type
foreach (get_categories() as $category) {
    $posts = get_posts(array('post_type' => $post_type, 'category' => $category->cat_ID));

    // If no posts found, ...
    if (empty($posts))
        // ...add category to exclude list
        $exclude[] = $category->cat_ID;
}

// Set up args
if (! empty($exclude)) {
    $args .= ('' === $args) ? '' : '&';
    $args .= 'exclude='.implode(',', $exclude);
}
echo $args;
// List categories
echo '<h3 style="margin-top:0; font-weight:bold;">Categories</h3><ul class="subNav" style="border-top:none;margin-top:17px;">';
                 wp_list_categories($args);
                //wp_list_categories('include='.$args.'&title_li=');
                //wp_list_categories('include='.$post_type_cats.'&title_li='); - old stylized way from archive page
                echo '</ul>';

}

And I call that menu in my custom-post-category.php page like so:

<?php
    if($this_category->category_parent)
        $this_category = wp_list_categories('orderby=name&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0");
    else
        $this_category = wp_list_categories('orderby=name&depth=1&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
    ?>  

<?php if ($this_category) { ?>
    <h3 style="margin-top:0; font-weight:bold;">Categories</h3>
        <ul class="subNav" style="border-top:none;margin-top:17px;">
        <?php echo $this_category; ?>
        </ul>
<?php } ?>

I've tried it 2 different ways and each one is wrong in the exact opposite way. For example. I have created a category-bwob.php template which has the custom post type query outside the loop:

$args = array('post_type'=>array('posts', 'bwob_blog'));
      query_posts($args); 

    if (have_posts()) : ?>

And then my loop:

 <article id="post-<?php the_ID(); ?>">
      <header>
    <ul class="posts">
        <li><a href="<?php the_permalink() ?>?category=<?php echo $this_cat; ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><span class="date"><?php echo get_the_date('M d, Y'); ?></span><?php the_title_attribute(); ?></a><div class="teaser"><?php the_content(); ?><p>Posted in: <?php echo get_the_category_list(', ') ?> | <?php the_tags(__('Tags:', 'kubrick') . ' ', ', ', ' '); ?> </p></div></li>
      </header>
 </article>

Which lists all the Business without Borders posts by latest date just fine. But then for the sidebar nav, and it winds up listing ALL of the Categories from the ENTIRE site:

Then when I try to query for the custom post type inside the loop as so:

$args = array( 'post_type' => 'bwob_blog', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();

and using the same category listing function as above, it displays the posts fine and the correct sub categories, but when you click into the sub categories, nothing is sorted, it just displays all of the Business with Border custom post types by date just like the category landing page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.