Support

Account

Home Forums Add-ons Repeater Field REPEATER IMAGE ALT TAG

Solved

REPEATER IMAGE ALT TAG

  • My set up is using the repeater upload iamge. For my slider and i want to know how to get the title tag of that image i upload to show in the alt tag.

    I attached my code below what am i doing wrong to with the alt tag.

    <?php 
     
    if(get_field('image_slider_illustrations'))
    {
    	echo '<ul class="slider">';
     
    	while(has_sub_field('image_slider_illustrations'))
    	{
    
    		echo '<li><img src="' . get_sub_field('illustrations_image') . '"  alt="' . the_sub_field( 'alt' )  . '"/></li> ';
                   
    	}
     
    	echo '</ul>';
    }
     
    ?>
  • It depends upon how you are saving the data for the illustrations_image field.

    Have a look and see if it’s Image Object, Image URL or Image ID

    Think you will probably have to use Image ID (which is the attachment ID in the database) then use the following:

    wp_get_attachment_image_src – for the source file and dimensions
    wp_get_attachment_image_attributes – for attributes eg. title, caption, alt

    • Jamie

    • September 12, 2013 at 10:10 pm

    You’ll need to have the image set as Image ID

    then use something like:

    <?php 
    				
    	$attachment_id = get_sub_field('illustrations_image');
    	$size = "full"; // (thumbnail, medium, large, full or custom size)
     				
     	$image = wp_get_attachment_image_src( $attachment_id, $size );
    
     	$attachmentinfo = get_post( $attachment_id );
    
    	$alt = get_post_meta($attachmentinfo->ID, '_wp_attachment_image_alt', true);
    
    ?>
    <img src="<?php echo $image[0]; ?>" alt="<?php echo $alt; ?>" />

    You’ll need to edit it to go in amongst your loop but hopefully you’ll get the jist.

  • It doesn’t seem to be working. Its using a repeater to add new images for a image slider i have. Is that why its not working? And yes its set up to display image ID now.

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
      <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
      
      
    
    <?php 
    				
    	$attachment_id = get_sub_field('illustrations_image');
    	$size = "full"; // (thumbnail, medium, large, full or custom size)
     				
     	$image = wp_get_attachment_image_src( $attachment_id, $size );
    
     	$attachmentinfo = get_post( $attachment_id );
    
    	$alt = get_post_meta($attachmentinfo->ID, '_wp_attachment_image_alt', true);
    
    ?>
    
    <ul class="slider">
    <li><img src="<?php echo $image[0]; ?>" alt="<?php echo $alt; ?>" /><li>
    </ul>
    
    </div>
    • Jamie

    • September 13, 2013 at 6:44 pm

    Here’s your code with my code combined 🙂

    <?php  			
    	if(get_field('image_slider_illustrations'))
    	{
    		echo '<ul class="slider">';
    		while(has_sub_field('image_slider_illustrations'))
    		{	
    
    			$attachment_id = get_sub_field('illustrations_image');
    			$size = "full"; // (thumbnail, medium, large, full or custom size)
    			
    			$image = wp_get_attachment_image_src( $attachment_id, $size );
    
    			$attachmentinfo = get_post( $attachment_id );
    			$alt = get_post_meta($attachmentinfo->ID, '_wp_attachment_image_alt', true);
    
    			echo '<li><img src="' . $image[0] . '"  alt="' . $alt  . '"/></li> ';               
    		}	 
    		echo '</ul>';
    	}	 
    ?>

    Just tried that and got the expected result – let me know if you’ve still got no joy

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.