<?php
/*
* Usage:
*
* [embed_post slug="my-post"]
* [embed_post slug="my-post" full="false"]
* [embed_post type="movie" slug="inception"]
*/
function peterwebdesign_embedded_post_shortcode( $attributes ) {
// Extract shortcode attributes.
extract(
shortcode_atts(
array(
'type' => 'post',
'slug' => '',
'full' => true
),
$attributes
)
);
// Setup arguments.
$args = array(
// Get post type ("post" by default).
'post_type' => $type,
// Get post by slug.
'name' => $slug
);
// Instantiate new query instance.
$my_query = new WP_Query( $args );
// Check that we have query results.
if ( $my_query->have_posts() ) {
// Begin generating markup.
$output = '<section class="embedded-post">';
// Start looping over the query results.
while ( $my_query->have_posts() ) {
$my_query->the_post();
// Add title to output.
$output .= '<h2 class="embedded-post-title">';
$output .= get_the_title();
$output .= '</h2>';
// Get full post if `$full` is true, otherwise, show the get excerpt
if ( 'true' === $full ) {
// Add full content to output.
$output .= '<div class="embedded-post-content">';
$output .= get_the_content();
$output .= '</div>';
} else {
// Add excerpt to output.
$output .= '<div class="embedded-post-excerpt">';
$output .= get_the_excerpt();
$output .= '… <a href="' . get_permalink() . '">' . __( 'See full post', 'tutsplus' ) . ' »</a>';
$output .= '</div>';
}
}
// End generating markup.
$output .= '</section>';
} else {
// Output message to let user know that no posts were found.
$output = '<section class="embedded-post-error">';
$output .= '<p>' . __( 'No posts found.', 'tutsplus' ) . '</p>';
$output .= '</section>';
}
wp_reset_postdata();
return $output;
}
add_shortcode( 'embed_post', 'peterwebdesign_embedded_post_shortcode' );
?>