1

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?

2 Answers 2

2

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 should not be using extract() Check the docs on how to properly create a shortcode Dec 19, 2014 at 7:04
  • The clip was created per the instructions in the WordPress codex. I used extract, which Pieter didn't like, so it's updated.
    – BODA82
    Dec 22, 2014 at 22:25
0

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.

4
  • 1
    Why are you using output buffering here? Nov 1, 2013 at 9:08
  • 1
    @KristianVitozev thats because the linked show_post function directly echos. For shortcodes to work correctly you must return the content, else it will always appear before any other output.
    – Zogot
    Nov 1, 2013 at 9:14
  • Thank you Zogot...it doesn't work, perhaps I'm missing something. I've pasted the above function into my functions.php, then used [show_post path="contact_copy"] in a page named "contact" at the spot I want that page content to appear. The shortcode text appears on the rendered page, not the content. BTW the source permalink looks like this, but will be a www domain when it's live: 192.168.1.230/contact_copy
    – Elk
    Nov 1, 2013 at 17:50
  • Update: I think I'd found a typo (s on show_posts_shortcode), however the new result is a broken, mostly empty page. Perhaps my path to the source is wrong? Thanks again -B
    – Elk
    Nov 1, 2013 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.