Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Ok so I have an issue with wordpress that has been driving me mad the last few days.

I have a custom post type called "help-line" this post type then has 13 categories and some of these categories have sub categories. I also have 5 custom fields.

When I create a post under help-line I assign it to whichever category it relates to (which can be multiple)

This is where I'm getting seriously confused.

I have a page called "page-help" which is using

<?php wp_list_categories('depth=1&title_li='); ?>

to display the top level categories.

When you click on one of the categories I'm assuming it is loading category.php which has the following code to display the children of the parent category that I have just clicked on.

                            <?php  
                                $categories = get_categories('hide_empty=0');  
                                foreach ($categories as $category) : 
                                    query_posts('post_type=help-line&showposts=2&cat='.$category->cat_ID); 
                                    while(have_posts()){
                                        the_post(); 
                                        ?>

                                        <div class="height">
                                            <div class="fourcol">
                                                <p class="business"><?php the_field('helpline_name'); ?></p>
                                                <p><?php the_field('helpline_address'); ?></p>
                                            </div>
                                            <div class="fivecol">
                                                <p class="phone"><?php the_field('helpline_phone_number'); ?></p>
                                            </div>                      
                                            <div class="threecol last">
                                                <ul>
                                                    <li><a href="" class="tiptip web" title="<?php the_field('helpline_name'); ?>" target="_blank"><?php the_field('helpline_name'); ?></a></li>
                                                    <li><a href="" class="tiptip comments" title="read comments from others" target="_blank"><?php the_field('helpline_comments'); ?></a></li>
                                                    <li><a href="mailto:<?php the_field('helpline_email'); ?>" class="tiptip email" title="email us your problem"><?php the_field('helpline_email'); ?></a></li>
                                                </ul>
                                            </div>
                                        </div><!-- end test height -->

                            <?php     }
                            endforeach; ?> 

But no matter what parent category I click on it just keeps showing the same posts.

The entire thing needs to be completely dynamic show that when I add a new category it will display automatically.

Can someone please help me, I would be so greatful!!! :(

Thanks in advance.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Categories by default only show posts, as do tags. This limitation is not enforced on custom taxonomies, and it can be overriden via a filter.

If you place this into your functions.php, then posts of type help-line will be shown in the listings.

// before we grab the posts in a query
add_filter('pre_get_posts', 'wpse49960_query_post_type');

function wpse49960_query_post_type($query) {
    // if it's a category/tag archive, and there are no 'suppress_filters'
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        // then set the 'post_type' query var so that it includes our custom post type, not just 'post'
        $query->set( 'post_type', array(
            'post', 'help-line','nav_menu_item'
        ));
        return $query;
    }
}

Also, for which template wordpress loads, see here:

http://codex.wordpress.org/Template_Hierarchy

share|improve this answer
    
Brilliant! Thank you so much!! Worked like a treat!! Is it as simple as me changing the name of the "post type" to something else to get the exact same thing to work for a different post type? –  Tyler Apr 24 '12 at 7:17
    
Or actually if you could break down your code you have given me explaining what each part does that would be great :) –  Tyler Apr 24 '12 at 7:33
    
Apologies for replying to this again :( Although your code is working perfect and again thank you very much! For some reason when I'm now on my category page my navigation doesn't show. The navigation was created using custom menus. –  Tyler Apr 24 '12 at 9:11
    
Try the updated code –  Tom J Nowell Apr 24 '12 at 10:10
    
Navigation still isn't showing. When I remove you code from functions.php it shows. –  Tyler Apr 24 '12 at 14:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.