How to embed page content in a blog post

Create a shortcode to embed the content. This will always be synchronized.

Sample code from an older project. Just updated. 🙂

GitHub: https://gist.github.com/3380118 · This post in German (auf Deutsch) on my blog.

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: T5 Embed Post Shortcode
 * Description: Embed any page, post or custom post type with shortcode.
 * Plugin URI:  http://wordpress.stackexchange.com/q/62156/73
 * Version:     2012.08.17
 * Author:      Thomas Scholz
 * Author URI:  http://toscho.de
 * License:     MIT
 * License URI: http://www.opensource.org/licenses/mit-license.php
 *
 * T5 Embed Page Shortcode, Copyright (C) 2012 Thomas Scholz
 */

add_shortcode( 'embed_post', 't5_embed_post' );

/**
 * Get a post per shortcode.
 *
 * @param  array $atts There are three possible attributes:
 *         id: A post ID. Wins always, works always.
 *         title: A page title. Show the latest if there is more than one post
 *              with the same title.
 *         type: A post type. Only to be used in combination with one of the
 *              first two attributes. Might help to find the best match.
 *              Defaults to 'page'.
 * @return string
 */
function t5_embed_post( $atts )
{
    extract(
        shortcode_atts(
            array (
                'id'    => FALSE,
                'title' => FALSE,
                'type'  => 'page'
            ),
            $atts
        )
    );

    // Not enough input data.
    if ( ! $id and ! $title )
    {
        return;
    }

    $post = FALSE;

    if ( $id )
    {
        $post = get_post( $id );
    }
    elseif( $title )
    {
        $post = get_page_by_title( $title, OBJECT, $type );
    }

    // Nothing found.
    if ( ! $post )
    {
        return;
    }

    return apply_filters( 'the_content', $post->post_content );
}

Just make sure not to embed two posts vice versa.

Leave a Comment