首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress,shortcode在刷新页面后获取旧值

Wordpress,shortcode在刷新页面后获取旧值
EN

Stack Overflow用户
提问于 2018-06-04 14:41:53
回答 1查看 116关注 0票数 0

我有打印最新新闻的快捷码。

代码语言:javascript
复制
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

代码语言:javascript
复制
jQuery(document).ready(function($){
    var data = {
        action: 'updateNews'
    };

    $.post(window.news.ajax_url, data, function (res) {}, 'json');

});

以及php文件中的操作本身。

代码语言:javascript
复制
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分钟后,我刷新了页面,页面中没有任何变化。只有在单击刷新按钮两次后,页面中的信息才会更新。在一次刷新之后,它看起来信息在数据库中更新了,但短码仍在使用旧的值。

EN

回答 1

Stack Overflow用户

发布于 2018-06-04 17:29:02

尝尝这个

代码语言:javascript
复制
update_option('my_settings', $optionsNew);

因为你的代码中有类型错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50674695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档