I didn’t test anything, but I imagine it would be something like this:
As you must include a sidebar.php
, just include an empty sidebar.php
, and leave the real one to the sidebar-ajax.php
, for example.
To call the sidebar:
<?php get_template_part('sidebar'); ?>
And it should be something like this:
<div id="sidebar"></div>
In your functions.php
:
add_action('wp_enqueue_scripts', 'theme_enqueue_scripts');
function theme_enqueue_scripts() {
wp_enqueue_script('jquery');
/* load your js file in footer */
wp_enqueue_script('theme-script', get_stylesheet_directory_uri() . '/your-js-file.js', false, false, true);
}
add_action('wp_ajax_get_ajax_sidebar', 'check_ajax');
add_action('wp_ajax_nopriv_get_ajax_sidebar', 'check_ajax');
function check_ajax() {
?>
get_template_part('sidebar-ajax');
<?php
}
And in your-js-file.js
:
jQuery.ajax({
type: 'POST',
url: location.href,
data: { get_ajax_sidebar: 1 },
success: function(data){
jQuery('#sidebar').html(data);
}
});