How to show multiple CPT by category in custom archive page?
-
Can anyone please tell me how can I show multiple CPT by category in archive page. Lets say I have CPT “product” and another CPT “news”. I managed to get the CPT product to list on my archive page but I can’t list news page. I renamed archive-news.php. I tried this code to list my both product and news but it only shows my product page which is on my archive page. It doesn’t show archive-news.php.
//add multi custom post type to tags and categories function add_custom_types_to_tax( $query ) { // Return right away if in admin or not working on the main frontend query if ( is_admin() || ! $query->is_main_query() ) { return; } // Check if we're on a category or tag archive. If so, add our post type if( ( $query->is_category() || $query->is_tag() ) ) { // Get all your post types $post_types = array('post' , 'product' , 'news'); $query->set( 'post_type', $post_types ); } } add_action( 'pre_get_posts', 'add_custom_types_to_tax' );
My archive page code is:
<?php if ( have_posts() ) : the_archive_title( '', false ); the_archive_description( '<div class="taxonomy-description">', '</div>' ); while ( have_posts() ) : the_post(); ?> <h3><?php the_title(); ?> </h3> <?php endwhile; the_posts_pagination( array( 'prev_text' => __( '<<', 'twentysixteen' ), 'next_text' => __( '>>', 'twentysixteen' ), 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'twentysixteen' ) . ' </span>', ) ); else : get_template_part( 'template-parts/content', 'none' ); endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘How to show multiple CPT by category in custom archive page?’ is closed to new replies.