Skip to content

Instantly share code, notes, and snippets.

@raamdev
Last active January 4, 2016 01:49
Show Gist options
  • Select an option

  • Save raamdev/8550578 to your computer and use it in GitHub Desktop.

Select an option

Save raamdev/8550578 to your computer and use it in GitHub Desktop.
WordPress Shortcode for outputting a list of recent posts. Includes many options.
<?php
if ( ! function_exists( 'independent_publisher_recent_posts_shortcode' ) ) :
/**
* Returns recent posts for given category and excludes given post formats
*
* Example usage: [independent_publisher_recent_posts count="10" category="Technology" exclude_formats="aside,gallery,link" more_text="« Full Archives"]
*/
function independent_publisher_recent_posts_shortcode( $atts /*$number_posts = '10', $category = '', $exclude_formats = array()*/ ) {
$formats_to_exclude = '';
$count = '';
$category = '';
$post_type = '';
$exclude_formats = '';
$tax_query = '';
$more_text = '';
$orderby = '';
$order = '';
$cat_include = '';
$cat_exclude = '';
$cat_orderby = '';
$cat_order = '';
extract( shortcode_atts( array(
'count' => '10', // Default is to return 10 recent posts
'category' => '', // Default is to list all categories
'post_type' => 'post', // Default post type is 'post'
'exclude_formats' => '',
'more_text' => '« Full Archives',
'order' => 'desc', // Default sort order is descending (valid values are asc and desc)
'orderby' => 'date', // Default order by is 'date' (valid values are none, ID, author, title, date, modified, parent, rand, comment_count, and menu_order)
'cat_include' => '', // Comma-separated list of category IDs to include when displaying multiple categories
'cat_exclude' => '', // Comma-separated list of category IDs to exclude when displaying multiple categories
'cat_orderby' => 'name', // When displaying multiple categories, sort categories by name (valid values are id, name, slug, count, and term_group)
'cat_order' => 'asc' // When displaying multiple categories, order by ascending (valid values are asc and desc)
), $atts ) );
// Make sure $count is in a valid format
if ( trim( $count ) !== '' && ! is_numeric( $count ) ) {
return sprintf( __( '<strong>Recent Posts Shortcode:</strong> Count value of <code>%1$s</code> is not valid (must be a numerical value)!', 'independent_publisher' ), $count );
}
// Make sure $post_type is a valid post type
if ( ! post_type_exists( $post_type ) ) {
return sprintf( __( '<strong>Recent Posts Shortcode:</strong> Post type <code>%1$s</code> does not exist!', 'independent_publisher' ), $post_type );
}
// Make sure $cat_include value is in the correct format
$cat_include = preg_replace( '/\s+/', '', $cat_include ); // Strip whitespace
if ( trim( $cat_include ) !== '' && ! preg_match( '/^\d+(?:,\d+)*$/', $cat_include ) ) {
return sprintf( __( '<strong>Recent Posts Shortcode:</strong> Category IDs to include (<code>%1$s</code>) is not in the proper format (must be comma spearated numeric values, e.g., <code>3,12,15</code>!', 'independent_publisher' ), $cat_include );
}
// Make sure $cat_exclude value is in the correct format
$cat_exclude = preg_replace( '/\s+/', '', $cat_exclude ); // Strip whitespace
if ( trim( $cat_exclude ) !== '' && ! preg_match( '/^\d+(?:,\d+)*$/', $cat_exclude ) ) {
return sprintf( __( '<strong>Recent Posts Shortcode:</strong> Category IDs to exclude (<code>%1$s</code>) is not in the proper format (must be comma spearated numeric values, e.g., <code>3,12,15</code>!', 'independent_publisher' ), $cat_exclude );
}
// Make sure $orderby has a valid value
if ( ! in_array( $orderby, array( 'none', 'ID', 'author', 'title', 'date', 'modified', 'parent', 'rand', 'comment_count', 'menu_order' ) ) ) {
return sprintf( __( '<strong>Recent Posts Shortcode:</strong> Order by <code>%1$s</code> is not a valid value (valid values are <code>none</code>, <code>ID</code>, <code>author</code>, <code>title</code>, <code>date</code>, <code>modified</code>, <code>parent</code>, <code>rand</code>, <code>comment_count</code>, and <code>menu_order</code>; see <a href="%2$s">WordPress Codex</a>)!', 'independent_publisher' ), $orderby, esc_url( 'http://codex.wordpress.org/Function_Reference/get_posts#Parameters' ) );
}
// Make sure $order has a valid value
if ( ! in_array( $order, array( 'asc', 'desc' ) ) ) {
return sprintf( __( '<strong>Recent Posts Shortcode:</strong> Order of <code>%1$s</code> is not a valid value (valid values are <code>ASC</code> and <code>DESC</code>)!', 'independent_publisher' ), $order );
}
// Make sure $cat_orderby has a valid value
if ( ! in_array( $cat_orderby, array( 'id', 'name', 'slug', 'count', 'term_group' ) ) ) {
return sprintf( __( '<strong>Recent Posts Shortcode:</strong> Category order by <code>%1$s</code> is not a valid value (valid values are <code>id</code>, <code>name</code>, <code>slug</code>, <code>count</code>, and <code>term_group</code>; see <a href="%2$s">WordPress Codex</a>)!', 'independent_publisher' ), $cat_orderby, esc_url( 'http://codex.wordpress.org/Function_Reference/get_categories#Parameters' ) );
}
// Make sure $cat_order has a valid value lower
if ( ! in_array( strtolower( $cat_order ), array( 'asc', 'desc' ) ) ) {
return sprintf( __( '<strong>Recent Posts Shortcode:</strong> Category order of <code>%1$s</code> is not a valid value (valid values are <code>asc</code> and <code>desc</code>)!', 'independent_publisher' ), $cat_order );
}
// If we have a category, make sure the category exists
if ( trim( $category ) !== '' && ! term_exists( $category, 'category' ) && ! get_category_by_slug( $category ) && ! get_the_category_by_ID( $category ) ) {
return sprintf( __( '<strong>Recent Posts Shortcode:</strong> Category <code>%1$s</code> not found!', 'independent_publisher' ), $category );
}
// Figure out what format the category was passed in as (slug, name, or ID) and get the category ID
elseif ( trim( $category ) !== '' ) {
if ( $idObj = get_category_by_slug( $category ) ) {
$category_id = $idObj->term_id;
}
elseif ( get_cat_ID( $category ) !== 0 ) {
$category_id = get_cat_ID( $category );
}
elseif ( get_the_category_by_ID( $category ) ) {
$category_id = $category;
}
else {
$category_id = ''; // Unable to figure out the category; defaulting to all categories
}
}
else {
$category_id = ''; // If no category was provided, all categories will be used
}
// Check if we have one or more formats to exclude
if ( trim( $exclude_formats ) !== '' ) {
if ( FALSE !== strpos( $exclude_formats, ',' ) ) {
$formats_to_exclude = explode( ',', $exclude_formats );
}
else {
$formats_to_exclude = array( $exclude_formats );
}
}
// Build array of format exclusion queries
if ( is_array( $formats_to_exclude ) ) {
$i = 0;
$tax_query = array();
foreach ( $formats_to_exclude as $format ) {
$tax_query[$i] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-' . $format,
'operator' => 'NOT IN'
);
$i ++;
}
}
// If we don't have a category, get all categories
if ( trim( $category_id ) === '' ) {
$args = array(
'orderby' => $cat_orderby,
'order' => $cat_order,
'hide_empty' => 1,
'hierarchical' => 0,
'include' => $cat_include,
'exclude' => $cat_exclude,
'taxonomy' => 'category'
);
$cats = get_categories( $args );
}
else {
$cats = array( get_category( $category_id, OBJECT ) );
}
// Start output buffering
ob_start();
// Iterate through available categories and output recent posts
foreach ( $cats as $cat ) {
// Build array of arguments for wp_get_recent_posts()
$args = array( 'numberposts' => $count, 'post_status' => 'publish', 'order' => $order, 'orderby' => $orderby, 'tax_query' => $tax_query );
$args['category'] = $cat->cat_ID;
$category_link = get_category_link( $cat->cat_ID );
// Get recent posts matching query
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
<div class="independent_publisher_recent_posts">
<h2><?php echo esc_attr( $cat->name ); ?></h2>
<ul>
<?php
foreach ( $recent_posts as $recent ) {
echo '<li><a href="' . get_permalink( $recent["ID"] ) . '" title="' . esc_attr( $recent["post_title"] ) . '" >' . esc_attr( $recent["post_title"] ) . '</a> </li> ';
}
?>
</ul>
<?php if ( trim( $more_text ) !== FALSE && trim( $category_link ) !== FALSE ) : // Show more link if applicable ?>
<?php echo '<p class="independent_publisher_recent_posts_more_link"><a href="' . esc_url( $category_link ) . '" title="' . esc_attr( $cat->name ) . '">' . esc_attr( $more_text ) . '</a></p>'; ?>
<?php endif; ?>
</div>
<?php
}
// Return recent posts HTML
return ob_get_clean();
}
endif;
add_shortcode( 'independent_publisher_recent_posts', 'independent_publisher_recent_posts_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment