0

I have an AJAX call that pulls the next page of posts on my home.php template. Now I want the total number of posts as a variable in the javascript file, so that I can use it in an if-statement to hide the loadmore button. The script is localised as follows:

// Register the script

wp_register_script( 'blog-ajax-script', get_stylesheet_directory_uri(). '/assets/js/blog-ajax-script.js', array('jquery'), false, true );

// Localize the script with new data
$script_data_array = array(
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'security' => wp_create_nonce( 'load_more_posts' ),
);

wp_localize_script( 'blog-ajax-script', 'blog', $script_data_array );

// Enqueued script with localized data.
wp_enqueue_script( 'blog-ajax-script' );

How can I get the total number of posts as a variable in the javascript file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.