WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get title from attached images (5 posts)

  1. AvWijk
    Member
    Posted 2 months ago #

    Hello,

    I'm using the following code to get the attached images from a post, only excluding the featured image.

    <?php $thumb_ID = get_post_thumbnail_id( $post->ID ); if ( $images = get_children(array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'exclude' => $thumb_ID, ))) : ?>
    
    		<?php foreach( $images as $image ) :  ?>
    
            <div class="col-sm-4 col-xs-6">
                <a class="fancybox-asset" href="<?php echo wp_get_attachment_url($image->ID, 'thumbnail-latest'); ?>" alt="Attached Image Title should be here" title="Attached Image Title should be here"></a>
            </div>
    
    		<?php endforeach; ?>
    		<?php else: // No images ?>
    		<?php endif; ?>

    What can I do to make it display the image title? (not the post title!)

  2. znuffn
    Member
    Posted 2 months ago #

    Give this a try (I haven't tested it):

    <?php 
    
    $thumb_ID = get_post_thumbnail_id($post->ID);
    
    $images = get_children
    (
    	array
    	(
    		'post_parent' => get_the_ID(),
    
    		'post_type' => 'attachment',
    
    		'post_mime_type' => 'image',
    
    		'exclude' => $thumb_ID,
    	)
    );
    
    if(!empty($images))
    {
    	foreach($images as $image)
    	{
    		$title = htmlspecialchars($image->post_title);
    
    		echo '<div class="col-sm-4 col-xs-6"><a class="fancybox-asset" href="'.wp_get_attachment_url($image->ID).'" alt="'.$title.'" title="'.$title.'"></a></div>';
    	}
    }
    
    ?>
  3. AvWijk
    Member
    Posted 2 months ago #

    I got a blank screen, code is not working. This is the full chunk of code I have now, how do I show the title?

    <div id="postcontainer-4">
    					  <?php query_posts('p=121'); while (have_posts()) : the_post(); ?>
                        <div class="col-sm-12" id="single-post post-<?php the_ID(); ?>">
                        <p><h2><?php the_title();?></h2></p>
                        <?php the_content();?>
                        </div> <!-- end: col 8 -->
    
    						   					<?php $thumb_ID = get_post_thumbnail_id( $post->ID ); if ( $images = get_children(array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'exclude' => $thumb_ID, ))) : ?>
                             								<?php foreach( $images as $image ) :  ?>
                                                             <div class="col-sm-4 col-xs-6">
    
                                 <div style="padding-bottom: 10px; padding-top: 10px;">
                                    <a class="fancybox-asset" href="<?php echo wp_get_attachment_url($image->ID, 'thumbnail-latest'); ?>" alt="<?php the_title();?>" title="TITLE GOES HERE!">
                                    <div class="circle largecircle" style="background: url(<?php echo wp_get_attachment_url($image->ID, 'thumbnail-latest'); ?>); background-size: cover; background-position: center; "><div class="circle-overlay-zoom"><i class="fa fa-search fa-2"></i></div></div>
    								</a>
                                 </div>
                                    </div>
                               								 <?php endforeach; ?><?php else: // No images ?><!-- This post has no attached images -->
    
    														 <?php endif; ?>
    
                        <?php endwhile;?>
                    </div>
  4. AvWijk
    Member
    Posted 2 months ago #

    Only the thumbnail part is important, but is part of the same post (query posts)

  5. AvWijk
    Member
    Posted 2 months ago #

    Got it! Works great in combination with my old code. Apparently this displays the title as I set it in Media Manager.

    <?php $filename = basename ( get_attached_file( $data->ID ) ); ?>
    <?php echo $filename ?>

Reply

You must log in to post.

About this Topic