php


How to get featured image from post


Currently working on a blog site using WordPress.
what I'm trying to do is I want to call a functions when a post is publish for a first time not on update of post and try to retrieve featured image url that is attached to that post.
I want to know which hook I have to call and how to do that. Do I have to add plugins?.
save_post hook can be used for this purpose it has a third parameter
$update which can be used to determine just to check it is new or
existing.
/**
* Save post metadata when a post is saved.
*
* #param int $post_id The post ID.
* #param post $post The post object.
* #param bool $update Whether this is an existing post being updated or not.
*/
function save_post_first_time($post_id, $post, $update)
{
//check for revision
if (wp_is_post_revision($post_id))
return;
if (!$update)
{
// it's a new post
//getting post featured image ID
$post_thumbnail_id = get_post_thumbnail_id($post_id);
$imageSRC = wp_get_attachment_image_src($post_thumbnail_id, 'thumbnail');
}
else
{
// it's an existing post
}
}
add_action('save_post', 'save_post_first_time', 10, 3);
Reference:
save_post
the_post_thumbnail_url
get_post_thumbnail_id
Hope this helps!
you can use
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
if you need the post featued image URL use the below code
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" alt="" />
If you need to change the size, replace "single-post-thumbnail" with thumbnail, medium, large, full

Related Links

syntax between inside syntax like in php(laravel)
php glob not returning all files
Showing fields from a query in php
How to add Different type of language pack for whole website
Android - 403 Forbidden on Form Submission
Exclude folder from 301 redirect Wordpress Multisite
parse error unexpected function T_STRING expecting ( [duplicate]
Is it possible to save php values from a form for further use?
disk_free_space and df, huge difference in output
How to generate Tests in tests/ directory in PhpStorm?
get woocommerce order id and amount for pixel
how to display image which is in my local server , and it need to be display in the mail content
Wordpress installation shows in plain html
PHP prevent changing url slug
How to ensure user is authenticated before he publishes an article in Laravel in the below scenario?
Authentication issues with the Twitter API

Categories

HOME
apache-nifi
mc
class
opencart
weblogic12c
gaming
functional-programming
yaml
seedstack
applescript
enums
tcsh
plaid
openlayers-3
out-of-memory
slider
websocket
tibco
position
specifications
gtk
nsstring
bundler
jpa-2.0
asp.net-core-1.0
fresco
l20n
asciimath
nose
coroutine
significance
pst
multi-upload
amazon-sns
acl
ratio
managed-c++
tortoisemerge
password-generator
tracing
python-appium
acrobat
pyspark-sql
sendmessage
fusion
mdns
windowsiot
argv
appcode
nest
gnu-classpath
bean-validation
pdfminer
landscape
eclipse-gmf
coverage.py
dbcc
transparency
sendinput
type-inference
xcglogger
cognos-tm1
d3v4
elastic4s
e
s-function
data-management
callouts
raw-sockets
noclassdeffounderror
highlighting
verisign
grinder
perceptron
self
mousehover
srv-record
tidyr
fasterxml
visual-studio-6
relationships
aho-corasick
senchatouch-2.4
nodeload
virtual-channel
pacman
delphi-xe4
icenium
gprof
lynx
daap
msinfo32
representation
tablet-pc
css-friendly
activestate
fgetc
iphone-sdk-4.3
oracle-pro-c
stretchblt

Resources

Encrypt Message