MichaelMHL
MichaelMHL

Reputation: 47

Getting image from custom field not default header image (featured image)

How would it be possible to revise the following script, so that an image from a created custom field is displayed, rather than from the default header image (which is set by a featured image)? Could I do something like this and make it work: ($post->ID, 'header-image', true)?

I've tried to setup a custom field called header-image but would like guidance on how to properly script it.

<?php
if ( has_post_thumbnail() && ! post_password_required() ) :
    $featuredimage = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'resonar-large' );
?>
<div class="entry-header-background" style="background-image:url(<?php echo esc_url( $featuredimage[0]

ATTEMPTED REVISION

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
 <?php
    if ( has_post_thumbnail() && ! post_password_required() ) :
$custom_field = get_post_meta($post_id, 'header-image', true);
?>
<div class="entry-header-background" style="">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img  src="http://www.example.com/images/<?php echo get_post_meta($post- >ID, 'header-image', true); ?>" alt="Icon for Post #<?php the_ID(); ?>" /></a> 

Upvotes: 0

Views: 74

Answers (1)

Hareesh Sivasubramanian
Hareesh Sivasubramanian

Reputation: 1260

You could use the get_post_meta() functio to retrieve the value stored in the custom field.

$custom_field = get_post_meta($post_id, 'header-image', true);
?>
<div class="entry-header-background" style="background-image:url(<?php echo esc_url( $custom_field [0]

Upvotes: 1

Related Questions