Permalink
Browse files

Merge pull request #16 from MatthewEppelsheimer/patch-no-categories-f…

…ound

Improved shortcode category handling
  • Loading branch information...
2 parents 71fef58 + 6cff7de commit 6bf02c828d4ea95f2644a62426096ec4217b0379 @ericmann committed Aug 24, 2012
Showing with 46 additions and 14 deletions.
  1. +46 −14 lib/class.wp-publication-archive.php
@@ -238,15 +238,6 @@ public static function shortcode_handler( $atts ) {
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 );
if(isset($_GET['wpa-paged'])) {
@@ -257,19 +248,33 @@ public static function shortcode_handler( $atts ) {
$offset = 0;
}
- $list = '<div class="publication-archive">';
-
// Get publications
$args = array(
'offset' => $offset,
'numberposts' => $pubs_per_page,
'post_type' => 'publication',
'orderby' => 'post_date',
'order' => 'DESC',
- 'post_status' => 'publish',
- 'category__in' => $catFilter
+ 'post_status' => 'publish'
);
+ 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) {
$args['tax_query'] = array(array(
'taxonomy' => 'publication-author',
@@ -282,8 +287,35 @@ public static function shortcode_handler( $atts ) {
$args['numberposts'] = -1;
$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
+ $list = '<div class="publication-archive">';
foreach( $publications as $publication ) {
$pub = new WP_Publication_Archive_Item( $publication->ID, $publication->post_title, $publication->post_date );
@@ -358,4 +390,4 @@ public static function the_title( $title, $id ) {
return $title . " (Download Publication)";
}
}
-?>
+?>

0 comments on commit 6bf02c8

Please sign in to comment.