I've created four custom post types -- videos, audio, programs, blogs -- with various custom fields and I've managed to created a custom archive page that displays certain custom fields depending on the post type. The archive page is working well when only viewing the archive of a certain custom post type. The issue I'm having is with the archive pages for categories.
Each custom post type has access to global 'post' categories, so all of my categories show up in each post type. I'd like to be able to query a category and show associated posts regardless of the post type. i.e. the 'business' category may pull up two videos, an audio post, and a blog. The problem is that my category pages are empty right now.
Here is my current loop in 'archive.php':
<?php
if ( have_posts() ) : ?>
<div class="container">
<div class="row">
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content-archive', get_post_format() );
endwhile;
the_posts_navigation(); ?>
</div><!-- .row -->
</div><!-- .container -->
<?php endif; ?>
This grabs the template for archive content 'content-archive.php':
<?php
/**
* Get featured posts from Archives
*/
if( is_post_type_archive( 'videos' ) ) {
$video_image = get_field( 'video_still_image' );
print_archive_post($video_image);
} elseif( is_post_type_archive( 'programs') ) {
$program_featured_image = get_field('program_featured_image');
print_archive_post($program_featured_image);
} elseif( is_post_type_archive( 'audio') ) {
$audio_featured_image = get_field('audio_featured_image');
print_archive_post($audio_featured_image);
} elseif( is_post_type_archive( 'blogs') ) {
$blog_featured_image = get_field('blog_featured_image');
print_archive_post($blog_featured_image);
}
?>
And, here is my function from 'functions.php' to build the content for the post:
// Function to print archive type posts
function print_archive_post($image) {
echo '<div class="col-md-4 featured-thumb-container">';
echo '<a href="' . get_the_permalink() . '"><span class="featured-thumb" style="background: url(' . $image . ')"></span></a>';
echo '<h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3>';
// The category list
$post_cats= array();
$categories = get_the_category();
echo '<p class="category-list">';
foreach($categories as $cat) :
echo '<a class="category-link" href="' . get_home_url() . '/category/' . $cat->slug . '">' . $cat->cat_name . '</a><span>,</span> ';
endforeach;
echo '</p>';
echo '</div>';
}
I know I'm missing a conditional to check for category, but I haven't been able to find a solution in my research. Any help would be greatly appreciated. :)