我的网站上有三个单独的搜索框:species
CPT搜索,glossary
CPT搜索和我想搜索的一般搜索post
和species
职位类型。
我的问题有三个,
创建漂亮的搜索URL最有效的代码是什么?WordPress默认情况下不激活这些URL的原因是什么searchform-species, searchform-glossary
, search
和searchresults
指定我的物种搜索只返回物种结果,词汇表将返回词汇表结果等。如何合并分页
我当前尝试的代码如下:
CODE FOR #1
add_action( \'template_redirect\', \'my_rewrite\' );
function my_rewrite() {
if ( is_search() and false === strpos( $_SERVER[\'REQUEST_URI\'], \'/search/\' ) ) {
wp_redirect( get_bloginfo( \'home\' ) . \'/search/\' . str_replace( \' \', \'+\', str_replace( \'%20\', \'+\', get_query_var( \'s\' ) ) ) );
exit();
}
}
CODE FOR #2 & #3
搜索表单。php<?php
$search_query = get_search_query();
if (!$search_query) {
$search_query = "SEARCH";
}
?>
<form id="topbar_search" action="<?php echo home_url( \'/\' ); ?>" method="post">
<p>SEARCH</p>
<input type="text" size="50" value="<?php echo $search_query; ?>" name="s" id="s" class="topbar_longinput default-value" />
<input type="submit" value="GO" class="topbar_submit" />
<p class="tinylinks"><a href="#1">advanced search</a></p>
</form>
搜索物种。php<h1 class="profilesearch">PROFILE<span class="white">SEARCH</span></h1>
<form id="profilesearch" action="<?php echo home_url( \'/\' ); ?>" method="post">
<input type="hidden" name="type" value="profile" />
<input type="text" size="50" class="default-value" value="SEARCH" name="s" />
<input type="submit" value="GO" class="profilesearch_submit" />
<!-- <p class="tinysearch"><a href="/dev/advanced-search/">ADVANCED SEARCH</a></p> -->
<input type="checkbox" name="showthumbnails" id="showthumbnails" class="checkbox" <?php if ($_POST["showthumbnails"] == "on") { echo \'checked="checked" \'; } ?>/><label for="showthumbnails">HIDE THUMBNAILS</label>
</form>
搜索。php if (isset($_REQUEST["type"])) {
switch ($_REQUEST["type"]) {
case "profile" :
$post_type = "species";
break;
case "glossary" :
$post_type = "glossary";
break;
default :
$post_type = array( \'post\', \'species\' );
break;
}
} else {
$post_type = array( \'post\', \'species\' );
}
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array(
\'s\' => $search_term,
\'post_type\' => $post_type,
\'paged\' => $paged
);
?>
<?php get_template_part(\'searchresults\'); ?>
<小时>What\'s currently happening is this:
单个搜索有效,但分页无效。当我单击next/previous_posts_link
按钮,它添加/page/2/
指向URL,但不执行任何操作。如果我试着回应get_query_var(\'page\')
它返回NULL
. 如果我试着回应\'paged\'
相反,它会返回0
.此外,当用户单击此链接时$search_query
来自searchform的变量。php显示为page/bettas
(其中bettas
是原始查询)。
<小时>
Ideally what I\'d like to happen is this:
<在“常规搜索”中,用户输入betta。URL更改为/search/betta
其中列出了post
或species
包含单词betta的内容/search/betta/species (或任何最适合SEO的)并显示species
包含单词betta的内容与#2相同的用户,然后单击Next 按钮(previous_posts_link
). URL更改为/search/betta/species/page/2
用户可以看到接下来列出的25个物种我是WP重写和分页方面的新手,因此非常感谢您的帮助。