0
votes

I realize this question has been asked a million times but I can't seem to find the answer I'm looking for.

The first question I can't find a straight answer for is am I able to use category.php to display custom post types?

If so then how?

I have a single-custom-post-type.php - works perfectly, same with an arhcive-custom-post-type.php but as I'm sure you experienced WordPress developers know once I click on 'custom-post-type' category it list 'normal' post types and not the custom post types.

For instance I have a custom post type called 'portfolio-post' I currently have two categories called 'book covers' and 'theatrical'. I know I can create category-custom-post-type.php pages but I am trying to avoid having to go in and create a new page every time my client decides to create a new portfolio category.

So it would be ideal if I could just modify category.php to handle custom post types. I'm somewhat new to WP development and I really need to get this down because this will be a great tool for future projects.

ANYWAY, any help would be much appreciated! Also on an another note is there anyway to only show categories associated with a custom post type in admin? Currently if I select 'categories' under 'Portfolio Post' it displays your standard blog post categories as well.

Thank you so much!

4

4 Answers

2
votes

This is a better way to do it:

global $wp_query;
$query = new WP_Query(array_merge(
$wp_query->query, 
array(
    'post_type' => 'any'
    )
));

while($query->have_posts()) : $query->the_post(); 

endwhile; wp_reset_query();
1
votes

I didn't test this but you could have a conditional statement like so, in your category.php:

if ('portfolio-post' == get_post_type()) {
  // code for the custom post
}

Please let me know if this works... I am curious :)

0
votes

I found a solution be happy to know if there is more efficient way.

<?php global $wp_query; $args = array_merge( $wp_query->query, array( 'post_type' => 'any' ) ); query_posts( $args ); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>

0
votes

add function in functions.php:

function changePost_type() {
    if( is_archive() && !is_admin() ) {
      set_query_var( 'post_type', array( 'page', 'portfolio-post' ) );
    }
    return;
}
add_action( 'parse_query', 'changePost_type' );

I hope i have helped :]