我有打印最新新闻的快捷码。
add_shortcode('latest_news', 'news_articles');
function news_articles($atts){
$options = get_option('my_settings');
var_dump(date('Y m d H:i:s', $options['last_update']));
ob_start();
if($options['articles'] != ''){
include 'includes/list-shortcode.php';
}
$content = ob_get_clean();
return $content;
}我希望最新的新闻每x分钟自动更新一次。因此,我创建了js文件,它位于jquery之后,并带有动作updateNews
jQuery(document).ready(function($){
var data = {
action: 'updateNews'
};
$.post(window.news.ajax_url, data, function (res) {}, 'json');
});以及php文件中的操作本身。
add_action('wp_ajax_updateNews', 'updateNews_ajax');
add_action('wp_ajax_nopriv_updateNews', 'updateNews_ajax');
function updateNews_ajax(){
$options = get_option('my_settings');
$time_now = time();
$time_diff = $time_now - $options['last_update'];
if($time_diff > $options['update']){ // $options['update'] - update after x minutes
$articles = get_results();
$optionsNew['last_update'] = time();
$optionsNew['articles'] = $articles;
$optionsNew['update'] = $options['update'];
update_option('my_settings', $optionsNew);
}
echo json_encode(['res'=>'ok']);
wp_die();
}X分钟后,我刷新了页面,页面中没有任何变化。只有在单击刷新按钮两次后,页面中的信息才会更新。在一次刷新之后,它看起来信息在数据库中更新了,但短码仍在使用旧的值。
发布于 2018-06-04 17:29:02
尝尝这个
update_option('my_settings', $optionsNew);因为你的代码中有类型错误。
https://stackoverflow.com/questions/50674695
复制相似问题