不错的插件。一个主题的使用示例(用二十五尝试):
1.自定义模板页面
page-pjax.php
在主题内创建文件。
在管理员处,创建一个页面并使用此模板。它只是显示带有跨度的存档链接。
<?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.添加pjax插件和自定义脚本
在主题文件夹里面/js
添加jquery.pjax.js
和下面的脚本my-pjax.js
。
jQuery(document).ready(function($) {
$(document).pjax('span.a-pjax a, #recent-posts-2 a', '#pjax-container', {fragment:'#main'})
});
3. 加载 jQuery、pjax 和我们的自定义脚本
在functions.php
. 仅在模板页面上加载。
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. 注意事项
$(document).pjax(
'span.a-pjax a, #recent-posts-2 a', // ANCHORS
'#pjax-container', // TARGET
{fragment:'#main'} // OPTIONS
);
ANCHORS 是存档链接和具有 ID 的小部件最近的帖子#recent-posts-2
。为此测试删除它或根据需要添加另一个链接容器。
TARGET 在模板上是硬编码的。
选项,片段是必不可少的,因为 pjax 不会加载完整的 HTML 页面,我们必须告诉它我们想要目标页面的哪个部分。
25 日,内容在这个 div:<main id="main" class="site-main" role="main">
中。根据使用的主题进行调整。
请参阅 pjax 注释:https ://github.com/defunkt/jquery-pjax#response-types-that-force-a-reload