Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2. /*
  3. * Usage:
  4. *
  5. * [embed_post slug="my-post"]
  6. * [embed_post slug="my-post" full="false"]
  7. * [embed_post type="movie" slug="inception"]
  8. */
  9. function peterwebdesign_embedded_post_shortcode( $attributes ) {
  10. // Extract shortcode attributes.
  11. extract(
  12. shortcode_atts(
  13. array(
  14. 'type' => 'post',
  15. 'slug' => '',
  16. 'full' => true
  17. ),
  18. $attributes
  19. )
  20. );
  21. // Setup arguments.
  22. $args = array(
  23. // Get post type ("post" by default).
  24. 'post_type' => $type,
  25. // Get post by slug.
  26. 'name' => $slug
  27. );
  28. // Instantiate new query instance.
  29. $my_query = new WP_Query( $args );
  30. // Check that we have query results.
  31. if ( $my_query->have_posts() ) {
  32.  
  33. // Begin generating markup.
  34. $output = '<section class="embedded-post">';
  35.  
  36. // Start looping over the query results.
  37. while ( $my_query->have_posts() ) {
  38.  
  39. $my_query->the_post();
  40. // Add title to output.
  41. $output .= '<h2 class="embedded-post-title">';
  42. $output .= get_the_title();
  43. $output .= '</h2>';
  44. // Get full post if `$full` is true, otherwise, show the get excerpt
  45. if ( 'true' === $full ) {
  46. // Add full content to output.
  47. $output .= '<div class="embedded-post-content">';
  48. $output .= get_the_content();
  49. $output .= '</div>';
  50. } else {
  51. // Add excerpt to output.
  52. $output .= '<div class="embedded-post-excerpt">';
  53. $output .= get_the_excerpt();
  54. $output .= '&hellip; <a href="' . get_permalink() . '">' . __( 'See full post', 'tutsplus' ) . ' &raquo;</a>';
  55. $output .= '</div>';
  56. }
  57. }
  58. // End generating markup.
  59. $output .= '</section>';
  60. } else {
  61. // Output message to let user know that no posts were found.
  62. $output = '<section class="embedded-post-error">';
  63. $output .= '<p>' . __( 'No posts found.', 'tutsplus' ) . '</p>';
  64. $output .= '</section>';
  65. }
  66. wp_reset_postdata();
  67. return $output;
  68. }
  69. add_shortcode( 'embed_post', 'peterwebdesign_embedded_post_shortcode' );
  70. ?>