Set Background Image from Custom Meta in WordPress - php

I'm trying to get the home page of a WordPress theme I am building to change the background image on refresh based on 3 potential background images.
Each image is considered part of a larger "case study" that has a title, link, text, etc.
The case study fields are created through custom meta boxes on the home page. (I am using this to simplify the meta box creation process: https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress).
Long story short, the client wants 3 case studies on the home page to have corresponding background images. This is why I didn't use the featured image functionality, as each background goes with specific meta data.
The problem is, I can't figure out how to get the meta ID of the chosen background image and use that to set the background CSS.
Here is what I have so far:
/**
* Home Page Case Randomizer
*/
$GLOBALS['_home_case_number'] = rand(1, 3);
function home_case_number() {
if ( is_front_page() ) : // checks whether this is the home page
// get the meta image
$attachment_image = get_post_meta( get_the_ID(), '_home_case_background_' . $_home_case_number, true );
This is where I get confused. The above returns the appropriate image.jpg file. How do I massage this to work with the below code?
The below code is adapted from http://s2webpress.com/responsive-featured-image-function-in-wordpress-themes/, and I'd like to use it to make sure I'm serving images responsively.
// store the image sizes in an array
$img_sizes = array( 'thumbnail', 'medium', 'large', 'full' );
// grab the URL for each image size and store in a variable
foreach ( $img_sizes as $img_size ) {
${ 'img_src_' . $img_size } = wp_get_attachment_image_src( $attachment_image, $img_size );
}
echo '<style type="text/css">
.featured-image {
width: 100%;
min-height:350px;
background-image: url(' . esc_url( $img_src_medium[0] ) . ');
}
#media screen and ( min-width: 400px ) {
.featured-image {
background-image: url(' . esc_url( $img_src_large[0] ) . ');
}
}
#media screen and ( min-width: 1000px ) {
.featured-image {
background-image: url(' . esc_url( $img_src_full[0] ) . ');
}
}
</style>';
endif;
};
add_action( 'wp_head', 'home_case_number' );
So do I go about this by getting the meta ID of the background image? Or the attachment ID? I don't know how to do either at this point.
Any help is much appreciated!

I figured it out!
1) The CMB "plugin" saves the attachment ID as as another meta key by adding "_id" to the end of it, so I used that to get the attachment ID, and
2) I had variable scope issues. As soon as I called my global variable within my function, things started working. Here's the final code:
/**
* Home Page Case Randomizer
*/
$GLOBALS['_home_case_number'] = rand(1, 3);
function home_case_number() {
global $_home_case_number;
if ( is_front_page() ) : // checks whether this is the home page
// get the attachment thumbnail ID
$attachment_image = get_post_meta( get_the_ID(), '_home_case_background_' . $_home_case_number . '_id', true );
// store the image sizes in an array
$img_sizes = array( 'thumbnail', 'medium', 'large', 'full' );
// grab the URL for each image size and store in a variable
foreach ( $img_sizes as $img_size ) {
${ 'img_src_' . $img_size } = wp_get_attachment_image_src( $attachment_image, $img_size );
}
echo '<style type="text/css">
.featured-image {
width: 100%;
min-height:350px;
background-image: url(' . esc_url( $img_src_medium[0] ) . ');
}
#media screen and ( min-width: 400px ) {
.featured-image {
background-image: url(' . esc_url( $img_src_large[0] ) . ');
}
}
#media screen and ( min-width: 1000px ) {
.featured-image {
background-image: url(' . esc_url( $img_src_full[0] ) . ');
}
}
</style>';
endif;
};
add_action( 'wp_head', 'home_case_number' );
Hopefully this will help someone else out there.

Related

Add icon on top of featured image based on article tag

I'm building a website with some articles accessible only for Members. All articles, accessible or not are shown on a page and i would like to add an icon on top of the featured image for people to understand which articles they can see for free or not.
Could you help ? I've already read the doc about featured image but my brain can't make the whole thing...
Here's what i already tried :
function article_reserve( $title ) {
if ( has_tag( 'abonne' ) ) {
$title = 'test ' . $title;
}
return $title;
}
add_filter( 'the_title', 'article_reserve' );
It change the title but i'm trying to change the featured image
Also tried that after comments :
function wpse_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
// Optionally add any logic here for determining what markup to output.
// $html will be an empty string if there is no post thumbnail.
if ( has_tag( 'abonne' ) ) {
$new_html = '<img src="https://placekitten.com/g/600/600" alt="kitten">';
}
return $new_html;
}
add_action( 'post_thumbnail_html', 'wpse_post_thumbnail_html', 10, 5 );

Using WordPress Featured Image as CSS Background Image

I'm trying to use the latest post featured image as a background image for a div on my homepage. Can anyone help with the php?
wp_get_recent_posts() will return a single, most recently published post using the parameters below.
I recommend placing the style in a stylesheet.
/* Get Recent Post */
$recent_post = wp_get_recent_posts(array(
'numberposts' => 1,
'post_status' => 'publish'
));
/* If Featured Image Set */
if ( has_post_thumbnail($recent_post[0]['ID']) ){
/* Get Image */
$image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_post[0]['ID']), 'full');
/* Output Div with Image Set Inline, Use padding Top for Responsive Ratio Size */
echo '
<div class="featured-image-div" style="background-image:url('.$image[0].');"></div>
<style>
.featured-image-div{
padding-top: calc( 450 / 800 * 100% );
background-size: contain;
background-repeat: no-repeat;
}
</stle>
';
}

