2

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' );
5
  • 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
    Commented Oct 14, 2013 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. Commented Oct 15, 2013 at 18:22
  • shortcodes should always return content, they can't directly print or echo, that's why your nested shortcode is breaking.
    – Milo
    Commented Oct 15, 2013 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. Commented Oct 15, 2013 at 18:44
  • your other shortcode needs to return its content.
    – Milo
    Commented Oct 15, 2013 at 18:46

2 Answers 2

1
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.

1
  • I would recommend adding square brackets around the shortcode name to properly get the shortcode content: do_shortcode( '[get_page]' ); Commented Aug 17, 2019 at 3:34
0

I had the same problem and code below solved it.

echo do_shortcode($pages[$i]->post_content);

It doesn't work:

  1. do_shortcode($pages[$i]->post_content);
  2. echo $pages[$i]->post_content;
1
  • But OP's code is a shortcode handler. That should return content, not echo it.
    – Rup
    Commented Feb 3, 2023 at 12:47

Your Answer

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

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