Skip to content

Instantly share code, notes, and snippets.

@vodanh1213
Created July 2, 2015 15:49
Show Gist options
  • Save vodanh1213/26f55104b17ab6bf699c to your computer and use it in GitHub Desktop.
Save vodanh1213/26f55104b17ab6bf699c to your computer and use it in GitHub Desktop.
Recent post global shortcode
add_shortcode( 'global_recent_posts', 'global_recent_posts_sc' );
function global_recent_posts_sc( $atts ) {
$data = wp_parse_args( $atts, array(
'order' => '',
'orderby' => '',
'is_meta' => 0,
'post_type' => 'post',
'post_status' => 'publish',
'recentglobalpostsdisplay' => 'title_content',
'recentglobalpostsnumber' => '5',
'recentglobalpoststitlecharacters' => '30',
'recentglobalpostscontentcharacters' => '100',
'recentglobalpostsavatars' => 'show',
'recentglobalpostsavatarsize' => '16',
'exclude_blogs' => '',
) );
$substr = function_exists( 'mb_substr' ) ? 'mb_substr' : 'substr';
extract( $data );
$args = array(
'post_type' => $post_type,
'post_status' => $post_status
);
if ( $is_meta ) {
$args['meta_key'] = $orderby;
$args['orderby'] = 'meta_value';
} else {
$args['orderby'] = $orderby;
}
$args['order'] = $order;
$query = network_query_posts( $args );
ob_start();
if ( network_have_posts() ) :
echo '<ul>';
while ( network_have_posts() ) :
network_the_post();
echo '<li>';
$post = network_get_post();
$the_permalink = network_get_permalink();
$the_title = network_get_the_title();
$the_content = network_get_the_content();
if ( $recentglobalpostsavatars == 'show' ) :
echo '<a href="', $the_permalink, '">', get_avatar( network_get_the_author_id(), $recentglobalpostsavatarsize, '' ), '</a> ';
endif;
$blog = get_blog_details( $post->BLOG_ID );
$blog_title = $blog ? $blog->blogname : '';
$title = $substr( $the_title, 0, $recentglobalpoststitlecharacters );
$content = $substr( strip_tags( $the_content ), 0, $recentglobalpostscontentcharacters );
switch ( $recentglobalpostsdisplay ) {
case 'blog_content':
echo '<a href="', $the_permalink, '">', '[', $blog_title, ']</a>';
echo '<br>';
echo $content, $recentglobalpostscontentcharacters < strlen( $the_content ) ? '&hellip;' : '';
echo '<br><a href="', $the_permalink, '">', __( 'Read More', 'rgpwidget' ), ' &raquo;</a>';
break;
case 'content':
echo $content, $recentglobalpostscontentcharacters < strlen( $the_content ) ? '&hellip;' : '';
echo '<br><a href="', $the_permalink, '">', __( 'Read More', 'rgpwidget' ), ' &raquo;</a>';
break;
case 'title':
echo '<a href="', $the_permalink, '">', $title, '</a>';
break;
case 'title_blog':
echo '<a href="', $the_permalink, '">', $title, ' [', $blog_title, ']</a>';
break;
case 'title_blog_content':
echo '<a href="', $the_permalink, '">', $title, ' [', $blog_title, ']</a>';
echo '<br>';
echo $content;
break;
case 'title_content':
default:
echo '<a href="', $the_permalink, '">', $title, '</a>';
echo '<br>';
echo $content;
break;
}
echo '</li>';
endwhile;
echo '</ul>';
endif;
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment