Wednesday, December 05, 2012

Removing Comments Form from Wordpress Photo Attachments



I just figured out a stupid Wordpress problem that has been bugging me for a while. (I’m using version 3.2.) I’m not a programmer or coder at any level so it feels good to figure this out on my own.

The problem was Wordpress was showing the comment form with attachments. The Discussion settings that let you close comments after so many days applies to Post only, and not attachments. I knew that I could tweek the PHP code to solve this.

I checked the attachment.php file and saw that this referenced loop-attachment.php so I checked that for the code that was adding the comment form. Near the bottom of the page I saw what I thought was the offending code ((Blogger strips out the PHP tags so I'm alerting the brackets here ( = < .... )):


(?php comments_template(); ?)


But commenting this out didn’t remove the form from attachment pages. I checked functions.php and didn’t see anything there. So I started to use simple echo statement to see what file would be adding the text to my pages.

This showed lead me to single.php and near the bottom was the same code I was looking for:


(?php  comments_template( '', true ); ?)


I knew I couldn’t remove this completely since it would remove the comment form from Post that I wanted it on. I knew a simple If Else statement could solve this. So using if_attachment I replaced  

(?php  comments_template( '', true ); ?)  with my If Else statement:


(?php if (is_attachment())
{
 echo "Comments Closed";
}
 else
 { comments_template( '', true ); 
}
?)


I added the echo but you could leave this out if you wanted. In the end this probably isn’t the most elegant solution but works for me!

No comments: