自定义帖子类型:漂亮搜索URL和HAS_ARCHIVE

时间:2012-03-11 作者:turbonerd

这是一个基于答案评论的问题here.

我正在使用以下代码为我的网站创建两个不同的搜索框:

<form action="<?php echo home_url( \'/glossary/\' ); ?>" method="get">
    <p style="font-size:12px;">SEARCH<strong class="dkblue">GLOSSARY</strong></p>
    <input type="text" name="s" value="" />
    <input type="submit" value="GO" class="glossary_submit" />
</form>

<form id="profilesearch" action="<?php echo home_url( \'/species/\' ); ?>" method="get">
    <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>
我的glossary CPT有以下创建$args:

$args = array(
  \'labels\' => $labels,
  \'public\' => true,
  \'publicly_queryable\' => true,
  \'exclude_from_search\' => true,    
  \'show_ui\' => true,
  \'show_in_menu\' => true,
  \'query_var\' => true,
  \'capability_type\' => \'post\',
  \'has_archive\' => false,
  \'hierarchical\' => true,
  \'menu_position\' => 40,
  \'menu_icon\' => $this->plugin_url . \'images/glossary.png\',
  \'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'page-attributes\')
);
还有我的species CPT有:

$args = array(
  \'labels\' => $labels,
  \'public\' => true,
  \'publicly_queryable\' => true,
  \'show_ui\' => true,
  \'show_in_menu\' => true,
  \'query_var\' => true,
  \'has_archive\' => true,
  \'capability_type\' => \'post\',
  \'has_archive\' => true,
  \'hierarchical\' => false,
  \'menu_position\' => 5,
  \'menu_icon\' => $this->plugin_url . \'images/fish20.png\',
  \'supports\' => array(\'author\',\'thumbnail\',\'excerpt\',\'comments\',\'revisions\')
);
目前,当我从词汇表表单提交搜索时,URL更改为:/glossary/?s=term. 当我从物种表单提交搜索时,URL更改为:/search/term.

词汇表URL的作用是搜索glossary 仅限CPT类型。然而,物种URL只是搜索所有内容。

If I change has_archive to false, the searches work as per the above glossary. If I change it to true, the searches work as per the above species.

我想要两者都做的是:将URL更改为/search/cpt-slug/term, i、 e。/search/species/bettas 并仅搜索具有该CPT的帖子。

我有没有做错什么$args?

3 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

最不幸的是,虽然我最初的解决方案很优雅,但我没有预见到这种复杂性,因此给您带来的不便,我深表歉意。

因此,我提供了另一种解决方案。

Firstly to get your fancy search query:

您的标记需要更改,但这将截获URL并在内部正确重写它们,它与其他答案类似,但更漂亮、更快、更易于阅读,而且只需一个函数

