Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Improved shortcode category handling #16

Merged
merged 2 commits into from

2 participants

@MatthewEppelsheimer

Addresses #14.

Note my comment on line 297.

@ericmann ericmann merged commit 6bf02c8 into ericmann:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 23, 2012
  1. @MatthewEppelsheimer

    Added error message to shortcode for 'categories' parameters that don…

    MatthewEppelsheimer authored
    …'t match any categories in the database. Return no results when no publications match the 'categories' parameter.
Commits on Aug 24, 2012
  1. @MatthewEppelsheimer
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 14 deletions.
  1. +46 −14 lib/class.wp-publication-archive.php
View
60 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,8 +248,6 @@ public static function shortcode_handler( $atts ) {
$offset = 0;
}
- $list = '<div class="publication-archive">';
-
// Get publications
$args = array(
'offset' => $offset,
@@ -266,10 +255,26 @@ public static function shortcode_handler( $atts ) {
'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)";
}
}
-?>
+?>
Something went wrong with that request. Please try again.