Web Development & WordPress

Show Specific Post Content into a page or sidebar using Shortcode and Post ID

Suppose you want to show specific post content into your homepage or any other page or in sidebar using shortcode, using the code i shared below you can do it. The shortcode works like this [copy_post_content id=”YOUR POST ID”].

You just need to paste the code below in your child theme’s function.php or you can use any plugins to utilize it.

function copy_post_content_shortcode($atts) {
    // Extract shortcode attributes
    $atts = shortcode_atts(array(
        'id' => 0,
    ), $atts);

    // Get the post content by ID
    $post_content = get_post_field('post_content', $atts['id']);

    // Return the post content
    return $post_content;
}
add_shortcode('copy_post_content', 'copy_post_content_shortcode');

Leave a comment