function custom_rewrite( $wp_rewrite ) {
    $feed_rules = array(
        \'search/(.+)/(.+)\'      =>  \'index.php?post_type=\'.$wp_rewrite->preg_index(1).\'&s=\' . $wp_rewrite->preg_index(2)
    );
    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( \'generate_rewrite_rules\', \'custom_rewrite\' );
因此example.com/search/post/bannana 将在所有“post”类型的帖子中搜索bannana。

如果您不希望“搜索”,而是希望显示一个术语,那么只需修改重写规则中的查询变量即可。

您可以使用monkeyman rewrite rules Analyzer插件来帮助您,方法是输入URL并显示哪个重写规则处理它以及所述规则的优先级/顺序

BUT

然而,您将无法使用优雅的html标记来使用这些URL,它将无法很好地发挥作用,您将需要修改表单提交到的位置,这意味着虽然这听起来可能是一个很棒的想法,但在实践中,实现意味着您将需要至少1个重定向,以便搜索框使用这些URL,这一点都不理想。

For your species search problem

还要修改物种登记,使其更加具体/准确:

$args = array(
  \'labels\' => $labels,
  \'public\' => true,
  \'publicly_queryable\' => true,
  \'exclude_from_search\' => false, // lets not guess what this will be, lets be authorative
  \'show_ui\' => true,
  \'show_in_menu\' => true,
  \'query_var\' => true,
  \'has_archive\' => true,
  \'capability_type\' => \'post\',
  \'has_archive\' => true,
  \'hierarchical\' => false,
  \'menu_position\' => 5,
  \'menu_icon\' => $this->plugin_url . \'images/fish20.png\',
  \'supports\' => array(\'author\',\'thumbnail\',\'excerpt\',\'comments\',\'revisions\')
);
但是,虽然这可能会解决问题,但我怀疑不会。重读你的问题,我看到它重写为/搜索/术语,但WordPress默认情况下不会这样做。我怀疑您在其他地方有代码可以将搜索词重写为更漂亮的版本,用于一般的帖子搜索,但这是一种干扰,无论是因为它在代码中添加的顺序还是其他原因。

由于我看不到您的整个代码库,而且我也不确定这个搜索代码修饰是什么样子的,所以我只能为您指出它的方向(如果没有设置post\\u类型,则会有条件地重写,而不是总是这样)。

edit: give this a try:

function custom_rewrite( $wp_rewrite ) {
    $feed_rules = array(
        \'species/(.+?)(/[0-9]+)?/?$\'      =>  \'index.php?post_type=species&species=\'.$wp_rewrite->preg_index(1).\'&page=\'.$wp_rewrite->preg_index(2)
    );
    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( \'generate_rewrite_rules\', \'custom_rewrite\' );

SO网友:fregante

你必须在permalinks上工作。我现在没有时间,但你可以在这里找到一些信息:http://shibashake.com/wordpress-theme/wordpress-permalink-add

这里有一些代码可以帮助您入门,它基于另一个article that was taken down, 我不能保证它会起作用。

add_filter(\'wp_loaded\',\'bfr_add_permastruct\');
add_filter(\'rewrite_rules_array\',\'bfr_add_permalink_rule\');

function bfr_add_permastruct(){
    add_permastruct(\'ctp_search\', \'search/%post-type%/%postname%\');

  //Remember to flush_rules() when for the first time 
  //$wp_rewrite->flush_rules();
}

// Adding a new rule
function bfr_add_permalink_rule($rules)
{
    $newrules = array();
    $newrules[\'search/([^/]+)/([^/]+)?$\'] = \'index.php?post_type=$matches[1]name=$matches[2]\';
    return $newrules + $rules;
}

SO网友:turbonerd

多亏了汤姆,我解决了我的问题。。。!

我在我的functions.php. 一定是一年前的事了,我都不记得放进去了!

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();
    }

}
这导致我/物种/搜索/失败。将其替换为这样做了:

add_action( \'template_redirect\', \'my_rewrite\' );
function my_rewrite() {

    if ( is_search() and false === strpos( $_SERVER[\'REQUEST_URI\'], \'/search/\' ) ) {
        if ( strpos( $_SERVER[\'REQUEST_URI\'], \'/species/\' ) )
            wp_redirect( get_bloginfo( \'home\' ) . \'/species/search/\' . str_replace( \' \', \'+\', str_replace( \'%20\', \'+\', get_query_var( \'s\' ) ) ) );
        else if ( strpos( $_SERVER[\'REQUEST_URI\'], \'/glossary\' ) )
            wp_redirect( get_bloginfo( \'home\' ) . \'/glossary/search/\' . str_replace( \' \', \'+\', str_replace( \'%20\', \'+\', get_query_var( \'s\' ) ) ) );
        else
            wp_redirect( get_bloginfo( \'home\' ) . \'/search/\' . str_replace( \' \', \'+\', str_replace( \'%20\', \'+\', get_query_var( \'s\' ) ) ) );

        exit();
    }

}
接下来,我必须改变has_archivetrue 在我的glossary CPT。

最后,我使用了Tom的重写规则和this excellent post 要使我的漂亮URL正常工作:

/* --- rewrite rules for searches...*/
function custom_rewrite( $wp_rewrite ) {
    $species = array(
        \'(species|glossary)/search/(.+?)(/page/([0-9]+))?/?$\'      =>  \'index.php?post_type=\'.$wp_rewrite->preg_index(1).\'&s=\'.$wp_rewrite->preg_index(2).\'&paged=\'.$wp_rewrite->preg_index(4)
    );  

    $wp_rewrite->rules = $species + $wp_rewrite->rules;
}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( \'generate_rewrite_rules\', \'custom_rewrite\' );

I now have working pretty URLs on my custom post type searches, AND pagination: /species/search/term, /species/search/term/page/2/ and /glossary/search/term, etc. all work correctly.

谢谢汤姆,你值得称赞!

结束

相关推荐

Search URL redirect problem

我的搜索结果页面有一个奇怪的问题,当我搜索时,我在URL中得到了这个http://f.cl.ly/items/2Z1c1b2G1n0B3r3W063B/Screen%20Shot%202011-09-29%20at%201.21.19%20PM.png “查看X&Y”?我不知道他们从哪里来&;我似乎不知道如何修复它&;它破坏了我的分页,那么我如何修复它呢?谢谢