0

I am using a WCK plugin to Wordpress. In the php code to call the image source, I used wp_get_attachment_image_src() but it only gets the url of the image. I want to include the alt value of it. Please help. Thanks.

Below is my code.

<div class="authortest-cont">
                <h2><span>W</span>hat Our <small>Authors Say</small></h2>
                <?php
                $wck_custom = get_post_meta( $post->ID, 'testimonialsbox', true);
                foreach($wck_custom as $wck_cstm) {

                    $author_name = $wck_cstm['author_name'];                            
                    $author_img = $wck_cstm['author_img'];
                    $author_testimonial = $wck_cstm['author_testimonial'];

                    if( is_numeric( $author_img ) ) {
                        $attach_author_img = wp_get_attachment_image_src( $author_img, 'full' );
                        $src_author_img = $attach_author_img[0];
                    }
                    else {
                        $src_author_img = $author_img;
                    }

                ?>


                <div class="col-md-6 col-sm-6">
                    <?php echo $author_testimonial; ?>
                    <div class="author-det">
                        <img src="<?php echo $src_author_img; ?>" alt=""/>
                    </div>
                </div>

                <?php } ?>
            </div>

How echo alt value of that image?

2 Answers 2

3

This looks like how you should use WCK:

<?php
foreach( get_cfc_meta( 'testimonialsbox' ) as $key => $value ){ ?>
    <?php $author_testimonial = $key['author_testimonial']; ?>
    <?php $photo_obj = get_cfc_field( 'testimonialsbox','photo', false, $key );  ?>
    <div class="col-md-6 col-sm-6">
        <?php echo $author_testimonial; ?>
        <div class="author-det">
            <img src="<?php echo $photo_obj['url'] ?>" alt="<?php echo $photo_obj['alt']; ?>"/>
        </div>
    </div>
<?php } ?>
3
  • how to return alt value of that image attachment?
    – bellabelle
    Commented Sep 8, 2017 at 2:53
  • I add the get_post_meta function to my answer. I think it is what you are looking for. Commented Sep 8, 2017 at 3:02
  • how to implement this to my code, above?
    – bellabelle
    Commented Sep 8, 2017 at 3:19
2

wp_get_attachment_image() will generate a full image tag, including the alt attribute. If you need to put a custom class or attribute on the image (one reason you might only want the src), you can still do that with that function by using the 4th argument:

echo wp_get_attachment_image( $attachment_id, 'large', false, array( 'class' => 'my-custom-class' ) );

If you just want the alt text on its own, then given the ID of the attachment you can just use:

echo get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
6
  • how to get the image of that attachment?
    – bellabelle
    Commented Sep 8, 2017 at 2:46
  • Is it automatically given?
    – bellabelle
    Commented Sep 8, 2017 at 2:46
  • In my first code snippet you'll get full <img src="" etc. HTML, so you'll get the image. If you're only using the second snippet, then you'd continue to use the wp_get_attachment_image_src() to get the URL. Commented Sep 8, 2017 at 2:48
  • I am using the get_post_meta($post->ID, '_wp_attachment_image_alt', true); but it returns empty string
    – bellabelle
    Commented Sep 8, 2017 at 2:52
  • What is $post? That's going to be the post, not the image attachment. You need to give it the attachment ID. Same as you were giving to the _src version. And make sure you, you know, have alt text. Commented Sep 8, 2017 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.