jc28
jc28

Reputation: 1

How to add Title Attribute to Wordpress Image on Attachment Page in TwentyEleven Theme?

This is a WP/SEO question related to adding the title attribute to the on an attachment page. We are using the twentyeleven theme and default image.php code, as below.

We want to pull in the title tag (or even automatically set the title for all images, so user doesn't have to individually set, or just use post title for the title - any of these).

The html from View Source looks like this:

The image.php code:

<a href="<?php echo esc_url( $next_attachment_url ); ?>" title="<?php the_title_attribute(); ?>" rel="attachment"><?php

                                $attachment_size = apply_filters( 'twentyeleven_attachment_size', 848 );

                                echo wp_get_attachment_image( $post->ID, array( $attachment_size, 1024 ) ); // filterable image width with 1024px limit for image height.

                                ?></a>

Upvotes: 0

Views: 1522

Answers (1)

Giancarlo Colfer
Giancarlo Colfer

Reputation: 559

I also wanted all my attachments to automatically have a title attribute, so I wrote the following PHP code:

<?php if(wp_attachment_is_image($post->id)) : $att_image = wp_get_attachment_image_src($post->id, "full"); ?><p class="attachment"><a class="show_image" rel="bookmark" href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php echo get_the_title(); ?>"><img class="img-responsive attachment-medium" src="<?php echo $att_image[0]; ?>" height="<?php echo $att_image[2];?>" width="<?php echo $att_image[1];?>" alt="<?php echo get_the_title(); ?>"></a></p><?php endif; ?>

This PHP code has been tested on WordPress 3.7.1. A live example of this code being used on a custom WordPress image.php file can be found on my Web Development & Design Blog

Upvotes: 1

Related Questions