在存档自定义帖子类型上创建搜索框

时间:2018-03-28 作者:Crashy

我正在尝试在存档页面上创建一个搜索框,该页面显示自定义帖子类型。

我按照此指南实现了归档页面上的搜索框:

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

1 个回复
最合适的回答,由SO网友:HU ist Sebastian 整理而成

在您的template\\u search\\u spec\\u需求中。php,删除自定义查询。url中的参数(s=&;post\\u type=spec needs res)应该可以很好地与“normal”循环配合使用。

因此,请尝试更改您的template\\u search\\u spec\\u需求。php到类似以下内容:

<?php

    while ( have_posts() ) : 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;
你应该没事;)

结束

相关推荐

making a search.php query

我正在尝试创建一个自定义搜索结果页面。我希望一次有6篇帖子,然后我要添加一个加载更多的按钮,可以加载更多的6篇帖子,以此类推。我希望页面处于引导网格布局中。每行2个立柱(col-md-6)。使用下面的代码,网页甚至不会显示出来。我是否需要向我的函数添加任何内容。php来实现这一点?有人能帮我吗?提前谢谢。<?php get_header(); $the_query = new WP_Query( [ \'posts_per_page\' =>

在存档自定义帖子类型上创建搜索框 - 小码农CODE - 行之有效找到问题解决它

在存档自定义帖子类型上创建搜索框

时间:2018-03-28 作者:Crashy

我正在尝试在存档页面上创建一个搜索框,该页面显示自定义帖子类型。

我按照此指南实现了归档页面上的搜索框:

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

1 个回复
最合适的回答,由SO网友:HU ist Sebastian 整理而成

在您的template\\u search\\u spec\\u需求中。php,删除自定义查询。url中的参数(s=&;post\\u type=spec needs res)应该可以很好地与“normal”循环配合使用。

因此,请尝试更改您的template\\u search\\u spec\\u需求。php到类似以下内容:

<?php

    while ( have_posts() ) : 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;
你应该没事;)

相关推荐

Media searching ignored

我们的网站使用WordPress,有很多媒体文件。我们网站的媒体名称格式如下[Car brand\'s name]-[number].jpg, 例如Tesla-1.jpg 或Aston Martin-3.jpg. 因此,我们可以通过搜索文章的名称轻松找到文章的特定媒体。但突然间,我们找不到媒体。我们正在尝试搜索名称为的媒体,但搜索结果不变。(不搜索任何内容时的媒体屏幕)(搜索Aston Martin时的媒体屏幕)当然,在填充搜索文本框后,它会显示一个加载图标,但结果总是一样的。为什么会发生这种情况?更新+