渲染wordpress短代码而不在循环中使用the_content()

Rah*_*eel 1 wordpress wordpress-theming wordpress-plugin

我在index.php页面中有以下代码,用于获取所有页面并更改它们.问题是我没有用来the_content()显示内容,而是使用echo $page->post_content().我现在必须在我的页面中嵌入一个联系表格7,但是当我在这里阅读不同的问题时,如果没有,则短代码将无效the_content().

我的问题是如何在这里展示联系表格.

$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
}
Run Code Online (Sandbox Code Playgroud)

rne*_*ius 6

您可以将the_content过滤器应用于post_content:

<?php echo apply_filters('the_content', $page->post_content); ?>
Run Code Online (Sandbox Code Playgroud)

它也有可能do_shortcode():

<?php echo do_shortcode($page->post_content); ?>
Run Code Online (Sandbox Code Playgroud)