WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get Title, Alt, or Caption from Media Library and insert into theme (8 posts)

  1. jungledsales
    Member
    Posted 3 years ago #

    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?

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
    $image_title = $attachment->post_title;
    $caption = $attachment->post_excerpt;
    $description = $image->post_content;
  3. jungledsales
    Member
    Posted 3 years ago #

    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 } } ?>
  4. keesiemeijer
    moderator
    Posted 3 years ago #

    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 } } ?>
  5. jungledsales
    Member
    Posted 3 years ago #

    Thank you! That worked just like I wanted!

  6. waterwalk
    Member
    Posted 2 years ago #

    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?

  7. waterwalk
    Member
    Posted 2 years ago #

    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.]

  8. waterwalk
    Member
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic