WordPress shortcode to make a list of your custom post type posts

Ever wanted to just drop in a list of posts from a post type into your page content? Well now you can with this simple shortcode.

add_shortcode( 'custom_posts', 'tcb_sc_custom_posts' );
function tcb_sc_custom_posts( $atts ){
  global $post;
  $default = array(
    'type'      => 'post',
    'post_type' => '',
    'limit'     => 10,
    'status'    => 'publish'
  );
  $r = shortcode_atts( $default, $atts );
  extract( $r );

  if( empty($post_type) )
    $post_type = $type;

  $post_type_ob = get_post_type_object( $post_type );
  if( !$post_type_ob )
    return '<div class="warning"><p>No such post type <em>' . $post_type . '</em> found.</p></div>';

  $return = '<h3>' . $post_type_ob->name . '</h3>';

  $args = array(
    'post_type'   => $post_type,
    'numberposts' => $limit,
    'post_status' => $status,
  );

  $posts = get_posts( $args );
  if( count($posts) ):
    $return .= '<ul>';
    foreach( $posts as $post ): setup_postdata( $post );
      $return .= '<li>' . get_the_title() . '</li>';
    endforeach; wp_reset_postdata();
    $return .= '</ul>';
  else :
    $return .= '<p>No posts found.</p>';
  endif;

  return $return;
}

Here is my example for my glossary plugin posts [custom_posts type=’glossary’]

[custom_posts type=’glossary’]

YouTube video thumbnails using WordPress shortcodes

We all love a good video on YouTube. This simple snippet lets you insert a thumbnail, with a link to the video you want to show, using a WordPress shortcode. All you need is the video ID to pop in the shortcode attributes.

add_shortcode( 'tcb_youtube', 'tcb_youtube_video_thumbnail' );
function tcb_youtube_video_thumbnail( $atts ) {
  extract( shortcode_atts( array(
    'id'    => 'RxoZ-AZKpX8',
    'size'  => '0',
    'align' => 'left'
  ), $atts ) );
  // Allow more human readable 'small' and 'large' to be used.
  $sizes = array( 'large' => 0, 'small' => 1, 'l' => 0, 's' => 1 );
  if ( isset( $sizes[$size] ) ) $size = $sizes[$size];

  $img_src = 'http://img.youtube.com/vi/' . $id . '/' . $size . '.jpg';
  $href    = 'http://youtu.be/' . $id;

  return '<a href="' . $href . '"><img class="hiddenSpellError" alt="" />src="' . $img_src . '" alt="" class="align' . $align . '" /></a>';
}

Example YouTube video thumbnail shortcode:

[[tcb_youtube]]

[tcb_youtube]

Extending the attributes

This can easily be extended to include other attributes. If you don’t link youtu.be, you could change the base href. Alternatively you can wrap it with your own caption styling.

Embed private member only content in your posts and pages

You may be running a site where you want to tease people into registering for your content. Perhaps to gain access to a download, or a sure-thing bet for next week’s game. Using a simple shortcode filter we can do this with elegance.

Private content shortcode

function tcb_private_shortcode($atts, $content=null) {
  $default = array( 'alt' => '<em>Private content, login to view it.</em>' );
  extract( shortcode_atts($default, $atts) );
  if( is_user_logged_in() && !empty($content) && !is_feed() )
    return $content;
  return $alt;
}
add_shortcode('private', 'tcb_private_shortcode');

It can easily be extended to include the WordPress login form.

Adding your public Google+ posts to WordPress

This is amazingly simple!

Well, I say that, there is a small warning that comes with this. There is no official RSS feed for Google Plus. Any feeds made available cannot be relied on to last forever, either due to the third-party losing interest or money, or because Google release their own.

The first Google Plus Feed

The first Google Plus RSS feed on AppSpot
The first Google Plus RSS feed on AppSpot

Just back in July ghacks.net blogged on this very topic. They suggested using plusfeed on AppSpot. As you can see, that has now closed.

The second Google Plus feed
However, some enterprising people have created plusfeed2. It really as simple as grabbing your Google Plus ID and appending it to the plusfeed2 url and putting it in an RSS widget. Just like any other blog.

Your Google Plus posts in you page content
If you don’t want to be restricted to your widgetized sidebars, you can insert your Google Plus posts directly into your posts using a shortcode:

[[rssfeed feed="http://plusfeed2.appspot.com/104120819592562195182"]]

[rssfeed feed=”http://plusfeed2.appspot.com/104120819592562195182″]

NOTE: This way of adding Google+ posts relies on a third-party RSS feed, so it extends the embedding RSS feeds into your post content idea. Please make sure you have that short code working before trying it out. Alternatively, if you just want your Google+ posts in a sidebar widget, then you can use it normally.