This is page 3 of 3. For the first part please go to page 1
Now that our loop is at place, let’s create the markup for the downloads. After <?php while ( have_posts() ) : the_post(); ?> add:
<?php
$download = get_post_meta( $post->ID, 'download', true );
$download_count = get_post_meta( $post->ID, 'download_count', true );
?>
<article id="download-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Download %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title( '', ' <span>&ra rr;</span>' ); ?></a></h1>
<div class="entry-meta">
<?php
$show_sep = false;
if ( ! empty( $download['postid'] ) )
$show_sep = '<span class="sep">|</span>';
// Get the download categories
echo get_the_term_list( $post->ID, 'download-categories', 'Type: ', ' ', $show_sep );
// Get the associated post
if ( ! empty( $download['postid'] ) )
echo '<a href="' . get_permalink( $download['postid'] ) . '" title="Read the post ' . get_the_title( $download['postid'] ) . '">Read the post</a>';
?>
</div><!-- .entry-meta -->
<?php
if ( has_post_thumbnail() )
the_post_thumbnail();
?>
</header>
<div class="download-description">
<?php echo $post->post_excerpt; // We can't use the_excerpt because some themes has a "Continue reading..." link ?>
</div>
<footer class="entry-meta">
<a href="" class="download-btn">Download “<?php the_title(); ?>”</a>
<span class="download-count">Downloaded <?php echo number_format( $download_count ); ?> times</span>
</footer><!--.entry-meta-->
</article><!-- #download-<?php the_ID(); ?> -->
Again, this markup is based on Twenty Eleven – you might want to change it. With some styling, here how it can look
Create the download counter
We have the post type, a custom taxonomy, a metabox and the archive. But we still haven’t created a way for the user to actually download the file. Luckily that’s quite easy to do.
/**
* Download counter function
*/
function jayj_count_and_redirect() {
// Return if not download
if ( ! is_singular( 'jayj_download' ) )
return;
$download_id = get_queried_object_id();
$postmeta = get_post_meta( $download_id, 'download', true );
$count_meta = get_post_meta( $download_id, 'download_count', true );
// Get the count
$count = isset( $download_id ) ? $count_meta : 0;
// Handle the redirect
$redirect = isset( $download_id ) ? $postmeta['file'] : '';
if ( ! empty( $redirect ) ) :
wp_redirect( esc_url_raw( $redirect ), 301 );
update_post_meta( $download_id, 'download_count', $count + 1 ); // Update the count
exit;
else :
wp_redirect( home_url(), 302 );
exit;
endif;
}
add_action( 'template_redirect', 'jayj_count_and_redirect' );
Let me explain the code. When a user goes to example.com/download/%slug% they normally would see the the single view of the download. But we don’t need that.
What this function does it that it detects when a user vists the single view, it gets the post meta, redirect them to the file and updates the download count. Pretty awesome.
That’s it!
That was it for part 1! Wow. It became quite longer that I imagined – hope I didn’t lose you.
In this part you learned how to
- Register the post type
- Create a custom “Download Categories” taxonomy
- Create a metabox
- Create the archive
- and create the download counter
Download the code on Github
The next part will be about creating a download shortcode, a widget and the archive for the custom taxonomy.
If you have any comments, suggestions or have spotted an error please write a comment.

Really usefull! Thanks for sharing.
You’re welcome
Hi,
Can you please throw any light on adding a “pay to download” method in custom download post type? Or any recommendation of any plugin which can be used to achieve this?
Thanks,
Rajat.
Hello,
Sorry but I don’t know how to add a “Pay to download” function to this.
You can take a look at some eCommerce plugins.
Pingback: Create a custom “Download” post type using WordPress - Part 2 | Jayj.dk
Pingback: Hello Downloads! | "Download" Post Type
Pingback: welikesport by dogriad38370 - Pearltrees
Pingback: Part 2: Create a custom "Download" post type using WordPress