0

How can i get image from media in a custom page with image url look like this image

How can i get image from media in a custom page with image url look like this image please help me

0

1 Answer 1

2

I believe you will need this function: wp_get_attachment_image_src(); (Here is the codex link: https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/)

All you have to do is pass in the ID of the image you wish to fetch and it will give you the URL.

A little example:

<?php
    // Getting the media attached to a post
    $post_image_url = wp_get_attachment_image_src($post->ID);

    // var_dump the variable to see what URL is returned.
    var_dump($post_image_url);

    // Using just an image ID
    $set_image_url = wp_get_attachment_image_src(19983);

    // var_dump the variable to see what URL is returned.
    var_dump($set_image_url);

    // You can also do it so it fetches a certain size of image
    $image_size_url = wp_get_attachment_image_src(19982, 'thumbnail');

    // var_dump the variable to see what URL is returned.
    var_dump($image_size_url);

?>

So in terms of getting the media to display on the custom page you can just write in an image tag that looks roughly like the following:

<img src="<?php echo wp_get_attachment_image_src(19983, 'thumbnail'); ?>" alt="" class="" />

This would go in the page template file you want it to appear on.

With a little experimentation you should be able to get it to work.

Not the answer you're looking for? Browse other questions tagged or ask your own question.