du9843 2016-07-08 12:44
浏览 21
已采纳

Wordpress 4.5.3:获取应用模板文件的内容?

Is there a way to get the content of an entire wordpress page? My problem is, I want to include page content on another page (to create a one-page layout). What I tried was this:

$post = get_post($the_page_id);
$content = apply_filters('the_content', $post->post_content);
echo $content;

But the page has its own template and I would like to display the entire page, including whatever is done in the template.php file. Is that possible?

  • 写回答

1条回答 默认 最新

  • duanfeigui6655 2016-08-04 09:38
    关注

    Well, I came up with a solution for this. It might not be the best way but this worked for me. I created a page "home" to represent the one-page page. All subpages of this page will have a certain template which does not include header or footer. My home template looks like this:

    //fetch header as usual
    get_header();
    
    //fetch all subpages of this page
    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'orderby' => 'menu_order', 'order' => 'ASC'));
    $children = get_page_children( get_the_ID(), $all_wp_pages );
    
    //browse these subpages and get the content for each one 
    foreach($children as $child){
        $post = get_post($child->ID);
        getOnePageContent($post);
    }
    
    //footer
    get_footer(); 
    

    Now in functions.php I defined the getOnePageContent:

    function getOnePageContent($page){
        global $post;
        $post = $page;
        $slug = get_page_template_slug( $page->ID );
        if($slug){
            $pl = get_the_permalink($page);
            $content = file_get_contents($pl);
            echo $content;
        } else {
            $content = apply_filters('the_content', $page->post_content);
            echo $content;
        }
        wp_reset_postdata();
    }   
    

    As long as the subpages do not contain headers or footers this works fine. Of course, I do not think it's an elegant or good solution to fetch the page with file_get_contents but at least it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算