• Resolved Southwest

    (@southwest)


    I’d like to use this plugin to set a featured image for each category, and then modify my theme so that when browsing the archives by category, the featured image displays in the same place it would for single posts.

    I’m using a child of the Lovecraft theme.

    I believe this is the relevant part of header.php that places the featured image for posts:

    <?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>

    <?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>

    <?php endif; ?>

    How do I modify this so that in addition to displaying featured images on single posts, it uses this plugin and displays category featured images on category archive pages?

    Thanks!

    https://wordpress.org/plugins/category-featured-images/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Mattia Roccoberton

    (@blocknotes)

    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.

    Thread Starter Southwest

    (@southwest)

    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?

    Thread Starter Southwest

    (@southwest)

    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>

    Plugin Author Mattia Roccoberton

    (@blocknotes)

    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,'" />';

    Thread Starter Southwest

    (@southwest)

    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.

    Plugin Author Mattia Roccoberton

    (@blocknotes)

    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….

    Plugin Author Mattia Roccoberton

    (@blocknotes)

    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!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How would I modify this part of header.php to display category image?’ is closed to new replies.