Get page content and execute shortcodes on a page

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' );

Related posts

2 comments

  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.

  2. 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;

Comments are closed.