BlogThemes

How to Create a Custom Single Attachments Template in WordPress

Within the course of of making a complete gallery show in WordPress powered by Customized Put up Sorts, we’ve shared the code for how one can get all submit attachments in WordPress apart from Featured Picture. We’ve got additionally confirmed you how one can add further fields to the WordPress Media uploader. On this article, we’ll present you how one can use the extra fields within the media uploader and show them in a customized single attachments template.

Earlier than we begin, it will be significant that you simply perceive how the template hierarchy works for attachments. You possibly can both specify it like mime_type.php. Mime kind examples (picture.php, video.php, software.php and so on). For specifying it only for a sure picture kind, you’ll be able to say image_gif.php. If the mime_type.php just isn’t discovered, then it seems for attachment.php, then single-attachment.php, then single.php, then index.php.

It depends upon how customized of a challenge you might be working for, you might not must create image_gif.php. You possibly can merely suffice for picture.php and even attachment.php.

All you must do is open a clean file and duplicate all your single.php content material in there. It can save you it as single-attachment.php or another mime_type that you really want. Then exchange the loop code with one thing like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
if ( have_posts() ) : whereas ( have_posts() ) : the_post();
$photographer = get_post_meta($submit->ID, 'be_photographer_name', true);
$photographerurl = get_post_meta($submit->ID, 'be_photographer_url', true);
?>
<h1><?php the_title(); ?></h1>
<div class="photometa"><span class="photographername"><?php echo $photographer; ?></span> // <a href="<?php echo $photographerurl ?>" goal="_blank" class="photographerurl"><?php echo $photographerurl ?></a></div>
                        <div class="entry-attachment">
<?php if ( wp_attachment_is_image( $submit->id ) ) : $att_image = wp_get_attachment_image_src( $submit->id, "full"); ?>
                        <p class="attachment"><a href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php the_title(); ?>" rel="attachment"><img src="<?php echo $att_image[0];?>" width="<?php echo $att_image[1];?>" top="<?php echo $att_image[2];?>"  class="attachment-medium" alt="<?php $post->post_excerpt; ?>" /></a>
                        </p>
<?php else : ?>
                        <a href="<?php echo wp_get_attachment_url($post->ID) ?>" title="<?php echo wp_specialchars( get_the_title($post->ID), 1 ) ?>" rel="attachment"><?php echo basename($submit->guid) ?></a>
<?php endif; ?>
                        </div>
<?php endwhile; ?>
<?php endif; ?>

The code above merely shows the Picture Title. Beneath it, it’ll show Writer’s title and URL which we added as further fields in our earlier article. Then it seems for the picture and shows the total measurement. You possibly can customise it to indicate another measurement you probably have further picture sizes.

Remaining Final result:

Single Attachment Example

Now that we’ve lined all three foremost points of this gallery that we have been going to create, within the subsequent article, we’ll present you the way it seems when all issues are pieced collectively. This fashion, you need to use WordPress’s built-in mechanism to create a full-featured gallery with albums. No further plugins required.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBest WordPress PluginsBlog

9 Best Social Proof Plugins for WordPress & WooCommerce (2020)

Tips

How To Disable The WordPress User Admin Toolbar

Tips

WordPress Front Page vs Homepage

Tips

5 Things You Must Avoid Doing in WordPress

Leave a Reply

Your email address will not be published.

Pay in your preferred currency
Indian rupee
Euro