I'm having a very strange issue. All I want to do is get the thumbnail url and assign it to a variable. Here is my code.
<?php /* Template for displaying content of MH Posts Large widget */ ?>
<article class="post-<?php the_ID(); ?> mh-posts-large-item">
<figure class="mh-posts-large-thumb">
<?php
$form_image = 'blank';
if (has_post_thumbnail()) {
$form_image = the_post_thumbnail_url('mh-magazine-lite-content');
?>
Basically, if the post has a thumbnail I want to store the actual URL of the thumbnail used in that variable for later use. However, instead of doing that it just prints the URL on screen and doesn't actually seem to put it in the variable.
I don't understand why and I would definitely appreciate any help! :)
Looking at the official documentation:
function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
$url = get_the_post_thumbnail_url( null, $size );
if ( $url ) {
echo esc_url( $url );
}
}
So the_post_thumbnail_url only outputs the URL it gets from get_the_post_thumbnail_url and doesn't return anything. Thus the solution is to use get_the_post_thumbnail_url directly.
Related
I want to re-title my image's alt tags etc based on the page they are being displayed on. For SEO Purposes.
I've tried to write a PHP function to do so and call the function on page load with "alt=<?php echo $post_title ?>" and "title=<?php echo $post_title ?>" but nothing is working.
How would you do it? PHP or jQuery... or?
Here's one of the functions I tried to manipulate and turn into something that works... I did get it to change the alt tags but I havent had any success beyond that.
function isa_add_img_title( $attr, $attachment = null ) {
$img_title = trim( strip_tags( $attachment->post_title ) );
$attr['title'] = $img_title;
$attr['alt'] = $img_title;
return $attr;
}
add_filter('wp_get_attachment_image_attributes','isa_add_img_title', 10, 2);
It appears this post may have had the answer but the link in the answer is now broken:
Dynamically filling image alt tags with the page title, PHP
Thanks so much! Appreciate any help.
This will set the alt and title of every image on the page to the post title.
function duck_diver_add_img_title( $attr, $attachment = null ) {
// Fetch the global post object. This is what the page it's on is.
global $post;
// Get the post title.
$img_title = wp_kses_post( $post->post_title );
$attr['title'] = $img_title;
$attr['alt'] = $img_title;
return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'duck_diver_add_img_title', 10, 2 );
<!DOCTYPE html>
<?php
$test ="Test";
?>
<html>
<head>
<title><?php echo "$test" ?></title>
</head>
<body>
<div class="center">
<img src="ribbn2.jpg" alt=<?php echo "$test" ?> width="100%" height="100%">
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' );
Short story:
I'm trying to get the ID of the Header Image in Wordpress.
All I found was this guide, which dosn't seem to work anymore:http://nickohrn.com/2013/09/get-attachment-id-wordpress-header-image/
Long Story
I'm trying to make the WP Header responsive with an srcset. so I don't want to use this code
<img id="masthead-bg" src="<?php header_image() ?>" alt="">
...but instead want to use the wp_get_attachment_image_srcset function to get the srcset of my header image. Only problem: I need an Image ID for this function -> The ID of my Header image.
<img id="masthead-bg"
src="<?php header_image() ?>"
srcset="<?php echo wp_get_attachment_image_srcset( image_id(), 'thumbnail' ); ?>"
sizes="100vw" alt="">
Any suggestions?
Try this...
// Get the header image data
$data = get_object_vars(get_theme_mod('header_image_data'));
// Now check to see if there is an id
$attachment_id = is_array($data) && isset($data['attachment_id']) ? $data['attachment_id'] : false;
if($attachment_id) {
// Put your image code here, user whatever function to get image by id you need
}
Note: if you use a proper WordPress function to get the image it should add in all the srcset etc stuff for you, to allow for responsive images.
To answer the original question, I've found the simplest way to get the ID while I was filtering the markup that gets outputted by <?php the_header_image_tag(); ?> (introduced in v4.4).
function header_img_markup( $html, $header, $attr) {
// we can get the image ID by passing its src url to this method
$header_img_id = attachment_url_to_postid($attr['src']);
// now we can get its metadata from the db
$header_img_data = wp_get_attachment_metadata($header_img_id);
// now we can use the data
$customSizeWidth = $header_img_data['sizes']['my-custom-size']['width'];
// ...your custom output here...
return $html;
}
add_filter('get_header_image_tag', 'header_img_markup', 20, 3);
Responsive Wordpress Header Image with fallback:
if (get_header_image() !== '') {
$attachment_id = attachment_url_to_postid(get_header_image());
echo wp_get_attachment_image($attachment_id, 'large');
}
if (get_header_image() == '') {
echo '<h1>'.get_bloginfo( "name" ).'</h1>';
echo '<h2>'.get_bloginfo( "description" ).'</h2>';
}
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 am trying to get the post thumbnail using post_id,but i am getting so many problems.
Iam calling the function in a separate php file in theme directory
echo get_the_post_thumbnail('637');
Fatal error: Call to undefined function get_the_post_thumbnail() in ...
1)can we get the thumbnail using post_id
or
2)can we get the image source using post_id
please any body help me
Thanks in advance
Try this
global $post;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'post');
echo $thumb[0];
In your case you make a small mistake that you put the single quote inside the function when function require an integer value.
echo get_the_post_thumbnail('637');
Bellow code are valid try it.
Simple Form
echo get_the_post_thumbnail(637);
Size Specified Form where second argument is the size of the image.
echo get_the_post_thumbnail(637, array(100,100));
also you can try bellow code also
get_the_post_thumbnail(637); // without parameter -> Thumbnail
get_the_post_thumbnail(637, 'thumbnail'); // Thumbnail
get_the_post_thumbnail(637, 'medium'); // Medium resolution
get_the_post_thumbnail(637, 'large'); // Large resolution
get_the_post_thumbnail(637, 'full'); // Original resolution
Also you can refer to the WordPress codex Here.
I am also going to write a full post on this topic on my blog
Use Require_once Or include_once
require_once('/the/path/to/your/wp-blog-header.php');
include_once('wp-blog-header.php' );
get_the_post_thumbnail($post_id); // without parameter -> Thumbnail
get_the_post_thumbnail($post_id, 'thumbnail'); // Thumbnail
get_the_post_thumbnail($post_id, 'medium'); // Medium resolution
get_the_post_thumbnail($post_id, 'large'); // Large resolution
get_the_post_thumbnail($post_id, 'full'); // Original resolution
get_the_post_thumbnail($post_id, array(100,100) ); // Other resolutions
Out side of loop
global $post;
if (has_post_thumbnail( $post->ID ) ){
//
get_the_post_thumbnail($post->ID);
//
}
Vallabh's solution works. This is how I use it as a background image:
<?php if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id(637), 'thumbnail' );
$image = $image[0];
} ?>
<div style="background-image: url(<?php echo $image; ?>)"> ... </div>
Create a post template..look like this(post_temp.php)
<?php
$args=array('order'=> 'DESC', 'posts_per_page'=>get_option('posts_per_page'));
$query=new WP_Query($args);
if( $query->have_posts()):
while( $query->have_posts()): $query->the_post();
{
echo get_the_post_thumbnail($post->ID);
}
endwhile;
else:
endif;
?>