• Resolved jungledsales

    (@jungledsales)


    I have researched several blog posts for the last couple hours and just can’t figure out how to solve my issue. I have the following code that inserts all the thumbnail images from a media gallery in my media library:

    <?php
    			                    $args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => $post->ID );
    			                    $attachments = get_posts($args);
    		        	            if ($attachments) {
    			                        foreach ( $attachments as $attachment ) { ?>
    			                        <a href="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" rel="lightbox" title="<?php the_title_attribute(); ?>"><img src="<?php bloginfo('template_url'); ?>/timthumb.php?h=75&w=75&zc=1&src=<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" alt="" width="75" height="75" border="0" /></a>
    		                    <?php } } ?>

    Currently I’m just using “the_title_attribute();” which just grabs the post title for the image title. How can I grab the actual title, alt, or caption from the media library instead?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
    $image_title = $attachment->post_title;
    $caption = $attachment->post_excerpt;
    $description = $image->post_content;
    Thread Starter jungledsales

    (@jungledsales)

    That looks good, but I must not be coding it right, because that didn’t work for me. So would I add that like this?

    <?php
    	$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'numberposts' => null, 'post_parent' => $post->ID );
    	$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
    	$image_title = $attachment->post_title;
    	$caption = $attachment->post_excerpt;
    	$description = $image->post_content;
    	$attachments = get_posts($args);
    	if ($attachments) {
    		foreach ( $attachments as $attachment ) { ?>
    		<a href="<?php echo wp_get_attachment_url( $attachment->ID, false ); ?>" rel="lightbox" title="<?php echo $image_title; ?>"><img src="<?php bloginfo('template_url'); ?>/timthumb.php?h=75&w=75&zc=1&src=<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" alt="" width="75" height="75" border="0" /></a>
     <?php } } ?>
    Moderator keesiemeijer

    (@keesiemeijer)

    try it with this:

    <?php
    	$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;
    ?>
    		<a href="<?php echo wp_get_attachment_url( $attachment->ID); ?>" rel="lightbox" title="<?php echo $image_title; ?>"><img src="<?php echo get_bloginfo('template_directory'); ?>/timthumb.php?h=75&w=75&zc=1&src=<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" alt="<?php echo $alt; ?>" width="75" height="75" border="0" /></a>
     <?php } } ?>

    Thread Starter jungledsales

    (@jungledsales)

    Thank you! That worked just like I wanted!

    Hi, I had been looking for a solution to show captions with a slideshow for literally days. And yours is the first one to work. Thank you so much. I have the captions working fine now BUT I have one other issue.

    keesiemeijer, I hope you’res still on these forums. I’ll buy you a starbucks gift card if this last little thing can get solved. I am using your code in a slideshow and it’s working great to show the captions on those slides. However, now the thumbnails below are not switching to the new picture when clicked. The strange thing is that everything else is getting switched. So if I click on a thumbnail below the slider and then click on the slider, the information for that slide is for the thumbnail but the image is still the original one. I feel like this may be just a misplaced “}” or something but can’t figure it out. If you have any suggestions, I’d appreciate it.

    Here is your code for the slider portion (where the main slider resides with the featured image):

    [No bumping. If it’s that urgent, consider hiring someone.]
    Any ideas?

    Sorry about that, the code pasted weird. Let me try again. For the actual slider portion (which is working to show correct captions on images when clicked):

    [No bumping. If it’s that urgent, consider hiring someone.]

    I’m confused. Why has my post been changed to show a ‘no bumping’ tag instead. Did I do something incorrectly?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Get Title, Alt, or Caption from Media Library and insert into theme’ is closed to new replies.