javascript - php搜索表单过滤不显示所有类别
问题描述
我有一个问题,我在 wordpress\wp-content\themes\boombox\searchform.php 中添加了一个代码:
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
<input type="search" class="search-field" placeholder="Search..." value="<?php echo esc_attr( get_search_query() ); ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ); ?>" />
</label>
<?php
// output all of our Categories
// for more information see http://codex.wordpress.org/Function_Reference/wp_dropdown_categories
$swp_cat_dropdown_args = array(
'show_option_all' => __( 'Any Category' ),
'name' => 'swp_category_limiter',
);
wp_dropdown_categories( $swp_cat_dropdown_args );
?>
<input type="submit" class="search-submit" value="Search" />
</form>
在 wordpress\wp-content\themes\boombox\functions.php 我添加了代码:
function my_searchwp_include_only_category( $ids, $engine, $terms ) {
// if and only if a category ID was submitted:
// we only want to apply this limitation to the default search engine
// if you would like to change that you can do so here
if ( ! empty( $_GET['swp_category_limiter'] ) && 'default' == $engine ) {
$category_id = absint( $_GET['swp_category_limiter'] );
$category_args = array(
'category' => $category_id, // limit to the chosen category ID
'fields' => 'ids', // we only want the IDs of these posts
'posts_per_page' => -1, // return ALL posts
);
$ids = get_posts( $category_args );
// if there were no posts returned we need to force an empty result
if ( 0 == count( $ids ) ) {
$ids = array( 0 ); // this will force SearchWP to return zero results
}
}
// always return our $ids
return $ids;
我的代码有什么问题?谢谢你帮助我
解决方案
推荐阅读
- php - Yii2 | 共享主机上的高级项目模板错误的重定向路线
- visual-studio - Microsoft Visual Studio 2019 教程
- ruby-on-rails - 如何使用 AWS SDK(Ruby on Rails)将文件从 S3 存储桶直接下载到浏览器?
- vivado - Vivado 中的仿真目录
- php - 使用 .htaccess 文件重定向我的页面时,我的会话不会在更改的页面之间移动
- matplotlib - 声明后如何关闭cartopy中的网格线?
- google-chrome - 如何从背景获取框架 ID 到内容脚本?
- makefile - 在 Makefile 中使用 `-include` 有什么实际理由吗?
- php - 在 WooCommerce 3.6 中向单个产品附加信息表添加行
- vuejs2 - 如何使用 Vue.axios.delete() 删除数据