我正在尝试在存档页面上创建一个搜索框,该页面显示自定义帖子类型。
我按照此指南实现了归档页面上的搜索框:
http://wpsnipp.com/index.php/template/create-multiple-search-templates-for-custom-post-types/
所以我创建了一个搜索。php文件,我在其中输入了以下代码:
<?
/* Template Name: Search Results */
$search_refer = $_GET["post_type"];
if ($search_refer == \'spec-needs-res\') { load_template(TEMPLATEPATH . \'/template_search_spec_needs.php\'); }
else { load_template(TEMPLATEPATH . \'/template_search_default.php\'); };
?>
我已经创建了所需的模板\\u search\\u spec\\u。php文件,我将以下代码(循环)放入其中:
<?php
$args = array( \'post_type\' => \'spec-needs-res\', \'posts_per_page\' => -1, \'s\' => $s, \'paged\' => $paged );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="span4 spec-needs-list-single">
<div class="spec-needs-title"><?php the_title(); ?></div>
<div><?php the_excerpt(); ?></div>
<div class="plus-btn-spec-needs-cont"><a href="<?php the_permalink(); ?>"><button class="plus-btn-spec-needs">+</button></a></div>
</div>
<?php
endwhile;
wp_reset_query();
?>
我在archive-spec-needs-res.php中添加了此代码
<form id="searchform" action="<?php bloginfo(\'home\'); ?>/" method="get">
<input id="s" maxlength="150" name="s" size="20" type="text" value="" class="txt" />
<input name="post_type" type="hidden" value="spec-needs-res" />
<input id="searchsubmit" class="btn" type="submit" value="Search" />
</form>
这个过程似乎很好,但每次我尝试搜索某个内容时,我都会得到一个空页面,模板会被选中,但我没有帖子,所以我永远不会得到结果。
我已经一步一步地跟着导游走了,有什么建议吗?
使现代化
我注意到,搜索框只在我搜索时给出结果,但没有参数,显然,我得到了所有的帖子,但一旦我输入了参数,我就再也得不到结果了,所以搜索框似乎起作用了,但当我使用关键字搜索自定义帖子时,我就不起作用了。
这是我用“webinar”这个词搜索时得到的URL:
http://www.matrix-test.com/edtech3/?s=webinar&post_type=spec-needs-res
URL是否正确?
我有一个自定义的帖子类型,标题是“Webinar”,另一个标签是“Webinar”,我也把Webinar放在文本中的某个地方。
下面是我用来创建自定义帖子类型的代码:
/****************************************
* Add custom taxonomy for Specific Needs *
****************************************/
add_action(\'init\', \'spec_needs_cat_register\');
function spec_needs_cat_register() {
$labels = array(
\'name\' => \'Specific Needs Categories\',
\'singular_name\' => \'Specific Needs Category\',
\'search_items\' => \'Search Specific Needs Categories\',
\'popular_items\' => \'Popular Specific Needs Categories\',
\'all_items\' => \'All Specific Needs Categories\',
\'parent_item\' => \'Parent Specific Needs Category\',
\'edit_item\' => \'Edit Specific Needs Category\',
\'update_item\' => \'Update Specific Needs Category\',
\'add_new_item\' => \'Add New Specific Needs Category\',
\'new_item_name\' => \'New Specific Needs Category\',
\'separate_items_with_commas\' => \'Separate Specific Needs categories with commas\',
\'add_or_remove_items\' => \'Add or remove Specific Needs categories\',
\'choose_from_most_used\' => \'Choose from most used Specific Needs categories\'
);
$args = array(
\'label\' => \'Specific Needs Categories\',
\'labels\' => $labels,
\'public\' => true,
\'hierarchical\' => true,
\'show_ui\' => true,
\'show_in_nav_menus\' => true,
\'args\' => array( \'orderby\' => \'term_order\' ),
\'rewrite\' => array( \'slug\' => \'spec-needs-tax\', \'with_front\' => true, \'hierarchical\' => true ),
\'query_var\' => true
);
register_taxonomy( \'spec_needs_cat_register\', \'spec-needs-res\', $args );
}
/*****************************************
* Add custom post type for Specific Needs *
*****************************************/
add_action(\'init\', \'spec_needs_res_register\');
function spec_needs_res_register() {
$labels = array(
\'name\' => \'Specific Needs\',
\'singular_name\' => \'Specific Needs\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Specific Needs\',
\'edit_item\' => \'Edit Specific Needs\',
\'new_item\' => \'New Specific Needs\',
\'view_item\' => \'View Specific Needs\',
\'search_items\' => \'Search Specific Needs\',
\'not_found\' => \'Nothing found\',
\'not_found_in_trash\' => \'Nothing found in Trash\',
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'has_archive\' => true,
\'rewrite\' => array( \'slug\' => \'spec-needs-res\', \'with_front\' => true ),
\'capability_type\' => \'post\',
\'menu_icon\' => \'dashicons-images-alt2\',
\'menu_position\' => 14,
\'supports\' => array(\'title\', \'excerpt\', \'editor\',\'thumbnail\') //here you can specify what type of inputs will be accessible in the admin area
);
register_post_type( \'spec-needs-res\' , $args );
}