Created
September 15, 2015 20:42
-
-
Save sl-digital/ef2c00a9db9249bd7628 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Template Name: AJAX Page | |
* | |
* This page template responds to the request type by serving only the content for an AJAX request | |
* and the content wrapped in the parent layout for a normal request. This allows for loading the | |
* content via AJAX and the History API without the need for hash-bang URLs. | |
*/ | |
$url = rtrim($_SERVER['REQUEST_URI'],"/"); | |
$content = ""; | |
$subtitle = ""; | |
$subcontent = ""; | |
//Get the sub-content | |
if(have_posts()){ | |
while(have_posts()){ | |
the_post(); | |
$subtitle = get_the_title(); | |
$subcontent = get_the_content(); | |
$subcontent = apply_filters('the_content',$subcontent); | |
} | |
} | |
if( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == "xmlhttprequest" ){ | |
header('Content-Type: application/json'); | |
header('Cache-Control: no-store'); | |
echo json_encode(array("title" => $subtitle,"content" => $subcontent)); | |
die(); | |
} else { | |
$ancestors = get_ancestors($post->ID,'page'); | |
if(!empty($ancestors)){ | |
$page = get_post($ancestors[0]); | |
$content = $page->post_content; | |
$content = apply_filters('the_content',$content); | |
} | |
get_header(); | |
echo $content; | |
?> | |
<div id='ajax-content'> | |
<?php echo $subcontent;?> | |
</div> | |
<?php get_footer(); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This template is meant to be used where a parent page contains the main layout and the child pages contain the content. If the child page is requested via AJAX, only the page title and contents from the editor are returned. If the page is requested with GET, such as a page refresh, then the parent content is loaded first followed by the child page content within an AJAX container.