Wrapping WordPress Latest Post Link - php

I'm trying to wrap a div with a link to the latest post. Currently, the code returns the value of the featured image to use as the background image for a continuing div. I just want to wrap it with the url for the latest post. Thanks
<?php
/* 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>
';
}

Use get_the_permalink like so
/* If Featured Image Set */
if ( has_post_thumbnail($recent_post[0]['ID']) ){
$image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_post[0]['ID']), 'full');
echo '<a href="' . get_the_permalink($recent_post[0]['ID']) . '">
<div class="featured-image-div" style="background-image:url('.$image[0].');"></div></a>
';
}
Function documentation: https://developer.wordpress.org/reference/functions/get_the_permalink/

Related

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: Change Post Featured Image alt tags to the given alt tag while uploading

I am giving alt tags for an image while uploading, but it is not taking the given image alt tags , it shows blank
i tried to add this filter but still not working
/* Register callback function for post_thumbnail_html filter hook */
add_filter( 'post_thumbnail_html', 'meks_post_thumbnail_alt_change', 10, 5 );
/* Function which will replace alt atribute to post title */
function meks_post_thumbnail_alt_change( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$post_title = get_the_title();
$html = preg_replace( '/(alt=")(.*?)(")/i', '$1'.esc_attr( $post_title ).'$3', $html );
return $html;
}
The post content is displayed from loop-single.php file
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
How to use those alt tags for my post featured image
Try this it worked for me :)
function change_post_thumbnail_html($html, $post_id, $post_thumbnail_id, $size, $attr) {
$id = get_post_thumbnail_id();
$src = wp_get_attachment_image_src($id, $size);
$alt = get_the_title($id); // gets the post thumbnail title
$class = $attr['class'];
$html = '<img src="' . $src[0] . '" alt="' . $alt . '" class="' . $class . '" />';
return $html;
}
add_filter('post_thumbnail_html', 'change_post_thumbnail_html', 99, 5);
Well i found out the error , it seems previous developer added the image manually to the post and featured image was also set . so i got confused.
i simply put the alt text in edit mode of the post directly.

I can't retrieve featured image url through wordpress

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

Get a single specific image from Wordpress Media Library

I've uploaded images to Wordpress Media Library.
I understand that I can view am image then get the URL for that specific image and then use the img html tag to display this on the page.
This however doesn't get the alt, title, caption and description of the image.
The img is not attached to a post or page field and so i assume you cannot use the Get Attachment function etc.
The reason I want to use a function instead of writing out a static img html code is so that they are cached better and easier to maintain with all data for the image been updated in the Media Library instead of having to edit html code which is not idea for the end user.
thank you in advance.
I presume you have an attachment ID? Have you tried using attachement functions?
From the codex:
Note that media items are also 'Posts' in their own right and can be
displayed as such via the WordPress Template Hierarchy. Themes can
make use of this to loop over media items or create galleries.
The following functions should get you started:
you can retrieve the image src using: wp_get_attachment_image_src()
$img= wp_get_attachment_image_src($attachmentID, $imageSizeName);
you can get the image caption using: get_post_field()
get_post_field('post_excerpt', $attachmentID)
you can get the alt tag using: get_post_meta()
get_post_meta($attachmentID, '_wp_attachment_image_alt', true);
first get image
function get_images_from_media_library() {
$args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => 5,
'orderby' => 'rand'
);
$query_images = new WP_Query( $args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= $image->guid;
}
return $images;
}
and display image
function display_images_from_media_library() {
$imgs = get_images_from_media_library();
$html = '<div id="media-gallery">';
foreach($imgs as $img) {
$html .= '<img src="' . $img . '" alt="" />';
}
$html .= '</div>';
return $html;
}
and use php fire event
<?php echo display_images_from_media_library(); ?>
or use this function
<?php
if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
)));
foreach ($attachments as $attachment) {
echo wp_get_attachment_link( $attachment->ID, '' , true, false, 'Link to image attachment' );
}
?>
Please try to below code:
<?php
$attachmentID = 1875;
$imageSizeName = "thumbnail";
$img = wp_get_attachment_image_src($attachmentID, $imageSizeName);
//print_r($img);
?>
<img src="<?php echo $img[0]; ?>" alt="image">
So many complicated and to my opinion wrong answers while the answer is very straight forward:
<?php
$url_to_my_attachment = "http://example.com/wp-content/uploads/image.png";
$attachment_id = attachment_url_to_postid($url_to_my_attachment);
print wp_get_attachment_image($attachment_id);
For more information have a look at wp_get_attachment_image
Note: this renders a "responsive image" where the alt attribute contains the alt data of the attachment. Thus this answer doesn't completely satisfy the OP's request which also demands to include the title, description and caption fields. See the accepted answer or other answers on how to include these other fields.

How can I get an automatic thumbnail from the first image in a Wordpress 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.

Categories