Include content from one page to another

I’d like to include content from one Page (named “contact_text”) to another Page in a specific spot (both of these are WordPress admin pages).
I’ve followed this Stack Overflow thread.

However I don’t believe I can call a PHP function inside a WP Page, much less at the spot I’d like it to appear. Do I need to set up a shortcode for that function in order to use it?

Related posts

2 comments

  1. Another shortcode option (add the following to your functions.php). With this one, you have two options. You can query by the post/page ID (will work for any post type), or the page name/slug (only works for pages).

    // Add "Show Content" Shortcode
    function wpse_120886_show_content( $atts ) {
        // Attributes
        $a = shortcode_atts(
            array(
                'id' => null,
                'page_name' => null,
            ), $atts )
        );
        // Get Content & Return
        if ( $a['id'] ) {
            $the_post = get_post( $a['id'] );
            $the_content = $the_post->post_content;
        } elseif ( $a['page_name'] ) {
            $the_post = get_posts( array( 'name' => $a['page_name'], 'post_type' => 'page', 'post_status' => 'publish', 'posts_per_page' => 1 ) );
            $the_content = $the_post[0]->post_content;
        }
        return $the_content;
    }
    add_shortcode( 'show_content', 'wpse_120886_show_content' );
    

    Usage:

    By ID: [show_content id="ID_HERE"]

    By Name/Slug: [show_content page_name="contact_text"]

  2. You could take exactly that code and apply into a shortcode if you want.

    add_shortcode( 'show_post', 'show_post_shortcode' );
    
    function show_posts_shortcode( $atts ) {
        $defaults = array(
            'path' => null 
        );
    
        $attributes = shortcode_atts( $defaults, $atts );
    
        if ( null === $attributes['path'] )
            return;
    
        ob_start();
    
        show_post( $attributes['path'] );
    
        $contents = ob_get_contents();
        @ob_end_clean();
    
        return $contents;
    }
    

    You then just type [show_post path="about"] to get the about page content.

Comments are closed.