Render wordpress shortcodes without using the_content() in loop

I have the following code in my index.php page that fetches all the pages and alter them. The problem is that i haven’t used the_content() to show the content rather i used echo $page->post_content(). I have to now embed a contact form 7 in my page but as i read on different questions here that shortcodes won’t work if there is no the_content().

My question is how can i show the contact form with any trick here.

$pages = get_pages($args); 
foreach ($pages as $page){
    // Override Homepage
    if($page->ID == 5){
        $page->post_content = file_get_contents(get_template_directory_uri().'/includes/homepage-override.php');
    }
    // Check if it is menu page
    if($page->post_parent == 169){
        $page->post_content = file_get_contents(get_template_directory_uri().'/includes/location-menu-page.php?location='.$page->post_title);
    }
    // Calling Gallery Images
    if($page->ID == 23){
        $page->post_content = file_get_contents(get_template_directory_uri().'/includes/gallery-page.php');
    }

    // Calling about page
    //if($page->ID == 7 || $page->ID == 17 || $page->ID == 29){
    if(has_post_thumbnail($page->ID)):
        $image = wp_get_attachment_image_src( get_post_thumbnail_id($page->ID), 'single-post-thumbnail' );
        $dom = new DOMDocument();
        @$dom->loadHTML($page->post_content);
        $imgs = $dom->getElementsByTagName("img");
        foreach($imgs as $img){
            $img->setAttribute( 'src' , $image[0] );
            break;
        //}
    }
    $page->post_content = $dom->saveHTML();
    endif;
    echo $page->post_content;
    //the_content(); This is not working
}

Related posts

Leave a Reply

1 comment

  1. You can apply the_content filters to post_content:

    <?php echo apply_filters('the_content', $page->post_content); ?>
    

    It also may be possible to do_shortcode():

    <?php echo do_shortcode($page->post_content); ?>