Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress wp_缓存在wp_头钩中无法工作_Wordpress_Caching - Fatal编程技术网

Wordpress wp_缓存在wp_头钩中无法工作

Wordpress wp_缓存在wp_头钩中无法工作,wordpress,caching,Wordpress,Caching,我一直在我的网站中使用wp_缓存功能,以提高主页的性能并避免多次查询 我在function.php中输入了如下代码: add_action( 'wp_head', 'set_data_in_cache' ); function set_data_in_cache(){ if ( is_front_page() ) { gloabl $wpdb; $dataInCache = wp_cache_get('post_data');

我一直在我的网站中使用wp_缓存功能,以提高主页的性能并避免多次查询

我在function.php中输入了如下代码:

add_action( 'wp_head', 'set_data_in_cache' );

function set_data_in_cache(){
    if ( is_front_page() ) {
        gloabl $wpdb;
        $dataInCache = wp_cache_get('post_data');           
        if( $dataInCache === false ){
            $result  = $wpdb->query('some query');
            wp_cache_set('post_data',$result,'',86400);         
        }
        else{
            //data in cache
        }
    }
}
我一直在检查,每次在页面加载时,$dataInCache变量都是空的。表示缓存中未存储任何数据。 一旦页面加载并将数据设置为缓存,它就不应该为空

但是$dataInCache=wp_cache_get('post_data');在车身标签内工作良好,数据显示良好


我的代码中有什么错误吗?

您需要在wp\u cache\u中使用force才能得到结果

wp_cache_get( $key, '', true );

另外,最好先定义一个组,然后获取缓存,这样就可以在头部使用它。

您尝试过“init”吗操作?实际上,你没有在文件的开头获得帖子数据,文件名为wp_headin'init'hook,我无法设置首页,所以需要使用wp_headhook@ahmed:我正在wp_head hook中获取post数据,它在页面中成功显示,但正如我告诉您的,当我刷新页面时,它没有存储在我想要的缓存中,每次查询都不应该运行,而是从缓存中获取数据并显示在页面上。