Hello Southwest.
In archive.php you could get the category ID, it should be:
$cat_id = get_query_var( 'cat' );
Then you show the image:
echo cfi_featured_image( array( 'size' => 'large', 'title' => 'This is a test...', 'class' => 'my-image', 'alt' => 'My image', 'cat_id' => $cat_id ) );
But I didn’t tried myself.
Thanks, that’s helpful. I’m working with it.
archive.php uses get_header();
to create the header at the top of the archive page, which is why I’m editing header.php.
Is there an easy way to test if a category has a featured image? For instance, header.php currently has a block that begins:
<?php if ( is_singular() && has_post_thumbnail() ) : ?>
I want to write something like <?php if ( is_category() && has_category_featured_image() ) : ?>
, or something else that tests to see if the category has a featured image specified. Is there a way to do that?
In other words, I’m trying to make an equivalent of this block for category featured images:
<?php if ( is_singular() && has_post_thumbnail() ) : ?>
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'post-image-cover' );
$post_image = $thumb['0'];
?>
<div class="header-image bg-image" style="background-image: url(<?php echo esc_url( $post_image ); ?>)">
<?php the_post_thumbnail('post-image'); ?>
</div>
You could do something like this:
$fimg = cfi_featured_image_url( array( 'size' => 'large', 'cat_id' => $cat_id ) );
if( !empty( $fimg ) ) echo '<img src="', $fimg,'" />';
Mattia, thank you for your help. I was able to get this working. This is what the relevant section of header.php looks like now:
<!-- southwest: declare these variables for use later -->
<?php $cat_id = get_query_var( 'cat' );
$fimg = cfi_featured_image_url( array( 'size' => 'post-image-cover-size', 'cat_id' => $cat_id ) );?>
<!-- /southwest's modifications -->
<?php if ( is_singular() && has_post_thumbnail() ) : ?>
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'post-image-cover' );
$post_image = $thumb['0'];
?>
<div class="header-image bg-image" style="background-image: url(<?php echo esc_url( $post_image ); ?>)">
<?php the_post_thumbnail('post-image'); ?>
</div>
<!-- southwest's modifications for category featured image go here -->
<?php elseif ( is_category() && !empty( $fimg ) ) : ?>
<div class="header-image bg-image" style="background-image: url(<?php echo esc_url( $fimg ); ?>)">
<?php cfi_featured_image( array( 'size' => 'post-image-cover-size', 'cat_id' => $cat_id) ); ?>
</div>
<!-- /southwest's modifications -->
<?php else : ?>
<div class="header-image bg-image" style="background-image: url(<?php if (get_header_image() != '') { header_image(); echo ')'; } else { echo get_template_directory_uri() . "/images/header.jpg)"; } ?>">
<?php
if (get_header_image() != '') {
echo '<img src="'; header_image(); echo '">';
} else {
echo '<img src="' . get_template_directory_uri() . '/images/header.jpg">';
}
?>
</div>
You can see this in action in a few categories on my blog. For example, the “Internet” category.
Good – please rate my plugin if you like it 🙂
this post is so much helpful. i want title above the image on post details page.
Thanks….
With the category id as above you can get the cat name as title:
echo get_cat_name( $cat_id );
Hi, I’m also using (a childtheme of) the Lovecraft theme on my site about exotic holidays (http://exotischevakantie.info) and stumbled into this topic. Really great solution, I instantly got it working, many thanks for sharing this!