[Plugin: Display Posts Shortcode] better way to filter the output
-
thanks for this plugin! very clean and simple.
i really like the filter, however it is a bit limited. i thought i’d suggest the following change that would give template designers much more flexibility
in the display-posts-shortcode.php lines ~76-99, the code becomes:
if ( $listing->have_posts() ): $return = apply_filters( 'display_posts_shortcode_output', $listing, $atts ); if($return != NULL){ $return .= '<ul class="display-posts-listing">'; while ( $listing->have_posts() ): $listing->the_post(); global $post; if ( $image_size && has_post_thumbnail() ) $image = '<a class="image" href="'. get_permalink() .'">'. get_the_post_thumbnail($post->ID, $image_size).'</a> '; else $image = ''; $title = '<a class="title" href="'. get_permalink() .'">'. get_the_title() .'</a>'; if ($include_date) $date = ' <span class="date">('. get_the_date('n/j/Y') .')</span>'; else $date = ''; if ($include_excerpt) $excerpt = ' - <span class="excerpt">' . get_the_excerpt() . '</span>'; else $excerpt = ''; $output = '<li>' . $image . $title . $date . $excerpt . '</li>'; endwhile; $return .= '</ul>'; } endif; wp_reset_query();basically, it sends the wp query directly to the filter so that the template designer can do anything they like (assign different classes, use div instead of li, rearrange, etc). it then tests to see if the filter is being used anywhere… if it is, it uses the filter… if not, then it uses the default output.
you can then provide an example filter function like so:
add_filter( 'display_posts_shortcode_output', 'theme_display_posts_shortcode_output', 10, 7 ); function theme_display_posts_shortcode_output( $listings, $atts ) { $return = '<ul class="post-list">'; while ( $listings->have_posts() ): $listings->the_post(); global $post; $return .= ""; if ( $image_size && has_post_thumbnail() ) $image = '<a class="image alignleft" href="'. get_permalink() .'">'. get_the_post_thumbnail($post->ID, $image_size).'</a> '; else if ( has_post_thumbnail() ) $image = '<a class="image alignleft" href="'. get_permalink() .'">'. get_the_post_thumbnail($post->ID,'thumbnail').'</a> '; else $image = ''; $title = '<h3 class="title">'. get_the_title() .'</h3>'; $readon = '<span class="readon">[ <a title="'.get_the_title().'" href="'.get_permalink().'">Read More</a> ]</span>'; $date = ' <span class="date">Posted on '. get_the_date() .'</span>'; $excerpt = '<div class="excerpt">' . get_the_excerpt() . '</div>'; $return .= '<li>' . $image . $title . $excerpt . $readon . $date . '<div class="clear"></</li>'; endwhile; $return .= "</ul>"; return $return; }I igored the attributes (ex: show-date = true/false) as I don’t think we need them for the site i made this change for.
The topic ‘[Plugin: Display Posts Shortcode] better way to filter the output’ is closed to new replies.