Sanjeev Kumar
Sanjeev Kumar

Reputation: 3163

How to get image source instead of image tag in wordpress

I am new to wordpress and trying to change the code so I can get image source so I could use it in css as background but its generating image

This is the code which is already written in funtions.php

$string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . get_the_post_thumbnail($post_id, array( 700, 500) ) .'</a></p></div>';

after reviewing many post regarding this, I found I can get the image url only by the following code

$string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . wp_get_attachment_image_src() .'</a></p></div>';

Update this code in my code wp_get_attachment_image_src() but nothing generated in result.

Can somebody please suggest what should I try?

Upvotes: 1

Views: 419

Answers (2)

Ionut Necula
Ionut Necula

Reputation: 11472

You could use get_the_post_thumbnail_url():

$string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . get_the_post_thumbnail_url($post_id, array( 700, 500) ) .'</a></p></div>';

If you want to use wp_get_attachment_image_src(), you also need to pass the post id:

wp_get_attachment_image_src($post_id)

$string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . wp_get_attachment_image_src($post_id) .'</a></p></div>';

You can read the documentation about wp_get_attachment_image_src here where you can see that the first argument passed to this function is the id and it is required, not optional, that's why it return nothing like you used it.

Upvotes: 1

sameera lakshitha
sameera lakshitha

Reputation: 1971

<?php get_image_tag( $id, $alt, $title, $align, $size ); ?>

Upvotes: 0

Related Questions