Nice plugin. Usage example on a theme (tried with Twenty Fifteen):
1. Custom Template page
Create the file page-pjax.php
inside the theme.
At admin, create a page and use this template. It simply displays archive links with a span around them.
<?php
/**
* Template Name: Pjax Example
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$args = array(
'type' => 'daily',
'limit' => '',
'format' => 'html',
'before' => '<span class="a-pjax">',
'after' => '</span>',
'show_post_count' => true,
'echo' => 1,
'order' => 'DESC'
);
wp_get_archives( $args );
?>
<div id="pjax-container"></div>
</main>
</div>
<?php get_footer(); ?>
2. Add pjax plugin and a custom script
Inside the folder /js
on the theme add jquery.pjax.js
and the following script my-pjax.js
.
jQuery(document).ready(function($) {
$(document).pjax('span.a-pjax a, #recent-posts-2 a', '#pjax-container', {fragment:'#main'})
});
3. Load jQuery, pjax and our custom script
In functions.php
. Loads only on the template page.
add_action( 'wp_enqueue_scripts', 'load_scripts_so_43903250' );
function load_scripts_so_43903250() {
if ( is_page_template( 'page-pjax.php' ) ) {
wp_register_script( 'pjax', get_stylesheet_directory_uri() . '/js/jquery.pjax.js' );
wp_enqueue_script( 'my-pjax', get_stylesheet_directory_uri() . '/js/my-pjax.js', array('jquery','pjax') );
}
}
4. NOTES
$(document).pjax(
'span.a-pjax a, #recent-posts-2 a', // ANCHORS
'#pjax-container', // TARGET
{fragment:'#main'} // OPTIONS
);
ANCHORS are the archive links and the widget Recent Posts that has the ID #recent-posts-2
. Remove it for this test or add another links container as desired.
TARGET is hardcoded on the template.
OPTIONS, the fragment is essential as pjax doesn't load full HTML pages, we have to tell it which part of the target page we want.
On Twenty Fifteen, the content is inside this div: <main id="main" class="site-main" role="main">
. Adjust according to theme used.
See pjax notes: https://github.com/defunkt/jquery-pjax#response-types-that-force-a-reload