My website consist of 5 inner pages, and I want to use different banner images for each page. The page is with side bar, but I want a full width banner, so I used a code which I got from wordpress and its working
this is the code..
<div class="banner">
<?php
if( is_page('About') ) $img = 'bannerAbout.jpg';
elseif( is_page('Services') ) $img = 'bannerServices.jpg';
elseif( is_page('Testimonials') ) $img = 'bannerTestimonials.jpg';
elseif( is_page('Testimonials') ) $img = 'bannerTestimonials.jpg';
elseif( is_home() ) $img = 'bannerBlog.jpg';
else $img = 'banner.jpg';?>
<img alt="" src="<?php bloginfo('stylesheet_directory'); ?>/images/<?php echo $img;?>" />
</div>
My question is, How Can I call 'Featured Image' of each page in this code? between $img=""
Or any plug-for this?
If I can call the featured image, then its easy to upload images, otherwise I need to use FTP all the time to change.
Please help me.
Thanks in Advance
Add this code to your theme's functions.php
add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );
this will enable featured image for post and page
Then in your page.php file add this code
before content and sidebar
<?php
global $post;
echo get_the_post_thumbnail($post->ID, 'full'); // visit http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail for more info
?>
Place below code in each condition
global $post
echo get_the_post_thumbnail( $post->ID,'full' );
full will take the full size of image what ever you have uploaded
Related
is there filter of some sort that can add a image if there is no featured image present when using the Elementor pro "post" element.
Because the title goes up if there is no image placed and it breaks the sites display
want to add a placeholder image like below when no featured image is available
You can add this filter to you theme functions.php :
function mlnc_filter_post_thumbnail_html( $image_placeholder ) {
// If there is no post thumbnail,
// Return a default image
if ( '' == $image_placeholder ) {
return '<img src="' . get_template_directory_uri() . '/images/default-thumbnail.png"/>';
}
// Else, return the post thumbnail
return $image_placeholder;
}
add_filter( 'post_thumbnail_html', 'mlnc_filter_post_thumbnail_html' );
Another way is to go where the image is outputting and add an If statement like below:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg"/>
<?php } ?>
I am trying to code a small custom wordpress theme. So I am overriding on the twentytwenty theme of wordpress.
Just to be clear I am talking PHP and overriding home.php, where I want to create some custom html header.
I am trying to get the url of the logo (that I (or the user, can change) using the wordpress customizer.
What I did is :
<img src="<?php echo (get_custom_logo()) ? get_custom_logo() : 'somefallback_url'; ?>" >
What is happening is :
get_custom_logo() function is returning an Image which is normal, I can't find the function that should return the URL in the codex. An error is happening i have an image inside another one.
So basically what I want is :
A PHP function that returns just the URL not the full Image tag.
For a short one-liner:
<?php echo esc_url( wp_get_attachment_image_src( get_theme_mod( 'custom_logo' ), 'full' )[0] ); ?>
The codex lies it out for you
function get_custom_logo_url()
{
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
return $image[0];
}
if you want to return only the image src URL you can use this with the simplest way.
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image_url = wp_get_attachment_image_src ( $custom_logo_id , 'full' );
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()) {
So I am trying to call an image to my main page (Home page). This image lives on a post and it is set as a featured image on that said post. So I got the post ID and I was able to display the title and content on the front page. But the featured image won't display the url. So here is the code I have on my front page:
<?php
$post_id = 53;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post_id));
echo $title;
echo $image;
echo $queried_post->post_content;
?>
It just outputs array. Thank you for the help.
Here is the solution for your problem.
$post_thumbnail_id = get_post_thumbnail_id($post_id);
$thumb_images = wp_get_attachment_url($post_thumbnail_id);
echo $thumb_images;
//Here you will get url of featured image.
This following code can help you with your problem.
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'single-post-thumbnail' ); // get array of featured image
echo $image[0]; //get url of featured image of post
I'm creating a Wordpress theme and I'd like to grab the first image in a post as a thumbnail to use in the Facebook's OG meta tag.
I tried using the function get_the_post_thumbnail() but it generates an html img element. Also I'd like to take the first image in the post, without the need of adding a featured image when creating the post.
This should be simple because there are already all thumbnails generated for every post, I'm just not getting it right.
Here I made some function for you that you can hook to add/edit attachment event.
function set_first_as_featured($attachment_ID){
$post_ID = get_post($attachment_ID)->post_parent;
if(!has_post_thumbnail($post_ID)){
set_post_thumbnail($post_ID, $attachment_ID);
}
}
add_action('add_attachment', 'set_first_as_featured');
add_action('edit_attachment', 'set_first_as_featured');
There is a lot of space for improvement, but this one works like a charm too. On every upload / edit attachment, function checks if the post already has featured image. If it has not, image in question is set as featured. Every next picture will be ignored (since post already has featured image).
Maybe someone finds it helpful (you found solution in the middle of my coding, so... :) )
I found this solution:
$size = 'thumbnail'; // whatever size you want
if ( has_post_thumbnail() ) {
the_post_thumbnail( $size );
} else {
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'numberposts' => 1)
);
foreach ( $attachments as $thumb_id => $attachment ) {
echo wp_get_attachment_image($thumb_id, $size);
}
}
I found a solution:
wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail' )[0];
It works fine.
Put this code in your theme's functions.php:
// make the first image of WordPress post as featured image
function first_image_as_featured() {
global $post, $posts;
$first_img_featured = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img_featured = $matches [1] [0];
if(empty($first_img_featured)){ //Defines a default image
$first_img_featured = "/images/default.jpg";
}
return $first_img_featured;
}
Then add the below code inside WordPress loop:
<?php
if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail(); ?>
<?php }
else { ?>
<img src="<?php echo first_image_as_featured(); ?>" />
<?php
}
?>
If the featured image not set, it will automatically take the first image as featured image.
Source: Get The First Image Of WordPress Post As Featured Image
I have tried the solutions above withou success. So, I build a new and easy solution:
function set_first_as_featured($post_id){
$medias = get_attached_media( 'image', $post_id );
if(!has_post_thumbnail($post_id)) {
foreach ($medias as $media) {
set_post_thumbnail($post_id, $media->ID);
break;
}
}
}
add_action('save_post', 'set_first_as_featured');
When you save a post, this code will check if it has any thumbnail. If not, so it will set the first image attached to this post as thumbnail.