1

I would like to get show multiple page's content on one page with shortcodes. If I use return $html; the shortcodes not working, if I use return do_shortcode($html); the shortcode working, but the content of the page dissapear. What is the working solution?

function get_page_func( $atts ){
    extract(shortcode_atts( array(
        'title' => ''
    ), $atts ) );

    $page = get_page_by_title($title);

    $args = array(
        'include' => $page->ID,
    ); 
    $pages = get_pages($args); 

    $html = $pages[0]->post_content;

    return do_shortcode($html);
}

add_shortcode( 'get_page', 'get_page_func' );
  • your code works as-is for me with a simple test shortcode embedded in the page called by your shortcode. I imagine any shortcode that relies on knowing the context of the current page will fail though. – Milo Oct 14 '13 at 19:42
  • My shortcode: pastebin.com/d3w2ZYmF Sorry It was too long to paste. Update: if i change return to echo do_shortcode($html) the shortcode content is on the top of the page. – user1452062 Oct 15 '13 at 18:22
  • shortcodes should always return content, they can't directly print or echo, that's why your nested shortcode is breaking. – Milo Oct 15 '13 at 18:30
  • Yes, I read that, but If I use return do_shortcode($html) the page render only the shortcode's output, without the content on the page. – user1452062 Oct 15 '13 at 18:44
  • your other shortcode needs to return its content. – Milo Oct 15 '13 at 18:46
0
function get_page_func( $atts ){
    extract(shortcode_atts( array(
        'title' => ''
    ), $atts ) );

    $page = get_page_by_title($title);

    $args = array(
        'include' => $page->ID,
    ); 
    $pages = get_pages($args); 

    $html = $pages[0]->post_content;

    $html = do_shortcode($html);

    return $html;
}

add_shortcode( 'get_page', 'get_page_func' );

Use above code. Wherever you require shortcode to be executed just use the following line

echo do_shortcode('get_page');

This will work for PHP page and also work if you use a shortcode in post or page created using dashboard.

| improve this answer | |
  • I would recommend adding square brackets around the shortcode name to properly get the shortcode content: do_shortcode( '[get_page]' ); – John Zenith Aug 17 '19 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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