我创建了一个搜索表单,通过类别过滤器和关键字输入进行搜索。搜索表单代码在此处-
<form action="<?php bloginfo(\'url\'); ?>" method="get" role="search" class="dropdown-form">
<div class="input-group">
<span class="input-group-addon">
<?php
wp_dropdown_categories(array(
\'show_option_all\' => \'all categories\',
\'class\' => \'search_cats\'
));
?>
</span>
<input type="search" class="form-control" placeholder="<?php esc_html_e(\'Search anything...\', \'onepro\'); ?>" name="s">
<span class="input-group-addon">
<button type="submit"><i class="ion-android-arrow-forward"></i></button>
</span>
</div>
</form>
然后我添加了
pre_get_posts
钩住底部
functions.php
文件-
add_action(\'pre_get_posts\', function() {
global $wp_query;
if (is_search()) {
$cat = intval($_GET[\'cat\']);
$cat = ($cat > 0) ? $cat : \'\';
$wp_query->query_vars[\'cat\'] = $cat;
}
});
搜索表单也在运行。但下面的告示出现在我使用的地方
WP_Query()
显示职位类别的步骤-
注意:is\\U search调用不正确。运行查询之前,条件查询标记不起作用。在此之前,它们总是返回false。有关详细信息,请参阅WordPress中的调试。(此消息是在版本3.1.0中添加的。)在C:\\xampp\\htdocs\\onepro\\wp中包含\\个函数。php在线3996
以下是查询代码-
global $wp_query;
global $paged;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query(array(
\'post_type\' => \'post\',
\'posts_per_page\' => $atts[\'show_posts\'],
\'paged\' => $paged,
));
if ( $wp_query->have_posts() ) :
$all_cat_slug = array();
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$category = get_the_category();
foreach( $category as $cat ){
array_push($all_cat_slug, $cat->slug);
}
endwhile;
$all_cat_slug = array_unique( $all_cat_slug );
endif;
<!--Portfolio Filter-->
<div class="row filters_row text-left">
<ul class="nav navbar-nav" id="blogs_filters">
<li data-filter="*" class="active"><?php echo esc_html__(\'all\', \'onepro-essential\'); ?></li>
<?php
foreach( $all_cat_slug as $cs ){
$catname = get_category_by_slug( $cs );
echo \'<li data-filter=".category-\'. $cs .\'">\'. $catname->name .\'</li>\';
}
?>
</ul>
</div>
错误通知出现在显示错误屏幕截图的类别之前-
如何解决此问题?
SO网友:Aness
Thanks @birgire. Got a similar issue here with the following error message and got it fixed up by commenting out the line : $wp_query = null;
Error message:
Notice : is_home was called incorrectly . Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.) in (MYsyspath)/functions.php on line 4146
And it worked, the culprit was the global variable unset action on my index.php
$wp_query = null; to //$wp_query = null;