Using a WordPress shortcode enables you for posting of individual blog posts based on ID, latest number of blog posts from a certain category or all categories, optional pagination, optional excerpt or full content showing and optional meta.
Instructions: Add the following code to the functions.php of your WordPress theme to enable the new shortcode.
<?php /************* SHORTCODES ****************/ // function showBlogPosts( $atts ) { extract( shortcode_atts( array( // what categories to show, default = all. 'cat' => '', // number of posts 'num' => 5, 'p' => '', // The following variables work on an "on/off" switch, 1 to allow, 0 to disallow // excerpt or content. default = excerpt 'excerpt' => 1, // show meta or not. default = yes 'meta' => 1, 'meta2' => 1, //paged. default on no. Determines if page should be paginated or not 'pnavi' => 0 ), $atts ) ); global $loopMeta, $loopExcerpt, $loopMeta2; $loopMeta = $meta; $loopExcerpt = $excerpt; $loopMeta2 = $meta2; if ($p != ''){ $posts_per_page = ''; } wp_reset_query(); global $paged; $args = array( 'posts_per_page' => $num, 'cat'=> $cat, 'p' => $p ); if($pnavi == 1){ $args['paged'] = get_query_var('page'); } query_posts($args ); while ( have_posts() ) : the_post(); /* for the loop part, use your own loop.php that contains only the post format. ** See my "micrLoopPart.php" to see how all the options play out */ include(locate_template('microLoopPart.php')); endwhile; if($pnavi == 1){ echo'<div id="blogNav">'; posts_nav_link( ' ', '»', '«' ); echo '</div>'; } wp_reset_query(); } add_shortcode( 'blogPosts', 'showBlogPosts' ); ?>
Now, create a new template file called microLoopPart.php on your root folder and add the following micro formatted loop.
<article id="post-<?php the_ID();?>" <?php post_class($classes);?> ><!-- START OF POST --> <h1 class="entry-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>" class="url"> <?php the_title(); ?> </a></h1> <span class="meta vcard" style=" <?php if($loopMeta == 0){ echo "display:none"; }?> "> <time class="updated" datetime="<?php $postDate = get_the_date('c'); $postDate2 = get_the_date('d.m.Y'); echo $postDate ?>" pubdate> <?php echo $postDate2; ?></time> | <span class="byline fn author"> <?php the_author_posts_link(); ?> </span> | <a href="mailto:<?php the_author_meta('email'); ?>" class="email author">email</a> | <?php the_category(', '); ?> </span></span> <div class="postContent entry-content"> <?php if($loopExcerpt == 0){ the_content(); } else{ if ( has_post_thumbnail() ) { the_post_thumbnail('thumbnail', array('class' => 'alignleft')); } the_excerpt(); echo '<div class="clearfix"></div>'; } ?> <p class="postmetadata" style=" <?php if($loopMeta2 == 0){ echo "display:none"; }?> ">Posted in <?php the_category(', '); ?> <br /> <?php the_tags(); ?> <br /> Source: <span class="vcard"><span class="source-org copyright"> <?php bloginfo('name'); ?> </span></span></p> </div> </article>
Lastly, you need to add the following shortcode within your blog posts to display a micro formatted list of posts.
[blogPosts cat=3 excerpt=0 meta=1 meta1=0 num=2]
You may also enjoy checking if shortcode already exists.
Parse error: syntax error, unexpected ‘<' in C:xampp3htdocsmrcwp-contentthemesmrcmicroLoopPart.php on line 6
Hi,
I really your code but like John said the pagination is not working. It will show the link but on next pages (like page 2) it still show the page one content. Any idea?
Regards
Michael
Thanks for this! – Getting the_excerpt to show for custom queried posts in the sidebar on *single.php* (oddly, it worked on all other pages) was killing me.
I tried rolling my own shortcode but i couldn’t quite get it to work. This was really handy, and fixed the problem …
I dont know if its a bug in WP 3.3 but resetting queries, rewinding, trying three diferent query types – from wp_query to get_posts just didnt work on single.php…i was just trying to create a loop outside the main loop – and they all worked on *every page* except on single posts. So frustrating!
thanks again!
This is a great contribution. Thank you. I have a problem though on my theme. Everything seems to work except when I add the pagination, the link is there for the pages and it goes to those pages, but every page shows the latest posts (in this example, the latest 2) This is the shortcode I used. Copied the code above without changing
[blogPosts
cat=3 excerpt=0 meta=1 meta1=0 num=2 pnavi=1]
Any ideas? Using WP 3.3.1
Thanks