Display Attachment URL in single.php in WordPress - wordpress

i've been trying to get the attachment URL, so far this code gets the DIRECT LINK to the image ;
<?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 '<a target="_blank" href="' . wp_get_attachment_url( $attachment->ID ) . '">Download Full Size</a>';
}
?>
but instead i want the attachment URL, please help...

I think you are doing it wrong way, Have you stored URL of upload file in DB ?
If no then you can't.
Use custom fields to store and use them.
If you want to it without coding use this plugin http://wordpress.org/extend/plugins/types/

You just need to use get_attachment_link instead of wp_get_attachement_url

Related

Thumbnail url returns at the top of the page

I wrote this simple shortcode to return my custom blog grid and I wanted to insert post featured image as the background image for post tile.
Everything works quite good except background image part. I cannot understand why image URL returns at the top of page and style attribute is empty it returns just `background-image url('');
Screenshot below:
function gita_blog_grid( $atts ) {
extract( shortcode_atts( array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'perpage' => 3
), $atts ) );
$output = '<div class="posts_wrapper"><div class="gita_posts_row">';
$args = array(
'post_type' => $post_type,
'orderby' => $orderby,
'order' => $order,
'posts_per_page' => $perpage
);
$gita_query = new WP_Query ( $args );
while($gita_query->have_posts()) : $gita_query->the_post();
if(has_post_thumbnail()) {
$post_thumb = the_post_thumbnail_url('full');
}
$output .= '<article class="gita_single_post" style="background-image: url(\''. $post_thumb . ' \')">';
$output .= '<h3 class="gita_post_title">' . get_the_title() . '</h3>';
$output .= '</article></div></div>';
endwhile;
wp_reset_query();
return $output;
}
add_shortcode('gita_blog', 'gita_blog_grid');
the_post_thumbnail_url displays the url directly to the page as soon as its called. In your case it is displaying the URL string directly to the screen as soon as its called, which is before you finish generating your output processing and returning it to be displayed.
Instead of displaying it directly, you want to get it as a variable so you need to use get_the_post_thumbnail_url i.e.
if(has_post_thumbnail()) {
$post_thumb = get_the_post_thumbnail_url(get_the_ID(), 'full');
}
Ref:
Codex for the_post_thumbnail_url
WP Developer Resource for get_the_post_thumbnail_url

Display attached images only for current post in wordpress

I want to display my post text and images separately, like in two different columns.
I followed dew tricks found online. Unfortunatly, they work but not the way I want them to. It's displaying all the images present in media instead of the images which I inserted in the current post. I found the following snippet in almost every solution related to this topic.
Code:
<?php
$args = array( 'post_type' => 'attachment', 'post_status' => 'any', 'post_parent' => $post->ID );
$attachments = get_post($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo '<div class="col-md-4 col-sm-4 col-xs-6">';
the_attachment_link( $attachment->ID , flase );
echo '</div>';
}
}
wp_reset_postdata();
?>
Try the following:
$images = get_attached_media('image');
You can then use your existing foreach loop

Wordpress How to remove the relation of my post with their uploaded files

Hi I´m trying to know if there are someway to remove the relation of the uploaded files with a post WITHOUT deleted the files, when you upload a file and inserted into the post. For example a pdf file. you will get a link inserted into the post. what I want to do is if I remove this link from the post. Delete the reference of this file from the post in the database.
The problem I'm facing is that I'm using a function wich return all the pdf files uploaded into a custom type of post.
function getPdfList(){
global $post, $posts;
$list = array();
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$ext = pathinfo($attachment->guid, PATHINFO_EXTENSION);
if("pdf" == strtolower($ext)){
$list[] = $attachment;
}
}
}
return $list;
}
So then I'm doing this in my php file
$args = array( 'post_type' => 'fuerzabasica', 'posts_per_page' => 40 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$pdfs = getPdfList();
echo '<div class="entry-content" style="">';
//the_content();
foreach ($pdfs as $pdf) {
echo $pdf->guid."<br />";
}
echo '</div>';
endwhile;
?>
The problem is that I'm still getting files wich I remove the link from the post, so if my USER upload new files he will get the old files(which the link was removed) and the new one. Is a way to delete the reference of this files of my post??
WordPress will maintain a parent/child relationship between posts and uploaded files. To prevent this you will have to make sure the post_parent value of every attachment is 0 instead of the parent posts ID.
You could accomplish this with a save_post filter, in which you'll loop through any attachments returned by get_posts(array('post_type' => 'attachment', 'post_parent' => $post_id, 'posts_per_page' => -1)); setting post_parent to 0. $post_id is the first parameter of your filter function.

How do i show wordpress attachments from current post?

So with my blog i have a photo attachment page but it only shows to photo's at a time, and those two photo's are used as the navigation and i hate that.
I want the attachment page to show all the photo's that goes along with the rest of that set.
Here is the current code
<div id="nav-images" class="navigation clearfix">
<div class="nav-next"><?php next_image_link() ?></div>
<div class="nav-previous"><?php previous_image_link() ?></div>
How do i change that to show all the post attachments?
To clarify, this doesn't work anymore - at least with version 3.5.2. I used this instead;
$attachments = get_children(
array(
'post_type' => 'attachment',
'post_parent' => get_the_ID()
)
);
foreach ($attachments as $attachment) {
// ...
}
Only resurrecting an old thread because this one ranks quite highly for this search term.
When you're on a page or post, you can get all of its attachments with the following:
global $post; // refers to the post or parent being displayed
$attachements = query_posts(
array(
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->ID, // only get attachments for current post/page
'posts_per_page' => -1 // get all attachments
)
);
foreach($attachements as $attachment){
// Do something exceedingly fancy
}
Since you're currently on an attachment page, you can get all the other attachments using the $post->post_parent value:
global $post; // refers to the attachement object
$attachements = query_posts(
array (
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->post_parent, // attachments on the same page or post
'posts_per_page' => -1 // get all attachments
)
);
To then display the attachment images, you can use the wp_get_attachment_image_src function. The attachment's ID will be available in each iteration of your foreach loop as $attachement->ID (if you use the same naming convention as my first example).
Since WordPress 3.6.0 you can also use get_attached_media.
$media = get_attached_media( 'image', $post->ID );
if(! empty($media)){
foreach($media as $media_id => $media_file){
$thumbnail = wp_get_attachment_image_src ( $media_id, 'thumbnail' );
$full = wp_get_attachment_url( $media_id );
echo '<img src="'.$thumbnail[0].'" alt="'.$media_file->post_title.'" />';
}
}

Automatic in page gallery wordpress

I'm looking for a simple image gallery for a client of mine.
I need to have the ability to upload multiple images from within a post or page and have the images automatically added to the page, and styled.
I was able to fine this http://wordpress.org/support/topic/post-image-4
Which sounds similar to what I'm looking for, but I can't seem to get it to work properly.
Does anyone know how I could go about this?
figured out.
Just need to add this to functions.php
<?php
function postimage($size=medium,$num=1,$lighbox=1) {
if ( $images = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => $num,
'order' => 'ASC',
'orderby' => 'ID',
'post_mime_type' => 'image',)))
{
foreach( $images as $image ) {
$attachmenturl=wp_get_attachment_url($image->ID);
$attachmentimage=wp_get_attachment_image($image->ID, $size );
$img_title = $image->post_title;
$img_desc = $image->post_excerpt;
if ($size != "full"){
echo ''.$attachmentimage.''.$img_title.'';
} else {
echo '<img src="'.$attachmenturl.'">';
}
}
} else {
echo "No Image";
}
}
?>
Then add this to the post page (in my case single.php)
<?php postimage('thumbnail'); ?>
Now any images uploaded in a particular post are automatically added.
try nextgen gallery :) http://wordpress.org/extend/plugins/nextgen-gallery/

Resources