Create a custom “Download” post type using WordPress – Part 2

Create a custom “Download” post type using WordPress - Part 2

This is part 2 of my “Create a custom “Download” post type using WordPress” series.

Table of Contents

  1. Create a “Download” shortcode
  2. Create the Download Categories archive
  3. Download the full code

Download the full code

I’ve created a TwentyEleven child theme with the full code both parts. As a bonus, the download includes two widgets and CSS styling. You can drop it in your wp-content/themes folder and use it – or just take parts of it for your own theme.

Download Demo Downloaded 1,741 times

Create a “Download” shortcode

The first thing is to create a shortcode called “download”. This is very useful for displaying downloads in posts.

function jayj_download_shortcode( $atts, $content = null ) {
	// Default values
	extract( shortcode_atts( array(
		'id' => '',
		'format' => 'box', // or inline
		'title' => '',
		'show_count' => true,
		'thumbnail' => false
	), $atts ) );
}

add_shortcode( 'download', 'jayj_download_shortcode' );

Now we’ve created the [download] shortcode and the default values for the shortcode parameters, that can be used like [download id="1"]

Here’s a small explanation of the parameters

  • ID: ID of the download
  • Format: box or inline. Default is box. I’ll explain this later
  • Title: The title on the anchor links
  • Show_count: Show the download count? If true, the count will be appended to the title as well. Default is true – set any value to make it false
  • Thumbnail: Show download thumbnail? Default is false – set any value to make it true. This is only for ‘box’ downloads
  • Content: This isn’t a parameter, but you can add content (like a description) by adding text between the opening and closing shortcode ([download]Content here[/download])

Let’s continue. Inside the jayj_download_shortcode function, after the default values, we first check if the ID is set and if the ID actually belongs to a download.

// No ID, return
if ( empty( $id ) )
	return '<em>Error: No download found</em>';

// Get download
$download = get_post( $id );

// The ID doesn't belong to a download, return
if ( $download->post_type != 'jayj_download' )
	return '<em>Error: The download is not valid</em>';

The next thing is to add some content to the shortcode. The download can be in two formats (you can easily add more): box or inline.

Box: There’s a box around the download link, like on this page
Inline: The download link is simply an anchor link (<a href=""></a>)

First the box format:

The reason why I use ob_start(); and return ob_get_clean(); is so that the the content is returned instead of echoed.

Now to the inline format

/**
 * Format: inline
 */
if ( 'inline' == $format ) :

	$count_meta = get_post_meta( $id, 'download_count', true );

	// Get the title, either from shortcode or the title of the download
	$title = ( $title ) ? $title : 'Download ' .  $download->post_title;
		
	// Append count to title
	if ( $show_count === true )
		$title .= ' (Downloaded ' . number_format( $count_meta ) . ' times)';
		
	// Get content
	$content = ( empty( $content ) ) ? 'Download ' .  $download->post_title : $content;
		
	return '<a href="' . get_permalink( $id ) . '" class="inline-download" title="' .  esc_attr( $title ) . '">' . $content . '</a>';
endif; // 'inline' == $format
	
return;

The shortcode is now done!


The Archive for Download Categories

In part 1, you created the archive page for all the downloads. But we still need an archive page for the terms in the “Download Categories” taxonomy.

The different term archives can be found at example.com/download-types/*term-name*

Create a new file in your theme and call it taxonomy-download-categories.php
You can pretty much copy the content from your archive-jayj_download.php file into it.

The only difference is that you should add

global $wp_query;
// Merge with the original query
$args = array_merge( $wp_query->query, $args );

before query_posts( $args ); – That will merge your custom query together with the original.


The Widgets

Last, but not least: the widgets.

As a bonus for those who download the full code, I’ve created two widgets: One with the latest downloads and one with the most popular downloads.
If you want those widgets, I suggest you download the full code :)

Download the full code

I’ve created a TwentyEleven child theme with the full code both parts. As a bonus, the download includes two widgets and CSS styling. You can drop it in your wp-content/themes folder and use it – or just take parts of it for your own theme.

Download Demo Downloaded 1,741 times
Posted in on
5 likes

11 responses

  1. Warning: number_format() expects parameter 1 to be double, string given in Z:\web\blog\wp-content\themes\BlackMotion\functions.php on line 648
    Download
    Warning: number_format() expects parameter 1 to be double, string given in Z:\web\blog\wp-content\themes\BlackMotion\functions.php on line 665
    Downloaded times

    1. Hello,

      Try this (line 323 and 368 in the download functions.php):

      $count_meta = (float) get_post_meta( $id, 'download_count', true );
      

      – Jesper

Leave a Reply