Created
March 27, 2013 16:37
-
-
Save krogsgard/5255803 to your computer and use it in GitHub Desktop.
Recent posts shortcode example. Inspired by Bill Erickson's excellent Display Posts shortcode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_shortcode( 'infom-feature', 'infom_featured_items_shortcode' ); | |
function infom_featured_items_shortcode( $atts ) { | |
extract(shortcode_atts(array( | |
'post_type' => 'page', | |
'id' => false, | |
'include_excerpt' => false, | |
'image_size' => false, | |
'posts_per_page' => 3, | |
'read_more' => false | |
), $atts)); | |
$post_type = sanitize_text_field( $post_type ); | |
$read_more = sanitize_text_field( $read_more ); | |
$include_excerpt = (bool)$include_excerpt; | |
$image_size = sanitize_key( $image_size ); | |
$posts_per_page = intval( $posts_per_page ); | |
$args = array ( | |
'post_type' => explode( ',', $post_type ), | |
'posts_per_page' => $posts_per_page | |
); | |
if( $id ) { | |
$posts_in = array_map( 'intval', explode( ',', $id ) ); | |
$args['post__in'] = $posts_in; | |
} | |
$krogsquery = new WP_Query( $args ); | |
if ( $krogsquery->have_posts() ) { | |
$output = '<div class="featured-posts">'; | |
while ( $krogsquery->have_posts() ) { | |
$krogsquery->the_post(); | |
$output .= '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>'; | |
if ( $image_size && has_post_thumbnail() ) | |
$output .= '<a class="image" href="' . get_permalink() . '">' . get_the_post_thumbnail( get_the_ID(), $image_size ) . '</a> '; | |
if ( $include_excerpt ) | |
$output .= ' <div class="excerpt">' . wpautop( get_the_excerpt() ) . '</div>'; | |
if ( $read_more ) | |
$output .= '<a class="read-more button" href="' . get_permalink() . '">' . $read_more . '</a>'; | |
} | |
$output .= '</div>'; | |
} | |
wp_reset_postdata(); | |
return $output; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment