Skip to content

Instantly share code, notes, and snippets.

@SeanChDavis
Created August 20, 2014 04:39
Show Gist options
  • Select an option

  • Save SeanChDavis/1c7f4bc1630bc63672bd to your computer and use it in GitHub Desktop.

Select an option

Save SeanChDavis/1c7f4bc1630bc63672bd to your computer and use it in GitHub Desktop.
Rebuild EDD [downloads] shortcode in theme/plugin
<?php
/*
* remove the default [downloads] shortcode from Easy Digital Downloads
* and then rebuild it with full control
*/
// remove default EDD [downloads] shortcode
remove_shortcode( 'downloads' );
// rebuild [downloads] shortcode
function my_custom_edd_downloads_query( $atts, $content = null ) {
extract( shortcode_atts( array(
'category' => '',
'exclude_category' => '',
'tags' => '',
'exclude_tags' => '',
'relation' => 'AND',
'number' => 10,
'price' => 'no',
'excerpt' => 'yes',
'full_content' => 'no',
'buy_button' => 'yes',
'columns' => 3,
'thumbnails' => 'true',
'orderby' => 'post_date',
'order' => 'DESC',
'ids' => ''
), $atts, 'downloads' )
);
$query = array(
'post_type' => 'download',
'posts_per_page' => (int) $number,
'orderby' => $orderby,
'order' => $order
);
if ( $query['posts_per_page'] < -1 ) {
$query['posts_per_page'] = abs( $query['posts_per_page'] );
}
switch ( $orderby ) {
case 'price':
$orderby = 'meta_value';
$query['meta_key'] = 'edd_price';
$query['orderby'] = 'meta_value_num';
break;
case 'title':
$query['orderby'] = 'title';
break;
case 'id':
$query['orderby'] = 'ID';
break;
case 'random':
$query['orderby'] = 'rand';
break;
default:
$query['orderby'] = 'post_date';
break;
}
if ( $tags || $category || $exclude_category || $exclude_tags ) {
$query['tax_query'] = array(
'relation' => $relation
);
if ( $tags ) {
$query['tax_query'][] = array(
'taxonomy' => 'download_tag',
'terms' => explode( ',', $tags ),
'field' => 'slug'
);
}
if ( $category ) {
$query['tax_query'][] = array(
'taxonomy' => 'download_category',
'terms' => explode( ',', $category ),
'field' => 'slug'
);
}
if ( $exclude_category ) {
$query['tax_query'][] = array(
'taxonomy' => 'download_category',
'terms' => explode( ',', $exclude_category ),
'field' => 'slug',
'operator' => 'NOT IN',
);
}
if ( $exclude_tags ) {
$query['tax_query'][] = array(
'taxonomy' => 'download_tag',
'terms' => explode( ',', $exclude_tags ),
'field' => 'slug',
'operator' => 'NOT IN',
);
}
}
if( ! empty( $ids ) )
$query['post__in'] = explode( ',', $ids );
if ( get_query_var( 'paged' ) )
$query['paged'] = get_query_var('paged');
else if ( get_query_var( 'page' ) )
$query['paged'] = get_query_var( 'page' );
else
$query['paged'] = 1;
switch( intval( $columns ) ) :
case 0:
$column_width = 'inherit'; break;
case 1:
$column_width = '100%'; break;
case 2:
$column_width = '50%'; break;
case 3:
$column_width = '33%'; break;
case 4:
$column_width = '25%'; break;
case 5:
$column_width = '20%'; break;
case 6:
$column_width = '16.6%'; break;
endswitch;
// Allow the query to be manipulated by other plugins
$query = apply_filters( 'edd_downloads_query', $query, $atts );
$downloads = new WP_Query( $query );
if ( $downloads->have_posts() ) :
$i = 1;
$wrapper_class = 'edd_download_columns_' . $columns;
ob_start(); ?>
<div class="edd_downloads_list <?php echo apply_filters( 'edd_downloads_list_wrapper_class', $wrapper_class, $atts ); ?>">
<?php while ( $downloads->have_posts() ) : $downloads->the_post(); ?>
<div itemscope itemtype="http://schema.org/Product" class="<?php echo apply_filters( 'edd_download_class', 'edd_download', get_the_ID(), $atts, $i ); ?>" id="edd_download_<?php echo get_the_ID(); ?>" style="width: <?php echo $column_width; ?>; float: left;">
<div class="edd_download_inner">
<?php
do_action( 'edd_download_before' );
if ( 'false' != $thumbnails ) :
edd_get_template_part( 'shortcode', 'content-image' );
endif;
edd_get_template_part( 'shortcode', 'content-title' );
if ( $excerpt == 'yes' && $full_content != 'yes' )
edd_get_template_part( 'shortcode', 'content-excerpt' );
else if ( $full_content == 'yes' )
edd_get_template_part( 'shortcode', 'content-full' );
if ( $price == 'yes' )
edd_get_template_part( 'shortcode', 'content-price' );
if ( $buy_button == 'yes' )
edd_get_template_part( 'shortcode', 'content-cart-button' );
do_action( 'edd_download_after' );
?>
</div>
</div>
<?php if ( $columns != 0 && $i % $columns == 0 ) { ?><div style="clear:both;"></div><?php } ?>
<?php $i++; endwhile; ?>
<div style="clear:both;"></div>
<?php wp_reset_postdata(); ?>
<div id="edd_download_pagination" class="navigation">
<?php
if ( is_single() ) {
echo paginate_links( array(
'base' => get_permalink() . '%#%',
'format' => '?paged=%#%',
'current' => max( 1, $query['paged'] ),
'total' => $downloads->max_num_pages
) );
} else {
$big = 999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, $query['paged'] ),
'total' => $downloads->max_num_pages
) );
}
?>
</div>
</div>
<?php
$display = ob_get_clean();
else:
$display = sprintf( _x( 'No %s found', 'download post type name', 'edd' ), edd_get_label_plural() );
endif;
return apply_filters( 'downloads_shortcode', $display, $atts, $buy_button, $columns, $column_width, $downloads, $excerpt, $full_content, $price, $thumbnails, $query );
}
add_shortcode( 'downloads', 'my_custom_edd_downloads_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment