0

I have a custom post type, "design_asset".

I am displaying them on a page template, with the loop

<?php
rewind_posts();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'design_asset', 'posts_per_page' => 100, 'orderby' => 'title', 'order' => 'ASC'  );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-xs-12">
<div class="thumbnail">
    <div class="caption">
        <?php the_post_thumbnail( 'thumbnail', array('class' => 'img-responsive') ); ?>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <p class="excerpt"><?php the_excerpt(); ?></p>
    </div>
</div>
</div>
<?php    endwhile;   ?>

Also on this page, I am listing the categories with wp_list_categories

When you click a category in this list, it goes to archive.php and displays the category there, thanks to this post's reply, e.g./category/components/

I don't fully understand why Wordpress uses archive.php instead of category.php, but let's go with it for now.

Now, I am trying to re-purpose this loop on archive.php. I moved it into loop-assetThumbs.php and call it with <?php get_template_part('loop', 'assetThumbs'); ?>

Of course, it calls ALL of the posts or the post type, I can't figure out how to display only the "active" category in the URL, /category/components/

How do I modify the loop to do this? Or is there a better way to display categories of custom post types via wp_list_categories?

Note: I'm also not sure why it's showing 2 empty iterations of the loop here.

enter image description here

  • Seems like categories is been treated as taxonomy terms, which will explain why archive.php is used instead of category.php. I also believe then that you don't have a taxonomy.php template then, otherwise wordpress would have used that and not archive.php – Pieter Goosen Jul 24 '14 at 16:02
  • Nope. I created taxonomy.php, and it still uses archive.php – Steve Jul 24 '14 at 19:33
1

I think your problem is in the exact code that you've mentioned. pre_get_post is a bastard if you don't use it correctly.

With any archive type conditions used in pre_get_posts, it will affect both front end and back end. This include archives pages, category and taxonomy pages, tag pages and author pages. You'll need to do a check that you only run pre_get_posts on the front end (use !is_admin()).

One other important thing you'll need to do is to run pre_get_posts only on the main query. Because pre_get_posts runs first before the main query and WP_Query and modify the query variables, both the main query and any custom queries using WP_Query will be affected. (Use is_main_query())

So you will need to modify the code in question to this to add the !is_admin check

function namespace_add_custom_types( $query ) {
    if( !is_admin() && $query->is_category() && $query->is_main_query ) {
        $query->set( 'post_type', array(
            'post', 'your-custom-post-type-here'
));
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

I hope this helps. If not, let me know.

EDIT

Steve, before you make these changes I've proposed, first check my answer on your other question. But please keep the points in mind in this answer for future reference.

| improve this answer | |
  • Thanks Pieter. As I said there, I have mostly worked with custom Page templates but am new to the other template loops and related functions. – Steve Jul 25 '14 at 11:52
  • I would recommend to have a read here to get a better understanding of theme development. Just glad I could help. Please accept this answer as well so this can also go as solved, unless you still need some assistance – Pieter Goosen Jul 25 '14 at 12:01

Your Answer

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

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