Support » Plugin: Display Posts Shortcode » [Plugin: Display Posts Shortcode] better way to filter the output

  • Resolved bristweb


    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>';
    		$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> ';
    				$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>';
    	$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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Bill Erickson


    Thanks for the recommendation, but I actually prefer the solution in the plugin. It’s a better practice (and is done throughout WordPress core) to build whatever you’d like, then right before echoing or returning it add your filter. That way the filter can modify the default output.

    Even if the approach you describe were better, it’s too late for me to change the filter. It does have a ton of variables in it now, but I can’t remove or change any of them without risking breaking existing filters. In order to be backwards compatible, I can only add additional fields to the end of it (that’s why my recently added “wrapper” field is the last variable).


    thanks for the response; i figured backwards compatibility would be an issue… i did take some shortcuts in my code

    i really don’t want to keep a hacked version of the plugin if the base concept could be integrated.

    could you add a second filter which filters the entire output (rather than just the
    ex: display_posts_shortcode_full_output

    if the full output could be filtered and the post array sent to the function – that would allow easy customization (you don’t have to keep trying to make changes to accommodate people’s specific requests)

    Plugin Author Bill Erickson


    I still don’t understand why you can’t use the existing filters in place. display_posts_shortcode_output will let you manage the full output of a post, and the other two filter everything before and after each one.

    Can you describe your specific use case so I can see why the current filters in place can’t handle it?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: Display Posts Shortcode] better way to filter the output’ is closed to new replies.