Monday, August 13, 2012

Ultimate shortcode for displaying custom post types

Here you go, I made this shortcode to display any custom post type within your post or page. You can also use do_shortcode function to use this shortcode inside your php file.

Insert the following code inside your theme's functions.php and let the wordpress know about this newly created shortcode by add_shortcode() function.


function display_cpt_fx($atts) {
  extract( shortcode_atts( array(
  'cpt' => 'posts',
  'ppp' => '-1', //-1 = show all posts; 0 or above = post per page
  'orderby' => 'ASC',
  'title' => 'linked', // linked, unlinked, true, false
  'content' => 'true',
  'limit_post' => '300',
  'post_cont' => '...',
  'img' => '3,medium,linked,class', //0 = no image; 1 = wrapper Bg; 2= before title; 3 = after title; 4 = after content
  //full = full size; medium = medium size; thumbnail = thumbnail size
  // none = no link to image; linked = image linked to permalink; custom = custom link for image
  'img_custom_link' => 'none', //appends when $img link holder is set to custom. url validated. example = http://www.google.com
  'pagination' => 'true',
  'wrapper_before' => '<div class="cpt_wrap">',
  'wrapper_after' => '</div>',
  'block_before' => '<div class="cpt_item">',
  'block_after' => '</div>',
  'title_before' => '<h1>',
  'title_after' => '</h1>',
  'img_before' => "<span>",
  'img_after' => "</span>",
  'content_before' => "<p>",
  'content_after' => "</p>",
  'content_after_custom_element' => '',
  'callback_fx' => 'callback_fx',
  'fb_like' => 'false',
  'fb_share' => 'false',
  'fb_comments' => 'false,2,470,light',
  ), $atts ) );
  
  $output = null;
  
  $img_link_error_msg = '<span style="background:#f00; color: #fff; padding: 5px; border: 1px solid #666;">ERROR: Custom Link is invalid. Please correct it! (Example: http://www.site.com)</span>';
  
  $bool_img_link_validate = preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $img_custom_link);
  
  $img = explode(',', $img);
  
  $fb_comments = explode(',', $fb_comments);
  
  if(!empty($wrapper_before)){
  $wrapper_before = explode('>', $wrapper_before);
  $wrapper_before = $wrapper_before[0] . ' style="clear:both;">';
  }
  
  if ($pagination == 'true') {
  $pagination_string = '&paged=' . $paged;
  } elseif ($pagination == 'false') {
  $pagination_string ='';
  }
  //query
  $cpt_query = new WP_Query('post_type='.$cpt.'&orderby='.$orderby.'&posts_per_page='.$ppp . $pagination_string);
  $output = $wrapper_before;
  
  //loop
  while ($cpt_query->have_posts()) : $cpt_query->the_post();
  
  //get the thumbnail
  if (has_post_thumbnail( $post->ID ) ){ 
  $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), $img[1]); }
  
  //Call back arguments defination
  $call_back_args = array();
  array_push($call_back_args, get_the_title(), get_the_content(), get_permalink(get_the_ID()), $image[0] );
  
  //img pos 1
  if($img[0] == "1") {
  $block_before = explode('>', $block_before);
  $block_before = $block_before[0] . ' style="background:url('. $image[0] .') no-repeat;">';
  }
  $output .= $block_before;
  
  //img pos 2
  if ($img[0] == "2") {
    if($img[2]== "linked") {
    $output .= $img_before . '<a href="'. get_permalink(get_the_ID()) .'"><img class="' . $img[4]. '" src="'. $image[0] .'" /></a>' . $img_after;
    } elseif($img[2]== "custom" && $img_custom_link == "none") {
    $output .= $img_before . '<a href="'. get_permalink(get_the_ID()) .'"><img class="' . $img[4]. '" src="'. $image[0] .'" /></a>' . $img_before;
    } elseif ($img[2]== "custom" && !($img_custom_link == "none")) {
    if ($bool_img_link_validate) {
    $output .= $img_before . '<a href="'. $img_custom_link .'"><img class="' . $img[4]. '" src="'. $image[0] .'" /></a>' . $img_after;
    } else {
    $output .= $img_link_error_msg;
    }
  } elseif($img[2]== "none") {
  $output .= $img_before . '<img class="' . $img[4]. '" src="'. $image[0] .'" />' . $img_after;
  }
  }
  if ($title == "linked") {
  $output .= $title_before . '<a href="'. get_permalink(get_the_ID()) .'">' . get_the_title() . '</a>' . $title_after;
  } elseif ($title == "unlinked") {
  $output .= $title_before . get_the_title() . $title_after;
  } elseif ($title == "true") {
  $output .= get_the_title();
  } elseif ($title == "false") {}
  
  //img pos 3
  if ($img[0] == "3") {
  if($img[2]== "linked") {
  $output .= $img_before . '<a href="'. get_permalink(get_the_ID()) .'"><img class="' . $img[4]. '" src="'. $image[0] .'" /></a>' . $img_after;
  } elseif($img[2]== "custom" && $img_custom_link == "none") {
  $output .= $img_before . '<a href="'. get_permalink(get_the_ID()) .'"><img class="' . $img[4]. '" src="'. $image[0] .'" /></a>' . $img_after;
  } elseif ($img[2]== "custom" && !($img_custom_link == "none")) {
  if ($bool_img_link_validate) {
  $output .= $img_before . '<a href="'. $img_custom_link .'"><img class="' . $img[4]. '" src="'. $image[0] .'" /></a>' . $img_after;
  } else {
  $output .= $img_link_error_msg;
  }
  } elseif($img[2]== "none") {
  $output .= $img_before . '<img class="' . $img[4]. '" src="'. $image[0] .'" />' . $img_after;
  }
  }
  
  $this_content = get_the_content();
  if(!empty($this_content)){
  if($limit_post < 0) {
  $output .= $content_before. $this_content.$content_after;
  } else {
  $output .= $content_before. substr($this_content, 0, $limit_post) . '<a href="' . get_permalink(get_the_ID()) .'">' .$post_cont  . '</a>' . $content_after;
  }
  }
  $output .= $content_after_custom_element; 
  
  //img pos 4
  if ($img[0] == "4") {
  if($img[2]== "linked") {
  $output .= $img_before . '<a href="'. get_permalink(get_the_ID()) .'"><img class="' . $img[4]. '" src="'. $image[0] .'" /></a>' . $img_after;
  } elseif($img[2]== "custom" && $img_custom_link == "none") {
  $output .= $img_before . '<a href="'. get_permalink(get_the_ID()) .'"><img class="' . $img[4]. '" src="'. $image[0] .'" /></a>' . $img_after;
  } elseif ($img[2]== "custom" && !($img_custom_link == "none")) {
  if ($bool_img_link_validate) {
  $output .= $img_before . '<a href="'. $img_custom_link .'"><img class="' . $img[4]. '" src="'. $image[0] .'" /></a>' . $img_after;
  } else {
  $output .= $img_link_error_msg;
  }
  } elseif($img[2] == "none") {
  $output .= $img_before . '<img class="' . $img[4]. '" src="'. $image[0] .'" />' .$img_after;
  }
  }
  if($fb_like == 'true'){
  $output .= '<div class="fb-like" data-href="'.get_permalink().'" data-send="false" data-layout="button_count" data-width="450" data-show-faces="true"></div>';
  }
  
  if($fb_comments[0] == 'true') {
  $output .= '<div class="fb-comments" data-href="'.get_permalink().'" data-num-posts="'.$fb_comments[1].'" data-width="'.$fb_comments[2].'" data-colorscheme="'.$fb_comments[3].'"></div>';
  }
  
  $output .= $block_after;
  //call back function arguments: title, content, permalink, image url
  $cb_fx = $callback_fx;
  $output .= $cb_fx($call_back_args); //call-back function, the f(x) should return some value or empty space. Declare function inside functions.php with a unique name. it accepts 1 argument.
  endwhile; wp_reset_query();
  
  
  $output .= $wrapper_after;
  
  if ($paged == 'true') {
  $output .= '<nav>';
  $output .= '<span style="float:left">' . previous_posts_link("&laquo; Newer") . '</span>';
  $output .= '<span style="float:right">' . next_posts_link("Older &raquo;") . '</span>';
  $output .= '</nav>';
  }
  return $output;
  
  }
function callback_fx() {
  return '';
  }
  add_shortcode('display_cpt', 'display_cpt_fx');


Use the shortcode [display_cpt] in your post/ page to display the custom post type. By default, the cpt attribute is set to posts. You can modify any attributes change the behavior of this shortcode. For example:


[display_cpt cpt="projects"] will display all the posts within your projects custom post type.

No comments:

Post a Comment