Skip to content
This repository has been archived by the owner on Sep 4, 2018. It is now read-only.

Commit

Permalink
Merge pull request #16 from MatthewEppelsheimer/patch-no-categories-f…
Browse files Browse the repository at this point in the history
…ound

Improved shortcode category handling
  • Loading branch information
ericmann committed Aug 24, 2012
2 parents 71fef58 + 6cff7de commit 6bf02c8
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions lib/class.wp-publication-archive.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -238,15 +238,6 @@ public static function shortcode_handler( $atts ) {


global $post; global $post;


// Create an array of category IDs based on the categories fed in.
$catFilter = array();
$catList = explode(',', $categories);
foreach( $catList as $catName ){
$id = get_cat_id( trim( $catName ) );
if( 0 !== $id )
$catFilter[] = $id;
}

$pubs_per_page = apply_filters( 'wpa-pubs_per_page', 10 ); $pubs_per_page = apply_filters( 'wpa-pubs_per_page', 10 );


if(isset($_GET['wpa-paged'])) { if(isset($_GET['wpa-paged'])) {
Expand All @@ -257,19 +248,33 @@ public static function shortcode_handler( $atts ) {
$offset = 0; $offset = 0;
} }


$list = '<div class="publication-archive">';

// Get publications // Get publications
$args = array( $args = array(
'offset' => $offset, 'offset' => $offset,
'numberposts' => $pubs_per_page, 'numberposts' => $pubs_per_page,
'post_type' => 'publication', 'post_type' => 'publication',
'orderby' => 'post_date', 'orderby' => 'post_date',
'order' => 'DESC', 'order' => 'DESC',
'post_status' => 'publish', 'post_status' => 'publish'
'category__in' => $catFilter
); );


if ( '' != $categories ) {
// Create an array of category IDs based on the categories fed in.
$catFilter = array();
$catList = explode(',', $categories);
foreach( $catList as $catName ){
$id = get_cat_id( trim( $catName ) );
if( 0 !== $id )
$catFilter[] = $id;
}
// if no categories matched categories in the database, report failure
if ( empty( $catFilter ) ) {
$error_msg = "<div class='publication-archive'><p>". __(' Sorry, but the categories you passed to the wp-publication-archive shortcode do not match any publication categories.' ) . "</p><p>" . __( 'You passed: ', 'wp-publication-archive' ) . "<code>$categories</code></p></div>";
return $error_msg;
}
$args['category'] = implode( ',', $catFilter );
}

if('' != $author) { if('' != $author) {
$args['tax_query'] = array(array( $args['tax_query'] = array(array(
'taxonomy' => 'publication-author', 'taxonomy' => 'publication-author',
Expand All @@ -282,8 +287,35 @@ public static function shortcode_handler( $atts ) {


$args['numberposts'] = -1; $args['numberposts'] = -1;
$total_pubs = count( get_posts( $args ) ); $total_pubs = count( get_posts( $args ) );

// Report if there are no publications matching filters
if ( 0 == $total_pubs ) {
$error_msg = "<p>" . __( 'There are no publications to display' );
if ( '' != $author )
$error_msg .= __( ' by ' ) . "$author";
if ( '' != $categories ) {
// There is probably a better way to do this…
$error_msg .= __( ' categorized ' );
$catList = explode ( ',', $categories );
$catNum = count( $catList );
$x = 3; // number of terms necessary for grammar to require commas after each term
if ( $catNum > 2 ) $x = 1;
for ( $i = 0; $i < $catNum; $i++ ) {
if ( $catNum > 1 && $i == ( $catNum - 1 ) ) $error_msg .= 'or ';
$error_msg .= $catList[$i];
if ( $i < ( $catNum - $x ) ) {
$error_msg .= ', ';
} else if ( $i < ( $catNum - 1 ) ) {
$error_msg .= ' ';
}
}
}
$error_msg .= ".</p>";
return $error_msg;
}


// Create publication list // Create publication list
$list = '<div class="publication-archive">';
foreach( $publications as $publication ) { foreach( $publications as $publication ) {
$pub = new WP_Publication_Archive_Item( $publication->ID, $publication->post_title, $publication->post_date ); $pub = new WP_Publication_Archive_Item( $publication->ID, $publication->post_title, $publication->post_date );


Expand Down Expand Up @@ -358,4 +390,4 @@ public static function the_title( $title, $id ) {
return $title . " (Download Publication)"; return $title . " (Download Publication)";
} }
} }
?> ?>

0 comments on commit 6bf02c8

Please sign in to comment.