Add a background image via a custom field and no theme hacking.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'the_content', function( $content ){ | |
// Just pretend you have a custom metabox that saves the ID for you. | |
$image_id = get_post_meta( 'custom_image', $post->ID ); | |
$image_src = ''; // instantiate variable | |
if ( !empty( $image_id ) ){ | |
$image_src = wp_get_attachment_image_src( $image_id, 'large'); | |
} | |
if ( !empty( $image_src ) ){ | |
return "<div class='has-bg-image' style='background-image: url(\" {$image_src} \" );'>" . $content . "</div>"; | |
} | |
// fallback for if there is no image | |
return $content; | |
}, 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment