Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Show attachement title WordPress

  • SOLVED

This function has worked fine for me, but now I need to show the title of the image instead of the title of the post.
<a class="full-image" href="<?php echo $full_attachment[0]; ?>" rel="colorbox-popup" title="<?php the_title(); ?>">view larger images</a>
<?php
}
}

Below is the full function
function my_attachment_galleries($postid=0, $size='thumbnail', $attributes='') {
if ($postid<1) $postid = get_the_ID();
if ($images = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'numberposts' => 0,
'post_mime_type' => 'image',)))

foreach($images as $image) {

$attachment=wp_get_attachment_image_src($image->ID, $size);
$full_attachment=wp_get_attachment_image_src($image->ID, 'large');//control the attachment size

?>
<a class="full-image" href="<?php echo $full_attachment[0]; ?>" rel="colorbox-popup" title="<?php the_title(); ?>">view larger images</a>
<?php
}
}

Answers (4)

2011-02-05

Roberto Mas answers:

/**
* Retrieves the attachment data such as Title, Caption, Alt Text, Description
* @param int $post_id the ID of the Post, Page, or Custom Post Type
* @param String $size The desired image size, e.g. thumbnail, medium, large, full, or a custom size
* @return stdClass If there is only one result, this method returns a generic
* stdClass object representing each of the image's properties, and an array if otherwise.
*/
function getImageAttachmentData( $post_id, $size = 'thumbnail', $count = 1 )
{
$objMeta = array();
$meta;// (stdClass)
$args = array(
'numberposts' => $count,
'post_parent' => $post_id,
'post_type' => 'attachment',
'nopaging' => false,
'post_mime_type' => 'image',
'order' => 'ASC', // change this to reverse the order
'orderby' => 'menu_order ID', // select which type of sorting
'post_status' => 'any'
);

$attachments = & get_children($args);

if( $attachments )
{
foreach( $attachments as $attachment )
{
$meta = new stdClass();
$meta->ID = $attachment->ID;
$meta->title = $attachment->post_title;
$meta->caption = $attachment->post_excerpt;
$meta->description = $attachment->post_content;
$meta->alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);

// Image properties
$props = wp_get_attachment_image_src( $attachment->ID, $size, false );

$meta->properties['url'] = $props[0];
$meta->properties['width'] = $props[1];
$meta->properties['height'] = $props[2];

$objMeta[] = $meta;
}

return ( count( $attachments ) == 1 ) ? $meta : $objMeta;
}
}
[/code]

This method encapsulates the information into a stdClass/Object with property names that reflect the actual fields in the Media Manager, e.g. when you enter the data. It was too cumbersome to refer to the results sets using the generic post_* fields.

[code]
Usage:
getImageAttachmentData( $_posts->ID, 'full' );
[/code]


Roberto Mas comments:

Baki is right

so in your case replace your code


<a class="full-image" href="<?php echo $full_attachment[0]; ?>" rel="colorbox-popup" title="<?php the_title(); ?>">view larger images</a>


by


<a class="full-image" href="<?php echo $full_attachment[0]; ?>" rel="colorbox-popup" title="<?php echo $image->post_title; ?>">view larger images</a>

2011-02-05

Michael Fields answers:

You need to use the setup_postdata() function before using template tags in the loop. Please try something like this:
<?php
function my_attachment_galleries($postid=0, $size='thumbnail', $attributes='') {
if ($postid<1) $postid = get_the_ID();
if ($images = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'numberposts' => 0,
'post_mime_type' => 'image',)))
global $post;
$_post = $post;
foreach( $images as $post ) {
setup_postdata( $post );
$attachment = wp_get_attachment_image_src($post->ID, $size);
$full_attachment = wp_get_attachment_image_src($post->ID, 'large');//control the attachment size
?>

<a class="full-image" href="<?php echo $full_attachment[0]; ?>" rel="colorbox-popup" title="<?php the_title(); ?>">view larger images</a>
<?php
}
$post = $_post;
}


Michael Fields comments:

@Baki - it is important to always escape data before outputting it. At the very least, this should be done instead:
<?php echo esc_html( $image->post_title ); ?>

2011-02-05

Baki Goxhaj answers:

You can even get the title or content without the setup_postdata() function. This will get you the title:

<?php echo $image->post_title; ?>


Baki Goxhaj comments:

@Michael: You're right - that's best practice.

<?php echo esc_html( $image->post_title ); ?>


Rick Bible comments:

below worked
function my_attachment_galleries($postid=0, $size='thumbnail', $attributes='') {

if ($postid<1) $postid = get_the_ID();

if ($images = get_children(array(

'post_parent' => $postid,

'post_type' => 'attachment',

'order' => 'ASC',

'orderby' => 'menu_order ID',

'numberposts' => 0,

'post_mime_type' => 'image',)))

global $post;

$_post = $post;

foreach( $images as $post ) {

setup_postdata( $post );

$attachment = wp_get_attachment_image_src($post->ID, $size);

$full_attachment = wp_get_attachment_image_src($post->ID, 'large');//control the attachment size

?>



<a class="full-image" href="<?php echo $full_attachment[0]; ?>" rel="colorbox-popup" title="<?php the_title(); ?>">view larger images</a>
<?php
}
}

but when I changed
<a class="full-image" href="<?php echo $full_attachment[0]; ?>" rel="colorbox-popup" title="<?php the_title(); ?>">view larger images</a>


by


<a class="full-image" href="<?php echo $full_attachment[0]; ?>" rel="colorbox-popup" title="<?php echo $image->post_title; ?>">view larger images</a>

The info comes up blank


Baki Goxhaj comments:

You can use this:

<?php echo esc_html( $image->post_title ); ?>

with the original code your provided, or use Michaels code altogether. Cannot add my script to Michael's. Makes sense?

2011-02-05

Maurizio Pelizzone answers:

When WordPress return the post title through "the_title()" function, run some filter that can clean or alter the result.
(for example when title need some charset conversion or is active multi language plugin).

for this reasons, in my opinion, the best way (and maybe the right way) for retrive image title is apply the filter.

this my code:
$image_title = apply_filters('the_title', $image->post_title);

in your case i would replace in this way:


foreach($images as $image) {

$attachment = wp_get_attachment_image_src($image->ID, $size);
$full_attachment = wp_get_attachment_image_src($image->ID, 'large');//control the attachment size
$image_title = apply_filters('the_title', $image->post_title);

$full_link = "<a class='full-image' rel='colorbox-popup'
href='" . $full_attachment[0] ."'
title='" . $image_title . "' >view larger images</a>";

echo $full_link;
}





sorry for my bad english...
greetings
maurizio


Rick Bible comments:

Got that, thanks. Works.