Finding attachment page URL of/from an image url - php

I'm using the below code to get all image URL's attached to a post.
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
if ($images) :
foreach ($images as $attachment_id => $image) :
$img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
if ($img_alt == '') : $img_alt = $image->post_title; endif;
$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];
endforeach; endif; }
The output I get is something like this:
https://www.example.com/wp-content/uploads/2019/01/image.gif
What I need to find is the attachment page URL for this image which will be something like this https://www.example.com/post-name/image-22
I tried using wp_get_attachment_image, but the output wasn't what I needed.
any idea how can i do that?

You are looking for get_attachment_link to return a pretty link you need to make sure your permalink structure is set to pretty links.
https://codex.wordpress.org/Function_Reference/get_attachment_link
Example from page:
<?php
$attachment_id = 1; // ID of attachment
$attachment_page = get_attachment_link( $attachment_id );
?>
<?php echo get_the_title( $attachment_id ); ?>

Related

How to return the SRC attribute of my secondary image via php

I'm using the following code in wordpress functions.php file in order to retrieve my secondary image (the non-featured one).
function haruki($post_id) {
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
if ($images) :
foreach ($images as $attachment_id => $image) :
if ( $image->ID != $thumbnail_ID ) :
$img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
if ($img_alt == '') : $img_alt = $image->post_title; endif;
$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];
echo $img_url;
endif; endforeach; endif; }
Then when I want to output the html for the image I do:
<?php haruki("$post->ID"); ?>
Now I would like to just return the src attribute, but I'm failing by doing this:
$myvariable = haruki( $post_id, false );
How am I supposed to fix this last one in order to make it work correctly?

wp returns "wrong" image ID

well,
Code dosent make mistakes so i have something i dont see here...
I have following code on wp page:
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $pages->ID
);
$images = get_posts($args);
$attachment_id = $images[0]->ID;
$i = wp_get_attachment_image_src($attachment_id, $size);
$p = array_values($i)[0];
if (has_post_thumbnail()) {
the_post_thumbnail($size);
} else {
} ?>
It, works, in a way.
it will return a image url and i can use it to show images on page, anyhow the ID it return seems to be somewhat random.
I have the right ID for page to look for images in $pages
i would need to return first image of that page.
I would assume, array[0] would be first image of page, but obviously it is not since it returns very strange pictures from another page, which has nothing to do with this page.
I'm not sure but seems you are doing it wrong. :)
Why can't you use
echo get_the_post_thumbnail( $post_id, 'thumbnail', array( 'class' => 'alignleft' ) )
or
get_the_post_thumbnail_url( int|WP_Post $post = null, string|array $size = 'post-thumbnail' )
or
get_post_thumbnail_id( $post_id );
Please Try :
global $post;
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $pages->ID
);
$images = get_posts($args);
$attachment_id = $images[0]->ID;
$i = wp_get_attachment_image_src($attachment_id, $size);
$p = array_values($i)[0];
if (has_post_thumbnail($post->ID )) {
the_post_thumbnail($size);
} else {
}

Wordpress get attachment image caption

I tried to get attachment meta caption value as mentioned here, but couldn`t get any output. Other meta arrays like [created_timestamp] or [iso] gave their values.
$img_meta = wp_get_attachment_metadata( $id );
echo $img_meta[image_meta][caption];
This issue happens to both [caption] and [title]. Any help is much appreciated.
The caption and title you are looking to get from wp_get_attachment_metadata are not the title and caption you add in WordPress they are meta data from the actual image itself. To get the WordPress data use something like this (assuming $id is the id of your image).
$image = get_post($id);
$image_title = $image->post_title;
$image_caption = $image->post_excerpt;
Since WordPress 4.6.0 there is get_the_post_thumbnail_caption($post) which gets you the caption for the specified post.
put this in your functions.php file:
function show_caption_image($type='title'){
global $post;
$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'numberposts' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;
}
}
return $type == 'title' ? $image_title : $caption.$description;
}
and below the image in your theme, or wherever you prefer to put it, usually in the single.php file:
<?php if ( has_post_thumbnail() ) :
?>
<span class="image main"><img src="<?php echo get_the_post_thumbnail_url()?>" alt="<?php echo get_the_title()?>" /><i><?php echo show_caption_image();?></i></span>
<?php endif; ?>

How can I get the image url in WordPress?

I'm using this code to get all images uploaded to a page and it's working fine but I'd like to use the image urls for a lightbox function and currently I'm getting an img object. Is there any way I can get the image url specifically? I'd like to think it's part of an array. Here's the code I'm using:
<?php
$images = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => 999 ) );
if ( $images ) {
//looping through the images
foreach ( $images as $attachment_id => $attachment ) {
?>
<li>
<a href="*the_img_url*" data-lightbox="lightbox-1" data-title="<?php echo $attachment->post_excerpt; ?>" ><?php echo wp_get_attachment_image( $attachment_id, 'full' ); ?>
</a>
</li>
<?php
}
}
?>
Thank you.
Have you tried
<?php echo wp_get_attachment_url( $attachment_id ); ?>
// $attachment_id is The ID of the desired attachment

Wordpress - Including page title in 'wp get attachment image'

Okay, I've set up a bit of code which searching for all the pages which are a child of the ID 8, then outputs all the attachments (in the gallery) of these pages as unordered list items. You can see the effect so far here http://goo.gl/eq4UF.
The problem I'm having is that I need to include the title of each page before each so you can easily identify which images come below which page. Normally I would just add this in, but the list items al use masonry and are positioned all over the page using some JS so they never appear beside the first image in the list.
I therefore will add the title of the page to every
<li> in the <ul> which will allow the title to run with each image but I don't know how to include this in the wp get attachment image function. Both the_title and wp_title doesn't work inside this loop. apply_filters( 'the_title', $attachment->post_title ); obviously takes the image title, but is there any good to take the page title?
Thanks in advance and hope this made sense,
R
<?php $postslist = get_pages('number=9999&sort_order=DESC&sort_column=post_date&child_of=8');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<ul class="main-projects-list">
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li class="each-image">';
echo wp_get_attachment_image( $attachment->ID, 'large' );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
?>
</ul>
<?php endforeach; ?>
You can try this:
<?php $postslist = get_pages('number=9999&sort_order=DESC&sort_column=post_date&child_of=8');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<ul class="main-projects-list">
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$attachments = get_posts( $args );
if ( $attachments ) {
$post_title = get_the_title($post->ID); // We get the post title
foreach ( $attachments as $attachment ) {
$img_title = apply_filters( 'the_title', $post_title . ' - ' . $attachment->post_title ); // We create the image title with the 2 strings
echo '<li class="each-image">';
echo wp_get_attachment_image( $attachment->ID, 'large' , false, array('title' => $img_title));
echo '<p>';
echo $img_title;
echo '</p></li>';
}
}
?>
</ul>
<?php endforeach; ?>

Categories