Display posts in current category

Display Posts Shortcode lets you limit posts to a certain category with the category parameter. If you add the code below to your theme’s functions.php file or a core functionality plugin, you can now use and it will use the category that the current post is in automatically.

<?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 );
view raw functions.php hosted with ❤ by GitHub

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk