I am using the following function to display a post thumbnail or ACF custom field image depending on the page type being viewed. This function is loaded from the inc/template-tags.php
file in the active theme directory.
if ( ! function_exists( 'flagstaffcounty_post_thumbnail' ) ) :
/**
* Displays an optional post thumbnail.
*
* Wraps the post thumbnail in an anchor element on index views, or a div
* element when on single views.
*/
function flagstaffcounty_post_thumbnail() {
if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
return;
}
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( is_singular( 'post' ) || is_search() ) : ?>
<figure class="" itemprop="image" itemscope="" itemtype="http://schema.org/ImageObject">
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
</figure><!-- .post-thumbnail -->
<?php elseif ( is_page() ) : ?>
<figure class="page-thumbnail" style="background-image: url(<?php echo $url; ?>);" itemprop="image" itemscope="" itemtype="http://schema.org/ImageObject">
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
</figure>
<?php elseif ( is_category( 'news' ) ) : ?>
<?php $image = get_field( 'taxonomy_image' ); ?>
<figure class="tax-thumbnail" <?php if( !empty( $image ) ) : ?>style="background-image: url(<?php echo esc_url($image['url']); ?>);"<?php endif; ?> itemprop="image" itemscope="" itemtype="http://schema.org/ImageObject">
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
</figure><!-- .post-thumbnail -->
<?php else : ?>
<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
<?php
the_post_thumbnail(
'post-thumbnail',
array(
'alt' => the_title_attribute(
array(
'echo' => false,
)
),
)
);
?>
</a>
<?php
endif;
}
endif;
The function works properly on is_singular( 'post' )
and is_page()
page types, but it does not work on is_category()
, either with empty parentheses or the news category slug provided.
The function is loaded into each page type using <?php flagstaffcounty_post_thumbnail(); ?>
. This code is placed just after the get_header();
include in each file. Archives are using the default archive.php
file, pages are using the default page.php
file, and posts are using the default single.php
file. There are no category specific template files that could be overriding the default archive.php, though I did test category-news.php
and category.php
, but both did not work.
There is nothing in my functions.php
file that could be affecting the loading of the thumbnail function on the archive pages.
Maybe I am missing something simple...
Any help is greatly appreciated.