• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Featured Image as div background (single.php)

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Featured Image as div background (single.php)

This topic is: not resolved

Tagged: featured image, single.php

  • This topic has 1 reply, 2 voices, and was last updated 8 years, 5 months ago by Brad Dalton.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • January 6, 2015 at 12:55 pm #136198
    sid
    Member

    Hi,

    I'm building a design which uses the featured image as a background for a div. And then the blog post title and date are shown inside the div (on top of the featured image).

    I was able to code this directly into home.php for the blog page.. But I couldn't make it work in single.php.

    After doing research I found the following code (for functions.php) which adds the featured image at the top of a blog post... But I am not sure how use the featured image as a background and insert the blog title + date.

    add_action( 'genesis_entry_header', 'single_post_featured_image', 5 );
    
    function single_post_featured_image() {
    	
    	if ( ! is_singular( 'post' ) )
    		return;
    	
    	$img = genesis_get_image( array( 'format' => 'html', 'size' => genesis_get_option( 'image_size' ), 'attr' => array( 'class' => 'post-image' ) ) );
    	printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img );
    	
    }

    The finished code structure that display at the top of each blog post should be like this.. Any idea how to get this into single.php? Or does it need to be done via functions.php??

    
    <?php
    $src = wp_get_attachment_image_src(  get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
    ?>
    
    <div class="featured-image" style="background: url('<?php 
    	if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    		echo esc_url( $src[0] );
    	} 
    	else echo "";
    ?>')">
    
    	<header class="entry-header">
    		<a href="<?php the_permalink(); ?>">
    			<p><?php the_date('F j, Y'); ?></p>
    			<h1 class="entry-title" itemprop="headline">
    				<?php the_title(); ?>
    			</h1>
    		</a>
    	</header>
    
    </div>
    January 6, 2015 at 8:07 pm #136237
    Brad Dalton
    Participant

    Link to the source of the code please.


    Tutorials for StudioPress Themes & WooCommerce.

  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble