• Resolved phillystyle123

    (@phillystyle123)


    Hello -I’ve seen this post but if it does apply to what I’m trying to do I can’t figure it out.

    I’m trying to add alt tags to my carbon fields images. There is no image alt tag field so I’ve been trying to add echo the image meta data.

    This:

    <?php foreach ($team_types as $type_index => $type): 
    				$name = $type['title'];
    				$type_image = $type['team_type_icon'];
    				$active_class = '';
    				if ($type_index == 0) {
    					$active_class = ' class="active"';
    				}
    				?>
    				<li <?php echo $active_class; ?> style="background-color:tan;">
    					<a href="#<?php echo sanitize_title_with_dashes($name); ?>">
    						<?php echo crb_wp_get_attachment_image($type_image, 'crb_home_tab_icon','alt'); ?>
    					</a>
    				</li>

    outputs alt=””

    I’m also using this function:

    function wp_get_attachment( $attachment_id ) {
    
    	$attachment = get_post( $attachment_id );
    	return array(
    		'alt' => get_post_meta( $attachment->ID, 'wp_attachment_image_alt', true )
    	);
    }

    But I’m not getting anywhere. I’m trying to output the image meta data -specifically the alt tag in the media library.

    Any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter phillystyle123

    (@phillystyle123)

    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Hi @phillystyle123,

    This is not really in the scope of Carbon Fields as your issue is with WordPress’ wp_get_attachment_image() not always adding an “alt” attribute.

    You can refer to https://wordpress.stackexchange.com/questions/1051/how-to-retrieve-an-image-attachments-alt-text where they discuss this and add alternative solutions.

    Thread Starter phillystyle123

    (@phillystyle123)

    Thanks for your reply, Atanas. I think this is related to carbon fields because there is no alt tag built into the carbon fields code. I’ve been attempting to frankenstien together the carbon fields code with WordPress’ wp_get_attachment_image() to no avail. This is what I have so far and it’s outputting

    <?php foreach ($team_types as $type_index => $type): 
    				$name = $type['title'];
    				$type_image = $type['team_type_icon'];
    				$active_class = '';
    				if ($type_index == 0) {
    					$active_class = ' class="active"';
    				}
    			
    				?>
                    <?php
    					$response = array(
            'id'          => $attachment->ID,
    		'alt'         => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
        );
    				 ?>
                    				<li <?php echo $active_class; ?> style="background-color:tan;">
    					<a href="#<?php echo sanitize_title_with_dashes($name); ?>">
    						<?php echo crb_wp_get_attachment_image($type_image, 'crb_home_tab_icon','alt'); ?>
    					</a>
    				</li>
    			<?php endforeach ?>
    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Carbon Fields provide you with an attachment id – this is sufficient to pull any and all attachment data through various WordPress functions (as discussed in the thread I linked in my previous message).

    There is no need of an additional alt field since WordPress already handles titles, alts and other data through their attachments:

    If you fill in that field, the following code is sufficient to pull the image with alt:

    
    echo wp_get_attachment_image( carbon_get_post_meta( 'your_field_name_here' ) );
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘New to carbon fields -need to add image alt tag’ is closed to new replies.