I have multiple custom posts types that use the same category taxonomy. Let's call the CPT's 'venue' and 'story', and say we have categorised each of them as 'foo'.
I already have the following code to ensure they're part of the taxonomy loops:
//make sure these custom post types are registered for taxonomy queries
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if(is_category() || is_tag()) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('nav_menu_item', 'venue', 'story'); // don't forget nav_menu_item to allow menus to work!
$query->set('post_type',$post_type);
return $query;
}
}
When I visit example.com/category/foo, category.php is fired up. What I'd like to see is:
Page starts and shows custom image, title, lead sentence
A title 3x items from the 'venue', category foo
A title 6x items from the 'story', category foo
Pagination, Footer
Here's category.php: (also at: https://gist.github.com/anonymous/b747a2c51bc5bb7a58dd)
<?php
/**
* The template for displaying category pages.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package DW Focus
* @since DW Focus 1.0
*/
get_header(); ?>
<?php
// vars
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$image = get_field('category_image', $queried_object);
$lead = get_field('category_lead', $queried_object);
?>
<div id="primary" class="site-content category span12">
<?php
if( !empty($image) ): ?>
<div id="featured_image_category" style="height:200px;">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
</div>
<?php endif; ?>
<div class="content-bar row-fluid">
<h1 class="page-title"><?php printf( __( '%s articles', 'dw-focus' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?></h1>
</div>
<?php if( !empty($lead) ): ?>
<div class="lead"><p><?php echo $lead; ?></p></div>
<?php endif; ?>
<h2 style="text-align: center;">Luxury and Boutique Hotels in <?php printf( __( '%s', 'dw-focus' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?></h2>
<?php get_template_part( 'content', 'venueloop'); ?>
<div class="clearfix"></div>
<h2 style="text-align: center;">Explore <?php printf( __( '%s', 'dw-focus' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?> with our unique travel experiences</h2>
<?php get_template_part( 'content', 'storyloop' ); ?>
<div class="clearfix"></div>
<?php dw_focus_pagenavi(); ?>
</div>
<?php get_footer(); ?>
Content-part for venueloop and story loop (also at https://gist.github.com/indietravel/38ed27762fb844eee0a5):
<div class="content-inner">
<?php
if ( is_category() ) {
$cat = get_query_var('cat');
$ourcat = get_category ($cat);
$category_selected = $ourcat->slug;
}
$args = array (
'post_type' => 'story',
'post_status' => 'publish',
'posts_per_page' => '6',
'category_name' => $category_selected,
);
// The Query
$cat_query = new WP_Query( $args );
?>
<?php if ( $cat_query -> have_posts() ) : ?>
<?php global $archive_i; $archive_i = 1 ?>
<?php while ( $cat_query -> have_posts() ) : $cat_query -> the_post(); ?>
<?php get_template_part('content', 'archive'); ?>
<?php $archive_i; ?>
<?php endwhile; ?>
<?php endif;
wp_reset_postdata(); // ADAPTED FOR CUSTOM POST TYPES
wp_reset_query();
?>
</div>
Venue Loop:
<?php
// WP_Query arguments
$args = array (
'post_type' => 'venue',
'post_status' => 'publish',
'pagination' => false,
'posts_per_page' => '3',
);
// The Query
$venue_query = new WP_Query( $args );
// The Loop
if ( $venue_query->have_posts() ) {
while ( $venue_query->have_posts() ) {
$venue_query->the_post();
the_title();
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
For some reason, the outcome is that the page always shows,
Page starts and shows custom image, title, lead sentence
A title 3x items from the 'venue' & 'story' CPT, category foo
A title 3x items from the 'venue' & 'story' CPT, category foo
Pagination, Footer