admin管理员组文章数量:1304893
I have a plugin that helps me to make lazy loading on posts of a custom post type. The idea is to add an action to the template_redirect/init and get data from the global $wp_query to make jquery understand how much loading there needs to be done.
The plugin php file looks like this:
function pbd_alp_init() {
global $wp_query;
// Add code to index pages.
if( !is_singular() ) { // can i select a specific post type here?
wp_enqueue_script(
'pbd-alp-load-posts',
plugins_url( 'js/load-posts.js', __FILE__ ),
array('jquery'),
'1.0',
true
);
// What page are we on? And what is the pages limit?
$max = $wp_query->max_num_pages; // this returns 0
$paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
// Add some parameters for the JS.
wp_localize_script(
'pbd-alp-load-posts',
'pbd_alp',
array(
'startPage' => $paged,
'maxPages' => $max,
'nextLink' => next_posts($max, false)
)
);
}
}
add_action('init', 'pbd_alp_init');
Im sure that max_num_pages function returns a number when i call it directly - why does my global $wp_query seem to be empty?
In the documentation it says that the action should hook to the 'template_redirect' but 'init' seem to be more right? Both doesn't hook up to the wp_query..
I have a plugin that helps me to make lazy loading on posts of a custom post type. The idea is to add an action to the template_redirect/init and get data from the global $wp_query to make jquery understand how much loading there needs to be done.
The plugin php file looks like this:
function pbd_alp_init() {
global $wp_query;
// Add code to index pages.
if( !is_singular() ) { // can i select a specific post type here?
wp_enqueue_script(
'pbd-alp-load-posts',
plugins_url( 'js/load-posts.js', __FILE__ ),
array('jquery'),
'1.0',
true
);
// What page are we on? And what is the pages limit?
$max = $wp_query->max_num_pages; // this returns 0
$paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
// Add some parameters for the JS.
wp_localize_script(
'pbd-alp-load-posts',
'pbd_alp',
array(
'startPage' => $paged,
'maxPages' => $max,
'nextLink' => next_posts($max, false)
)
);
}
}
add_action('init', 'pbd_alp_init');
Im sure that max_num_pages function returns a number when i call it directly - why does my global $wp_query seem to be empty?
In the documentation it says that the action should hook to the 'template_redirect' but 'init' seem to be more right? Both doesn't hook up to the wp_query..
1 Answer
Reset to default 1If you look at the Action Reference, you can see the order things happen.
Note where init
is, the query object doesn't exist until the wp
action.
// ...
init
└─ widgets_init
register_sidebar
wp_register_sidebar_widget
wp_default_scripts
wp_default_styles
admin_bar_init
add_admin_bar_menus
wp_loaded
parse_request
send_headers
parse_query
pre_get_posts
posts_selection
wp
// ...
本文标签: phpWhy doesn39t global wpquery not get hooked
版权声明:本文标题:php - Why doesn't global $wp_query not get hooked? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741796754a2397981.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论