插件不排队脚本或样式 - 自定义查询
Plugin Doesn't Enqueue Script or Style - Custom Query
我正在使用找到的 Pro Blog Design 插件 here。它添加了一个按钮来加载更多内容。它在我的存档和搜索页面上运行良好,但在自定义页面上根本不起作用(例如:"page-customname.php")。
自定义页面还有一个带分页的自定义查询循环:
<?php $the_query = array('post_type' => 'post', 'post_status' => 'publish', 'category_name' => 'stories');
// Get current page and append to custom query parameters array
$the_query['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $the_query );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
echo 'loop items go here';
endwhile;
endif;
wp_reset_postdata();
$wp_query = NULL;
$wp_query = $temp_query;
?>
此循环的末尾是插件分页,再次在存档和搜索上运行良好,但在自定义查询页面上运行不佳。
我可以看出脚本和样式没有入队,因为插件 css 样式不起作用(仅在此页面上)。
这可能是什么原因?
插件功能如下:
function pbd_alp_init() {
global $wp_query;
// Add code to index pages.
if( !is_singular() ) {
// Queue JS and CSS
wp_enqueue_script(
'pbd-alp-load-posts',
plugin_dir_url( __FILE__ ) . 'js/load-posts.js',
array('jquery'),
'1.0',
true
);
wp_enqueue_style(
'pbd-alp-style',
plugin_dir_url( __FILE__ ) . 'css/style.css',
false,
'1.0',
'all'
);
// What page are we on? And what is the pages limit?
$max = $wp_query->max_num_pages;
$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('template_redirect', 'pbd_alp_init');
编辑: 更新了自定义查询,更简单,没有 null
<?php
// Define custom query parameters
$the_query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'stories',
'paged' => get_query_var('paged')));
while ( $the_query->have_posts() ) : $the_query->the_post();
echo 'loop stuff';
endwhile;
wp_reset_postdata();?>
编辑:工作解决方案!
问题出在 javascript。 !is_singular()
需要更改为不会否定自定义页面的内容,例如:!is_home();
此外,javascript 无法识别查询或其关联的页码,因此我不得不将 $global wp_query
更改为 $wp_query = new WP_Query( array('post_type' => 'post', 'post_status' => 'publish'));
,这可能不是最佳解决方案,但是它似乎适用于所有页面。
行 if( !is_singular() ) {
在存档和搜索页面上的计算结果为 TRUE,因为这些页面不显示单个资源 (is_singular() returns false 然后被前导“否定” !”)。所以 if 块中的代码永远不会出现在您的页面上 运行 因为 is_singular 计算结果为 TRUE 然后被“!”否定。
我正在使用找到的 Pro Blog Design 插件 here。它添加了一个按钮来加载更多内容。它在我的存档和搜索页面上运行良好,但在自定义页面上根本不起作用(例如:"page-customname.php")。
自定义页面还有一个带分页的自定义查询循环:
<?php $the_query = array('post_type' => 'post', 'post_status' => 'publish', 'category_name' => 'stories');
// Get current page and append to custom query parameters array
$the_query['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $the_query );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
echo 'loop items go here';
endwhile;
endif;
wp_reset_postdata();
$wp_query = NULL;
$wp_query = $temp_query;
?>
此循环的末尾是插件分页,再次在存档和搜索上运行良好,但在自定义查询页面上运行不佳。
我可以看出脚本和样式没有入队,因为插件 css 样式不起作用(仅在此页面上)。
这可能是什么原因?
插件功能如下:
function pbd_alp_init() {
global $wp_query;
// Add code to index pages.
if( !is_singular() ) {
// Queue JS and CSS
wp_enqueue_script(
'pbd-alp-load-posts',
plugin_dir_url( __FILE__ ) . 'js/load-posts.js',
array('jquery'),
'1.0',
true
);
wp_enqueue_style(
'pbd-alp-style',
plugin_dir_url( __FILE__ ) . 'css/style.css',
false,
'1.0',
'all'
);
// What page are we on? And what is the pages limit?
$max = $wp_query->max_num_pages;
$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('template_redirect', 'pbd_alp_init');
编辑: 更新了自定义查询,更简单,没有 null
<?php
// Define custom query parameters
$the_query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'stories',
'paged' => get_query_var('paged')));
while ( $the_query->have_posts() ) : $the_query->the_post();
echo 'loop stuff';
endwhile;
wp_reset_postdata();?>
编辑:工作解决方案!
问题出在 javascript。 !is_singular()
需要更改为不会否定自定义页面的内容,例如:!is_home();
此外,javascript 无法识别查询或其关联的页码,因此我不得不将 $global wp_query
更改为 $wp_query = new WP_Query( array('post_type' => 'post', 'post_status' => 'publish'));
,这可能不是最佳解决方案,但是它似乎适用于所有页面。
行 if( !is_singular() ) {
在存档和搜索页面上的计算结果为 TRUE,因为这些页面不显示单个资源 (is_singular() returns false 然后被前导“否定” !”)。所以 if 块中的代码永远不会出现在您的页面上 运行 因为 is_singular 计算结果为 TRUE 然后被“!”否定。