Wordpress Genesis theme display a custom header/background image using featured image

I am building a site using the Genesis framework. I ran into a problem with adding a custom header to each page and post using the featured image link in the admin area. Right now, the featured image header won't show on the page I assign to be the "Blog" page under the "Reading" settings.
Here is the site URL, http://ajcustomfinishes.starfireclients.com/.
Here's the function I am using:
// Create new image size for our hero image
add_image_size( 'hero-image', 1400, 400, TRUE ); // creates a hero image size
// Hook after header area
add_action( 'genesis_after_header', 'bw_hero_image' );
function bw_hero_image() {
// If it is a page and has a featured thumbnail, but is not the front page do the following...
if (has_post_thumbnail() && is_page() ) {
// Get hero image and save in variable called $background
$image_desktop = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'hero-image' );
$image_tablet = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'large' );
$image_mobile = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'medium' );
$bgdesktop = $image_desktop[0];
$bgtablet = $image_tablet[0];
$bgmobile = $image_mobile[0];
// You can change above-post-hero to any class you want and adjust CSS styles
$featured_class = 'above-post-hero';
?>
<div class='<?php echo $featured_class; ?>'><h1 class="custom-title">AJ Customs Finishes in Las Vegas<br>Call 702-795-7338 today!</h1></div>
<style>
<?php echo ".$featured_class "; ?> {background-image:url( <?php echo $bgmobile; ?>);height:176px;}
#media only screen and (min-width : 480px) {
<?php echo ".$featured_class "; ?> {background-image:url(<?php echo $bgtablet;?>);height:276px;}
}
#media only screen and (min-width : 992px) {
<?php echo ".$featured_class "; ?> {background-image:url(<?php echo $bgdesktop;?>);height:400px;}
}
</style>
<?php
}
}
Does anybody know how I can use the featured image to display a custom header on each page?
You have this condition in your code, which will be FALSE in case of page you set as blog in reading settings:
if (has_post_thumbnail() && is_page() ) {
is_page() will be false on blog posts home (page you set for posts in reading).
You need to set this condition as is_home() to be true i.e. you can repurpose your code to be:
if (has_post_thumbnail() && is_page() || is_home()) {

Wordpress Shortcode inside of plugin

Here is a snippet from the shortcodes.php file in one of the plugins I'm writing.
if($home_loop->have_posts()) {
switch($display) {
case "static":
while ($home_loop->have_posts()): $home_loop->the_post();
if(has_post_thumbnail() ) {
$content .= '<div class="parallax">';
$content .= get_the_post_thumbnail($post->ID, 'wrapper-wide', array('class' => "img-responsive"));
$content .= '</div>';
$content .= '<div class="container">';
$content .= get_the_content();
$content .= '</div>';
$content .= '<style>';
$content .= '.parallax{//css style here}';
$content .= '</style>;
} else {
//Something
}
endwhile;
break;
This works perfect. If I put a featured image thumbnail in my home post it will be displayed in my parallax div. The question I have is instead of placing the thumbnail inside of the div, I want to have it background-image: url(). I found this code below
<?php
$background = get_field( 'background_image' );
if($background):
?>
<style>
.main-wrapper {
background-image: url(<?php echo $background ?>);
}
</style>
<?php endif; ?>
How would I incorporate and run something like this inside of the $content? The reason I'm trying to get the image as a background-image:url() is so I can add specific css styling like background-position, size, repeat, attachment.
Why not just add the image as a background-image right inside your code? [wp_get_attachment_image_src][1] will return an array of the url, width and height of any attachment. Pass it the ID of the featured image, and gain the flexibility to use ANY size you want, not just the actual featured image size. Then place it as the background using inline CSS. It can be overwritten in your CSS sheet if needed.
($home_loop->have_posts()) {
switch($display) {
case "static":
while ($home_loop->have_posts()): $home_loop->the_post();
if(has_post_thumbnail() ) {
$post_thumnail_info = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'wrapper-wide' );
$content .= '<div class="parallax" style="background-image: url( \'' . $post_thumbnail_info[0] . '\' );">';

How to get featured image thumbnail instead of cropping automatically?

I'm working on a gallery for the homepage of this site: Warm Glow Photo
I have called the featured image at a specific size (800x600) that I have defined in functions.php. This seems to be calling
...IMAGE.jpg?resize=800%2C600
instead of the version of the featured image called
...IMAGE-800x600.jpg
I am using a plug-in for cropping thumbnails which means I need to call this image rather than cropping with ?resize.
I've found lots of info about how to call different size thumbnails but nothing that explains why it crops with ?resize instead of calling the different thumbnail itself. Any ideas on how to do this would be much appreciated.
The relevant code is:
<?php
if ( has_post_thumbnail() ) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
echo '<li><a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '">';
the_post_thumbnail( 'bones-thumb-800' );
echo '</a></li>';
}
?>
and defining bones-thumb-800 in functions.php
add_image_size( 'bones-thumb-800', 800, 600, true );
To get the plain URL of a post use this function :
function um_get_post_featured_image_src($post_id = null,$size = "full"){
if(!$post_id){
global $post;
$post_id = $post->ID;
}
if(has_post_thumbnail($post_id)){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
return $image[0];
}else{
return "";
}
}
If you have a $post globally than you don't need to pass post ID.

Categories