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

时间:2014-12-01 09:48:51

标签: wordpress wordpress-plugin wordpress-theming

我的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
}

1 个答案:

答案 0 :(得分:6)

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

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

也可以do_shortcode()

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