我终于成功了。
找到这个post 这与我的任务有关。
根据这一点,首先,我们必须通过放置隐藏输入来编辑搜索表单。根据需要为输入命名。以及使用javascript更改表单提交时的输入值。
<input type=”hidden” name=”search_type” value=”site-search” />
其次,搜索。必须用以下代码替换为搜索结果创建的php或任何其他页面模板。通过设置控制隐藏输入值的条件,并相应地加载相关模板。
<?php
$search_refer = $_GET[“search_type”];
if ($search_refer == ‘icon - packs’) {
load_template(TEMPLATEPATH . ‘ / pack - search . php’);
} elseif ($search_refer == ‘icons’) {
load_template(TEMPLATEPATH . ‘ / icons - search . php’);
};
?>
为了在分类法中进行搜索并在搜索结果页面中将它们作为结果列出,我使用了一些wp query和其他相关功能,并创建了这个用于显示它们的自定义函数。
1) 功能。php-我创建了一个函数,将搜索到的短语和分类名称发送给它,它返回一个自定义分类的术语数组。
// We get a list taxonomies on the search box
function getTaxBySearch($search_text,$tax_name){
$args = array(
\'taxonomy\' => \'icon_package\', // taxonomy name
\'orderby\' => \'id\',
\'order\' => \'ASC\',
\'fields\' => \'all\',
\'name__like\' => $search_text
);
$terms = get_terms( $args );
$count = count($terms);
if($count>0){
return $terms;
}else{
return 0;
}
}
最后,为每种搜索类型(图标、图标包)制作搜索结果页面模板
<?php get_header(); ?>
<?php $terms = getTaxBySearch($args[\'searched_phrase\'], \'icon_package\'); ?>
<?php if ($terms > 0) : ?>
<div class="icons-search__container">
<div class="icons-header">
<h1> Free SVG Flat Icon Packs</h1>
<p></p>
</div>
<div class="item-grid-masonry" data-controller="item-grid--masonry" data-item-grid--masonry-has-masonry-value="true" data-item-grid--masonry-scrollbar-fix-value="true" style="height: 3555px;">
<?php foreach ($terms as $term) : ?>
<?php $term_url = get_term_link($term->term_id, \'icon_package\'); ?>
<?php $term_by_id = get_term($term->term_id, \'icon_package\'); ?>
<div class="item-grid-item" style="width: 461px; transform: translate3d(0px, 0px, 0px);">
<div class="icon-pack-card" style="--color-bg: #e3e9e7">
<a class="icon-pack-card__link" href="<?php echo $term_url; ?>" aria-label="Link to item page">
<div class="icon-pack-card__header">
<div class="icon-pack-card__title">
<?php $term->name; ?>
</div>
<div class="icon-pack-card__count">
<?php echo $term_by_id->count; ?> Icons
</div>
</div>
<div class="icon-pack-card__inner" data-item-count="4">
<?php
$args_query = array(
\'post_type\' => array(\'icons\'),
// \'nopaging\' => true,
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'icon_package\',
\'field\' => \'term_id\',
\'terms\' => $term->term_id,
\'operator\' => \'IN\',
\'include_children\' => false,
),
),
);
$query = new WP_Query($args_query);
if ($query->have_posts()) :
$postcounter = 0;
while ($query->have_posts() && ($postcounter < 4)) :
$query->the_post();
?>
<?php $featured_img_url = get_the_post_thumbnail_url($post->ID, \'full\'); ?>
<img class="icon-pack-card__image" width="2048" height="2048" alt="Flat Pan" src="<?php echo $featured_img_url; ?>">
<?php $postcounter++; ?>
<?php endwhile; ?>
<?php else : ?>
<div>nothing</div>
<?php endif; ?>
</div>
</a>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php else : ?>
<div class="search-no-results">
<div class="search-no-results__img">
<img width="100" height="100" alt="asd" title="asdas" src="#">
</div>
<h3>No results found</h3>
<p> Uh oh! Looks like we don\'t have any <strong><?php echo $args[\'searched_phrase\']; ?></strong> icons yet. We\'re building our catalog, so check back soon.
</p>
</div>
<?php endif; ?>
<?php get_footer(); ?>