admin管理员组

文章数量:1389903

I have a CPT with an ACF number field. Now I want to get the total out of all existing numbers when ever a new CPT is created without refreshing the page. Using setTimeout does not seem to work here. Any other ideas how to accomplish this?

PHP

function counter_script() {

wp_register_script('counter', get_template_directory_uri() . '/js/counter.js'); 
wp_enqueue_script('counter');   

global $post;
$postargs = array(
    'post_type' => 'cpt'
);
$cpt_query = new WP_Query($postargs);
if ($cpt_query->have_posts()) : while ($cpt_query->have_posts()) :  $cpt_query->the_post();
    $numbersArray += get_field('numbers'); 
    endwhile; 
endif; 
wp_reset_query();

wp_localize_script( 'counter', 'numbers', $numbersArray);

}
add_action( 'wp_enqueue_scripts', 'counter_script' );

Script

function updateNumbers(){
    $(".counter").html(numbers);
    setTimeout(updateNumbers, 5000);
}

updateNumbers();

I have a CPT with an ACF number field. Now I want to get the total out of all existing numbers when ever a new CPT is created without refreshing the page. Using setTimeout does not seem to work here. Any other ideas how to accomplish this?

PHP

function counter_script() {

wp_register_script('counter', get_template_directory_uri() . '/js/counter.js'); 
wp_enqueue_script('counter');   

global $post;
$postargs = array(
    'post_type' => 'cpt'
);
$cpt_query = new WP_Query($postargs);
if ($cpt_query->have_posts()) : while ($cpt_query->have_posts()) :  $cpt_query->the_post();
    $numbersArray += get_field('numbers'); 
    endwhile; 
endif; 
wp_reset_query();

wp_localize_script( 'counter', 'numbers', $numbersArray);

}
add_action( 'wp_enqueue_scripts', 'counter_script' );

Script

function updateNumbers(){
    $(".counter").html(numbers);
    setTimeout(updateNumbers, 5000);
}

updateNumbers();
Share Improve this question asked Mar 27, 2020 at 15:39 heshes 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Ok. Solved.

PHP

function counter_script() {

wp_enqueue_script('counter', get_template_directory_uri() . '/js/counter.js');  

wp_localize_script( 'counter', 'numbers', array(
    'ajaxurl' => admin_url( 'admin-ajax.php' )
));

}
add_action( 'wp_enqueue_scripts', 'counter_script' );

add_action( 'wp_ajax_nopriv_numberstotal', 'numberstotal' );
add_action( 'wp_ajax_numberstotal', 'numberstotal' );

function numberstotal() {

global $post;
$postargs = array(
    'post_type' => 'cpt',
    'posts_per_page' => -1
);
$cpt_query = new WP_Query($postargs);
if ($cpt_query->have_posts()) : while ($cpt_query->have_posts()) :  
$cpt_query->the_post();
    $numbersArray += get_field('numbers'); 
    endwhile; 
endif; 
wp_reset_query();

echo json_encode($minutesArray);

wp_die();
}

Script

function updateNumbers(){
    $.ajax({
    url: numbers.ajaxurl,
    data: {
        action: 'numberstotal'
    },
    success: function (response) {
        $('.counter').html(response)
    }
    });
    setTimeout(updateNumbers, 5000);
}

updateNumbers();

本文标签: phpACF values to Script with auto refresh