0

I think this will be a simple question for anyone who is familiar with WordPress and PHP. I'm not a PHP guru, so that's why I ask you guys! What we want is simple. Our images have a title and alt desc. But we want to echo them in the theme.

Below is the code where it should be echoed. Yes, I was able to find the piece of code responsible for this ;-). Any suggestions? enter image description here

        while( have_posts() ) :

        the_post();

        $image = wp_get_attachment_image_src( (int)get_post_meta( get_the_ID() , "_thumbnail_id" , true ) , "collection" );
        $large_image = wp_get_attachment_image_src( (int)get_post_meta( get_the_ID() , "_thumbnail_id" , true ) , "original" );

        $terms_array = array();

        $terms = wp_get_post_terms( get_the_ID() , "brand" );

        $brand_image = "";

        $has_brand_names = array();

        foreach( $terms as $term ) :

            if( !$brand_image ) $brand_image = $brand_images["brand-".$term->term_id];

            $terms_array[] = "brand-" . $term->term_id;

            $has_brand_names[ $term->slug ] = true;

        endforeach;

        $terms = wp_get_post_terms( get_the_ID() , "collection_subcategory" );

        foreach( $terms as $term ) :

            $terms_array[] = "subcategory-" . $term->term_id;

        endforeach;

        printf( "<div class='shoe %s' data-image='%s' data-large-image='%s' ><div class='shoe-outer' ><div class='shoe-inner' ><img src='%s' ></div></div></div>", join(" ", $terms_array ), esc_attr( $image[0] ), esc_attr( $large_image[0] ), $brand_image );

    endwhile;
2
  • You want to display the image with custom title and alt text... right? Are you getting any errors in the above code?
    – Sid
    Commented Dec 20, 2017 at 9:21
  • Hi Sid, the image has a title, this we want to echo on the printf as well. Currenty we don't have any errors.
    – Wouter
    Commented Dec 20, 2017 at 9:24

1 Answer 1

0

At the top of your code, just after $large_image, you can add this :

$image_alt = get_post_meta((int)get_post_meta( get_the_ID() , "_thumbnail_id" , true ), '_wp_attachment_image_alt', true);
$image_title = get_the_title((int)get_post_meta( get_the_ID() , "_thumbnail_id" , true ));

The alt for attachment is stock in post_meta (_wp_attachment_image_alt), the title is a post_title.

And then your replace the value in your printf().

EDIT:

printf( "<div class='shoe %s' data-image='%s' data-large-image='%s' ><div class='shoe-outer' ><div class='shoe-inner' ><img src='%s' alt='%s' title='%s' ></div></div></div>", join(" ", $terms_array ), esc_attr( $image[0] ), esc_attr( $large_image[0] ), $brand_image, $image_alt, $image_title );

Tell me if it's work :)

7
  • I'm not really a hero with printf. Could you show me how to modify? I've added them to the printf, but now we have a error.
    – Wouter
    Commented Dec 20, 2017 at 9:27
  • Answer is update :)
    – Elex
    Commented Dec 20, 2017 at 9:29
  • Thanks, but still nothing to see unfortunatly :-(. It is the featured image of a post. I've added an image to show you.
    – Wouter
    Commented Dec 20, 2017 at 9:31
  • Can you try to dump the 2 variables ? $image_alt, $image_title
    – Elex
    Commented Dec 20, 2017 at 9:35
  • I've dumped them. With $image_title I see the image name because that's the name from the image. But I want the featured post image title. Is this a different field?
    – Wouter
    Commented Dec 20, 2017 at 9:50

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.