Shortcode: Embeddable Post Excerpts

With this custom shortcode you can quickly embed an excerpt of any post in any other post! This shortcode will be a great editorial tool for online magazines, review sites, or for any time you want to mention another post in your writing.

Here’s a preview of this shortcode in action (click for full size):

Embeddable Post Excerpt

Shortcode Usage

After installing the shortcode (follow steps 1 + 2 below) you can execute it anywhere WordPress accepts shortcodes. To connect to a post, you simply need to enter the post ID you wish to display:

[post_excerpt id="post_id"]

Tip: you can quickly get the ID of a post by editing it and searching the URL to grab the ID (screenshot below):

Get WordPress Post ID

Step 1: Paste the shortcode into your child theme’s functions.php file:

(…or download the code here to paste into your child theme)

/**
 * Creates the [post_excerpt] shortcode that will display a single
 * post by its ID by running a custom Loop. Shortcode displays post
 * title, excerpt, and featured image.
 *
 * Code snippet from: https://marketersdelight.net/snippets/embeddable-excerpts/
 *
 * @since 1.0
 */

function md_post_excerpt( $atts, $content = '' ) {
	extract( shortcode_atts( array(
		'id'    => '',
		'title' => '',
		'desc'  => ''
	), $atts, 'post_excerpt' ) );

	if ( empty( $atts['id'] ) )
		return;

	$loop = new WP_Query( array(
		'post_type' => 'post',
		'p'         => $atts['id']
	) );

	ob_start();
?>

	<?php if ( $loop->have_posts() ) : ?>
		<?php while ( $loop->have_posts() ) : $loop->the_post();
			$title = ! empty( $atts['title'] ) ? $atts['title'] : get_the_title();
			$desc  = ! empty( $atts['desc'] ) ? $atts['desc'] : get_the_excerpt();
		?>

			<div class="embed-excerpt block-single mb-single">

				<div class="columns-30-70 columns-single">

					<div class="col col1 mb-half text-center">
					    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'medium' ); ?></a>
					</div>

					<div class="col col2">
					    <h5><a href="<?php the_permalink(); ?>"><?php echo $title; ?></a></h5>
					    <p><?php echo $desc; ?></p>
					    <span class="links-sec"><a href="<?php the_permalink(); ?>"><?php _e( 'Read more →', 'md' ); ?></a></span>
					</div>

				</div>
			</div>

		<?php endwhile; ?>
		<?php wp_reset_postdata(); ?>
	<?php endif; ?>

<?php
	return ob_get_clean();
}

add_shortcode( 'post_excerpt', 'md_post_excerpt' );

Step 2: For shadow effects, paste this line into your style.css

This is optional, but adds a nice shadow to make the post excerpt stand out a little better:

.embed-excerpt {
	box-shadow: 0 7px 20px rgba(0, 0, 0, 0.12);
}

Bonus: Personalize Excerpt With Shortcode Attributes

By default this shortcode pulls the post title and content. When you execute the shortcode you can write-in your own custom title and excerpt text by simply defining these attributes to your shortcode:

[post_excerpt id="post_id" title="My Custom Title" desc="My custom description"]

Leave a Comment