Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active October 19, 2017 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/76261bbdd0dcd5c727c55f581f097734 to your computer and use it in GitHub Desktop.
Save billerickson/76261bbdd0dcd5c727c55f581f097734 to your computer and use it in GitHub Desktop.
<?php
/**
* Display Posts Shortcode, support for 'current' category
*
*/
function be_dps_current_category( $args, $atts ) {
if( !empty( $args['category_name'] ) && 'current' == $args['category_name'] ) {
$current_categories = get_the_terms( get_the_ID(), 'category' );
if( !empty( $current_categories ) && ! is_wp_error( $current_categories ) ) {
$category = array_shift( $current_categories );
$args['category_name'] = $category->slug;
}
}
return $args;
}
add_filter( 'display_posts_shortcode_args', 'be_dps_current_category', 10, 2 );
@rktic
Copy link

rktic commented Feb 16, 2017

Hi Bill,

thank you very much for this. Noticed there's a closing bracket missing:

if( !empty( $current_categories ) && ! is_wp_error( $current_categories )
if( !empty( $current_categories ) && ! is_wp_error( $current_categories ))

But that's where my (poor) php skills end. Hence the question: could you extend this to work with all taxonomies like tags, custom posts etc? Tempted to use the shortcode as general loop replacement since it's so convenient.

Thank you
Ronny

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment