WordPress Show image title only on attachment page (with all in one seo editing too)

By default, wordpress shows image title + post title + blog name on the attachment page title.

Yesterday I got a request from client that he want to make it image title only on attachment page to avoid keyword stuffing.

To do that, the code that we will be utilizing is simple.

This code editing is bit theme specific too. Every theme has it’s own way of calling page title in header.php.

Goto header.php and find the part between <title> </title> tags.

Suppose it is: <?php get_title(); ?>

Replace this code with:

<?php

if (is_attachment()) { 
	echo $post->post_title; 
}else{
 get_title(); 
}

?>

On attachment page, the posts post_title is the title of the image / attachment so this will get the image title only for attachment page and leave the rest for the normal code to figure out for other pages.

Please note, if your title comprises of conditional tags, place the is_attachment logic above is_single because attachment page give true for is_single too.

One more issue on the client’s installation was All in one seo plugin. This plugin replaces all the content within <title> </title> tags with it’s own generated title if rewrite titles is checked in all in one seo settings in wordpress admin backend. For that we have to add this edit the aioseop.class.php file and add this code to stop it from changing titles of the attachment page.

Add this to line 92 of aioseop.class.php in wp-content/plugins/all-in-one-seo/

		//added by nabeel
		if(is_attachment()) {
			return;
		}

before:

		if (is_feed()) {
			return;
		}

Now your wordpress works normally with all in one seo plugin but the attachment page titles are not stuffed with keywords anymore and contain only the image title.

Leave a Reply

Your email address will not be published.