WordPress Attachment Page Template Code Snippets
I haven't written as many WordPress tutorials as usual lately, as I have been so busy building websites and blogs, so I thought I would take the time out to share a few WordPress codes I have developed recently for attachment page templates and specifically the image attachment page template. The attachment page template is the page that displays a single image when the images link URL is set to 'Post URL'. You can set the image's link URL when inserting a single image into a post, and also when using the gallery shortcode.
The codes in this article are mostly applicable to images inserted using the gallery shortcode as they are most useful for posts or pages that have multiple image attachments. Read more about using image and file attachments.
These template codes can be added to your WordPress theme using the attachment.php and image.php files. If these template files do not exist in your theme you can create them, or WordPress will default to using single.php or index.php to show attachments by default. (See more about template hierarchy).
If you don't want to create a seperate image.php or attachment.php template you can always edit the index.php or single.php files and wrap the attachment specific codes in the is_attachment clause like so:
if(is_attachment()){
//attachment page specific code goes here
}
These php functions are loosely based on code I found in this article: Adding text links to WordPress Gallery by Michael Fields. In this article he provides code examples of how to show previous and next thumbnail links in a WP attachment page. I also made use of this previous-next keys in array function which is infinitely useful!
My first set of functions will return text links for previous image, next image and back to gallery. The functions themselves will need to be added to your theme's functions.php file before calling them in your image.php or attachment.php files. So here we go:
Read on…











WordPress Tips - Removing The Title Attribute From wp_list_categories & wp_list_pages | Sam Burdge…
I noticed in the WordPress forums that a lot of people were asking how to remove the title=”example” attribute from the links generated by the WordPress template tags wp_list_categories and wp_list_pages. The title attribute is what generates the lit…
Where would I place the code snippet for wp_list_pages?
Hi Tom
The code snippet above is meant to replace the existing wp_list_pages function used in your theme.
wp_list_pages is usually called in either header.php, footer.php or sidebar.php of your theme.
More info about wp_list_pages here:
http://codex.wordpress.org/Template_Tags/wp_list_pages
Sam
Thanks for the code snippit! I had started using nicetitles for http://chattanoogaswing.org and then noticed that wp_list_pages added tiles to all the links!
The calendar I was using doesn’t add a class for the dates I wanted the tooltips on. If it did, I could just modify the nicetitles javascript to only select that class of element.
Using your little regex to remove the titles form wp_list_pages is much easier!
Also, cool theme you’ve got going on here. I like the green-on-black console look.
thanx a lot for this tip !.
I have another question for everybody : How can I remove a hyperlink from wp_list_pages, but only from the first ul in the heirachy ?
thx for all who will consider my question !
Sébastien from France
Yet another thank you from me. Have been looking for a plugin to do this, but this is even better. Do you think it’s better for me to write a private plugin implementing this small little code? to avoid the snippet to be removed when I upgrade WP.
W9xinK great tips. I enjoyed reading this,
Exactly what I was looking for, thanks a lot!
Thanks a lot . I needed to remove the “title-attribute” because of a firefox(macOs)-bug that would always reload the dropdown after stoping to move the mouse on it.
This would not happen if there was no “title-attribute” anymore. To figure out this already took forever. This code finally make my dropdowns work like they should.
What happens if it isn’t hard coded into the theme and WP seems to be generating them of its own accord? What file do I need to edit?
For the wp_list_categories hack, how would I get the category name to display instead of “View all posts filed under “? Is there a simple hack that I’m not aware of?
Great tip - many thanks
You rock. Great post.
To answer JR, and anybody else wondering, open your header.php and look for replace it with his code.
If there are specifics in your brackets, make sure to include them in his code in addition to echo=0
Mine looks like this: $clean_page_list = wp_list_pages(’title_li=&depth=2&echo=0′);
meant to say look for the php calling wp_list_pages() …the php code got cut out.
Good tip. Thanks.
Those who aren’t comfortable editing their theme files can use the Remove Title Attributes plugin to solve this problem.