사용자 지정 게시물 유형에 대한 사용자 지정 검색을 만드는 방법은 무엇입니까?
5 대답
- 투표
-
- 2013-03-08
다음은 제가 시도한 결과이며 3 단계에 대한 해결책입니다. 맞춤 게시물 유형이 " 제품 "
이라고 가정 해 보겠습니다.1. 기능 코드 추가 에서 archive-search.php
를 지정할 수 있습니다.함수template_chooser ($template) { 글로벌 $ wp_query; $post_type=get_query_var ( 'post_type'); if ($ wp_query- >is_search & amp; & amp; $post_type=='제품') { returnfind_template ( 'archive-search.php');//archive-search.php로 리디렉션 } return $template; } add_filter ( 'template_include','template_chooser');
2. 맞춤 게시물 유형 (archive-search.php)에 대한 검색 결과 템플릿 만들기
<사전> <코드> & lt;?php /* 템플릿 이름 : 맞춤 검색 */ get_header ();? > & lt; div class="contentarea"> & lt; divid="content"class="content_right"> & lt; h3 > 검색 결과 : & lt;?phpecho "$ s";? > & lt;/h3 > & lt;?phpif (have_posts ()) : while (have_posts ()) :the_post ();? > & lt; divid="post- & lt;?phpthe_ID ();? >" class="게시물"> & lt; 기사 > & lt; h4 > & lt; a href="& lt;?phpthe_permalink ();? >"title="& lt;?phpthe_title ();? >"> & lt;?phpthe_title ();? > & lt;/a >/h4 > & lt;p > & lt;?phpthe_exerpt ();? > & lt;/p > & lt;p align="right"> & lt; a href="& lt;?phpthe_permalink ();? >"> 자세히 알아보기 & lt;/a > & lt;/p > & lt; span class="post-meta"> 작성자 : & lt;?phpthe_author ();? > | 날짜 : & lt;?phpecho date ( 'j F Y');? > & lt;/span > & lt;/article > & lt;!-#post-> & lt;/div > & lt;?phpendwhile;? > & lt;?phpendif;? > & lt;/div > & lt;!-콘텐츠-> & lt;/div > & lt;!-contentarea-> & lt;?phpget_sidebar ();? > & lt;?phpget_footer ();? >-
검색 양식 작성
<사전> <코드> & lt; div > & lt; h3 > 제품 검색 & lt;/h3 > & lt;form role="search"action="& lt;?phpecho site_url ( '/');? >"method="get"id="searchform"> & lt;inputtype="text"name="s"placeholder="제품 검색"/> & lt;inputtype="hidden"name="post_type"value="products"/> & lt;!-//숨겨진 '제품'값-> & lt;inputtype="submit"alt="검색"value="검색"/> & lt;/양식 > & lt;/div >
이 검색 양식에서 "제품"값은 숨겨져 있으며 제품 게시물 만 검색합니다.
자세한 내용은 여기로 연결하겠습니다.
http://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/Here is what I've tried and got a solution with 3 steps. Let's say your custom post type is "products"
1 . Add Function Code here you can specify the archive-search.php
function template_chooser($template) { global $wp_query; $post_type = get_query_var('post_type'); if( $wp_query->is_search && $post_type == 'products' ) { return locate_template('archive-search.php'); // redirect to archive-search.php } return $template; } add_filter('template_include', 'template_chooser');
2 . Create search result template for custom post type ( archive-search.php )
<?php /* Template Name: Custom Search */ get_header(); ?> <div class="contentarea"> <div id="content" class="content_right"> <h3>Search Result for : <?php echo "$s"; ?> </h3> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div id="post-<?php the_ID(); ?>" class="posts"> <article> <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4> <p><?php the_exerpt(); ?></p> <p align="right"><a href="<?php the_permalink(); ?>">Read More</a></p> <span class="post-meta"> Post By <?php the_author(); ?> | Date : <?php echo date('j F Y'); ?></span> </article><!-- #post --> </div> <?php endwhile; ?> <?php endif; ?> </div><!-- content --> </div><!-- contentarea --> <?php get_sidebar(); ?> <?php get_footer(); ?>
Build Search Form
In this Search Form, the value "products" is hidden and it will search only product posts.<div> <h3>Search Products</h3> <form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform"> <input type="text" name="s" placeholder="Search Products"/> <input type="hidden" name="post_type" value="products" /> <!-- // hidden 'products' value --> <input type="submit" alt="Search" value="Search" /> </form> </div>
for more, I would like to link you to here
http://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/-
팁 : 게시물 유형을 등록 할 때 **publicly_queryable ** 인수를 **true **로 설정해야합니다.그렇지 않은 경우get_query_var ( 'post_type')은 url 인수에 지정된post_type 값을 반환하지 않습니다.https://codex.wordpress.org/Function_Reference/register_post_type#ArgumentsTip: when registering the post type, the **publicly_queryable** argument must be set to **true**. If not, the get_query_var('post_type') will never return the post_type value given in the url argument. https://codex.wordpress.org/Function_Reference/register_post_type#Arguments
- 0
- 2015-05-21
- Gustavo
-
또 다른 팁/제안 된 편집 :`get_query_var ( 'post_type')`은 배열 (문자열이 아닌)을 반환하므로 직접 비교할 수 없습니다.한 번에 하나의 게시물 유형 만 검색하므로`$post_type` var를`$post_type [0]`으로 변경했습니다.Another tip/suggested edit: `get_query_var('post_type')` returned an array (rather than a string) so couldn't be compared directly. Since I'm only searching one post type at a time, I simply changed my `$post_type` var to `$post_type[0]`.
- 0
- 2016-04-08
- indextwo
-
`http ://localhost : 3000/? s=cloud % 27 &post_type=product`에서`http ://localhost : 3000/search/cloud/product`로 URL을 다시 쓰는 방법이 있습니까?is there a way to rewrite the url from `http://localhost:3000/?s=cloud%27&post_type=product` to `http://localhost:3000/search/cloud/product`
- 0
- 2017-11-06
- YarGnawh
-
@YarGnawh 늦은 응답에 대해 죄송합니다. https://wordpress.stackexchange.com/questions/15418/pretty-permalinks-for-search-results-with-extra-query-var를 확인하십시오.rewrite라는 플러그인도 있습니다 https://wordpress.org/plugins/rewrite/@YarGnawh Sorry for late response, check this out https://wordpress.stackexchange.com/questions/15418/pretty-permalinks-for-search-results-with-extra-query-var. There is a plugin called rewrite too https://wordpress.org/plugins/rewrite/
- 0
- 2017-11-13
- Ronald
-
```search_template``` 필터가```template_include''`보다 더 적절한 대안 인 것 같습니다.the ```search_template``` filter seems to be a more appropriate alternative to ```template_include```
- 0
- 2018-07-19
- Alexey Kosov
-
- 2016-01-12
다음은 나에게 적합한 방법입니다. 깨끗하지는 않지만 다른 답을 얻을 수 없었습니다.
맞춤 게시물 유형 검색 양식 :
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>"> <label> <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span> <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" /> <input type="hidden" name="post_type" value="book" /> </label> <input type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" /> </form>
functions.php :
function searchfilter($query) { if ($query->is_search && !is_admin() ) { if(isset($_GET['post_type'])) { $type = $_GET['post_type']; if($type == 'book') { $query->set('post_type',array('book')); } } } return $query; } add_filter('pre_get_posts','searchfilter');
search.php :
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php if(isset($_GET['post_type'])) { $type = $_GET['post_type']; if($type == 'book') {?> /* Format for "book" custom post type */ <?php } else { ?> /* Format for custom post types that are not "book," or you can use elseif to specify a second post type the same way as above. Copy the default format here if you only have one custom post type. */ <?php } ?> <?php } else { ?> /* Format to display when the post_type parameter is not set (i.e. default format) */ <?php } ?> <?php endwhile; else: ?> /* What to display if there are no results. */ <?php endif; ?>
당연히 세 곳 모두에서 "책"을 사용자 지정 게시물 유형으로 대체해야합니다.
이 정보가 도움이되기를 바랍니다.
Here is what works for me. Not as clean but I couldn't get any of these other answers to work.
Search form for Custom Post Type:
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>"> <label> <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span> <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" /> <input type="hidden" name="post_type" value="book" /> </label> <input type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" /> </form>
In functions.php:
function searchfilter($query) { if ($query->is_search && !is_admin() ) { if(isset($_GET['post_type'])) { $type = $_GET['post_type']; if($type == 'book') { $query->set('post_type',array('book')); } } } return $query; } add_filter('pre_get_posts','searchfilter');
In search.php:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php if(isset($_GET['post_type'])) { $type = $_GET['post_type']; if($type == 'book') {?> /* Format for "book" custom post type */ <?php } else { ?> /* Format for custom post types that are not "book," or you can use elseif to specify a second post type the same way as above. Copy the default format here if you only have one custom post type. */ <?php } ?> <?php } else { ?> /* Format to display when the post_type parameter is not set (i.e. default format) */ <?php } ?> <?php endwhile; else: ?> /* What to display if there are no results. */ <?php endif; ?>
Naturally in all three places you'll need to replace "book" with your custom post type.
Hope this helps someone!
-
- 2015-11-16
짧은 코드가 더욱 현실화 됨
function template_chooser($template) { global $wp_query; $post_type = $wp_query->query_vars["pagename"]; if( isset($_GET['s']) && $post_type == 'products' ) { return locate_template('archive-search.php'); // redirect to archive-search.php } return $template; } add_filter('template_include', 'template_chooser');
A short code more actualized
function template_chooser($template) { global $wp_query; $post_type = $wp_query->query_vars["pagename"]; if( isset($_GET['s']) && $post_type == 'products' ) { return locate_template('archive-search.php'); // redirect to archive-search.php } return $template; } add_filter('template_include', 'template_chooser');
-
- 2017-03-02
일반 검색과 맞춤 게시물 유형 검색에 대해 두 가지 다른 형식을 사용하려고했습니다.
내 맞춤 게시물 유형이 일반 페이지와 다른 헤더를 사용합니다. 일반 페이지에서 내 검색 양식에 대한 호출은 다음과 같습니다.
<?php get_search_form(true); ?>
맞춤 게시물 유형 헤더의 검색 양식 호출은 다음과 같습니다.
<?php get_template_part('search','library'); ?>
추가 필드가있는 경우 :
<input type="hidden" name="post_type" value="library" /> //Where "library" is my custom post type.
함수 파일에 귀하가 제공 한 다음 코드가 있습니다.
/** Custom Search for Library */ function search_library($template) { global $wp_query; $post_type = get_query_var('post_type'); if( $wp_query->is_search && $post_type == 'library' ) { return locate_template('search-library.php'); // redirect to archive-search.php } return $template; } add_filter('template_include', 'search_library');
검색 양식이 사용자 정의 필드 내에서 검색을 수행하는지 감지하여 사용자 정의 템플릿으로 검색을 표시하고 그렇지 않으면 일반 템플릿을 사용합니다.
편집 : 무슨 일이 있어도 참을 반환했을get_search_form () 함수 호출을 수정했습니다.
I was looking to use two different forms for my normal searches and my searches on a custom post type.
My custom post type uses a different header than normal pages, on my normal page, the call to my search form is:
<?php get_search_form(true); ?>
And the call to my search form in the custom post type header is:
<?php get_template_part('search','library'); ?>
Which has an additional field:
<input type="hidden" name="post_type" value="library" /> //Where "library" is my custom post type.
In the functions file I have the following code that you have provided.
/** Custom Search for Library */ function search_library($template) { global $wp_query; $post_type = get_query_var('post_type'); if( $wp_query->is_search && $post_type == 'library' ) { return locate_template('search-library.php'); // redirect to archive-search.php } return $template; } add_filter('template_include', 'search_library');
Which detects if the search form is doing a search within custom fields, thus showing the search in a custom template, otherwise use the normal template.
Edit: fixed the get_search_form() function call which would have returned true no matter what.
-
주목할 가치가 있지만`get_search_form ( 'true')`는`get_search_form (true)`이어야합니다.`get_search_form`은 부울 입력을 찾고 있으므로`true` 또는`false`입니다.따옴표로 묶으면 부울 매개 변수가 아닌 문자열이 제공됩니다.함수가 설정되는 방식은` 'true'`와`'false'`가 모두 비어 있지 않은 문자열이기 때문에 동일한 결과를 반환합니다 (두 경우 모두true를 반환하는 함수).Worth noting, but `get_search_form('true')` should be `get_search_form(true)`. `get_search_form` is looking for a boolean input, so either `true` or `false`. By wrapping it in quotes you are feeding it a string, not a boolean parameter. The way that function is set up, both `'true'` and `'false'` would return the same result, because they are both non-empty strings (which causes the function to return true in both cases).
- 1
- 2018-02-22
- Mike
-
- 2014-09-16
빈 입력 검색 문제를 해결하려면 다음과 같이 기능 코드를 대체 할 수 있습니다.
function template_chooser($template) { global $wp_query; $post_type = get_query_var('post_type'); if( isset($_GET['s']) && $post_type == 'products' ) { return locate_template('archive-search.php'); // redirect to archive-search.php } return $template; } add_filter('template_include', 'template_chooser');
To fix the empty input search issue you can substitute the function code with this:
function template_chooser($template) { global $wp_query; $post_type = get_query_var('post_type'); if( isset($_GET['s']) && $post_type == 'products' ) { return locate_template('archive-search.php'); // redirect to archive-search.php } return $template; } add_filter('template_include', 'template_chooser');
-
코드 작동 방식을 설명하고 코드 소스를 공개하면 좋을 것입니다.Would be great if you explain how your code works, an reveal your source of the code
- 3
- 2014-09-16
- Pieter Goosen
블로그 게시물에 대한 검색 필드가 있지만 맞춤 게시물 유형을위한 다른 필드가 필요합니다. 다른 검색 결과 레이아웃 을 사용하여이 맞춤 검색 양식 을 만들려면 어떻게해야하나요?