work on homepage
This commit is contained in:
@@ -392,30 +392,31 @@ function misha_my_load_more_scripts()
|
||||
global $wp_query;
|
||||
|
||||
// In most cases it is already included on the page and this line can be removed
|
||||
wp_enqueue_script('jquery');
|
||||
//wp_enqueue_script('jquery');
|
||||
|
||||
// register our main script but do not enqueue it yet
|
||||
wp_register_script('my_loadmore', get_stylesheet_directory_uri() . '/js/myloadmore.js', array('jquery'));
|
||||
//wp_register_script('my_loadmore', get_stylesheet_directory_uri() . '/js/myloadmore.js', array('jquery'));
|
||||
|
||||
// now the most interesting part
|
||||
// we have to pass parameters to myloadmore.js script but we can get the parameters values only in PHP
|
||||
// you can define variables directly in your HTML but I decided that the most proper way is wp_localize_script()
|
||||
wp_localize_script('my_loadmore', 'misha_loadmore_params', array(
|
||||
//wp_localize_script('my_loadmore', 'misha_loadmore_params', array(
|
||||
wp_localize_script('dis2019scripts', 'misha_loadmore_params', array(
|
||||
|
||||
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
|
||||
'posts' => json_encode($wp_query->query_vars), // everything about your loop is here
|
||||
'current_page' => get_query_var('paged') ? get_query_var('paged') : 1,
|
||||
'max_page' => $wp_query->max_num_pages,
|
||||
));
|
||||
|
||||
wp_enqueue_script('my_loadmore');
|
||||
// wp_enqueue_script('my_loadmore');
|
||||
}
|
||||
|
||||
add_action('wp_enqueue_scripts', 'misha_my_load_more_scripts');
|
||||
|
||||
|
||||
function misha_loadmore_ajax_handler()
|
||||
{
|
||||
|
||||
|
||||
// prepare our arguments for the query
|
||||
$args = json_decode(stripslashes($_POST['query']), true);
|
||||
$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded
|
||||
@@ -436,7 +437,6 @@ function misha_loadmore_ajax_handler()
|
||||
query_posts($args);
|
||||
get_template_part('loop');
|
||||
|
||||
|
||||
die; // here we exit the script and even no wp_reset_query() required!
|
||||
}
|
||||
|
||||
@@ -445,19 +445,19 @@ add_action('wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax
|
||||
|
||||
/* -------------------- different number of posts on home ------------------- */
|
||||
|
||||
add_action('pre_get_posts', 'dis_2019_more_posts_on_home', 1 );
|
||||
function dis_2019_more_posts_on_home(&$query) {
|
||||
add_action('pre_get_posts', 'dis_2019_more_posts_on_home', 1);
|
||||
function dis_2019_more_posts_on_home(&$query)
|
||||
{
|
||||
|
||||
//Before anything else, make sure this is the right query...
|
||||
if ( ! $query->is_home() ) {
|
||||
if (!$query->is_home()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//set post_per_page if it's homepage
|
||||
if (! $query->is_paged ) {
|
||||
$query->set('posts_per_page',14);
|
||||
}
|
||||
else {
|
||||
if (!$query->is_paged) {
|
||||
$query->set('posts_per_page', 14);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -470,7 +470,7 @@ function dis_2019_more_posts_on_home(&$query) {
|
||||
|
||||
// //Ensure we're modifying the right query object...
|
||||
// if ( $query->is_home() ) {
|
||||
// //Reduce WordPress's found_posts count by the offset...
|
||||
// //Reduce WordPress's found_posts count by the offset...
|
||||
// return $found_posts - $offset;
|
||||
// }
|
||||
// return $found_posts;
|
||||
|
||||
Reference in New Issue
Block a user