Wordpress 검색 결과를 표시하는 방법
커스텀 메이드 템플릿에서 검색이 동작하지 않는 이유를 알아내는 데 많은 시간을 소비했습니다.지금까지 헤더에 searchform.php 파일을 포함시키는 방법을 생각해 냈고, 현재 비어 있는 search.php 파일을 작성했습니다(그래서 무언가를 검색할 때 빈 페이지로 리다이렉트되며, 그것을 작동시키기 위해 반드시 필요한 것이 있다고 생각합니다).Wordpress를 읽고 있었지만 s를 찾을 수 없었습니다.olution, 내가 찾은 유용한 정보는 이것뿐이었다.
http://codex.wordpress.org/Creating_a_Search_Page
검색 결과를 표시하기 위해 무엇을 해야 하는지 제안해 주시겠습니까? 특별한 쿼리, 기능 등이 있습니까?어디에서도 못 찾겠어요.
필요할 때를 대비해서 내 검색폼.my searchform.model 파일을 준비하세요.
<form action="<?php echo home_url(); ?>" id="search-form" method="get">
<input type="text" name="s" id="s" value="type your search" onblur="if(this.value=='')this.value='type your search'"
onfocus="if(this.value=='type your search')this.value=''" />
<input type="hidden" value="submit" />
</form>
Wordpress 루프를 검색에 포함해야 합니다.php 이것은 예시입니다.
search.displate 템플릿파일:
<?php get_header(); ?>
<?php
$s=get_search_query();
$args = array(
's' =>$s
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
_e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>");
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php
}
}else{
?>
<h2 style='font-weight:bold;color:#000'>Nothing Found</h2>
<div class="alert alert-info">
<p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>
</div>
<?php } ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
기본적으로 검색 결과를 루프하여 템플릿의 일부로 표시하려면 search.php 템플릿에 Wordpress 루프를 포함해야 합니다.
다음은 ThemeShaper의 The WordPress 테마 검색 템플릿 및 페이지 템플릿의 매우 기본적인 예입니다.
<?php
/**
* The template for displaying Search Results pages.
*
* @package Shape
* @since Shape 1.0
*/
get_header(); ?>
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header><!-- .page-header -->
<?php shape_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php shape_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'search' ); ?>
<?php endif; ?>
</div><!-- #content .site-content -->
</section><!-- #primary .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
사용하고 있다searchform.php그리고.search.php여기 실제 코드를 입력합니다.
검색 페이지 생성 codex페이지가 도움이 됩니다.#Creating_a_Search_Page_Template에 검색 쿼리를 나타냅니다.
저 같은 경우에는 합격입니다.$search_queryWP_Query에 대한 인수 Class(이것은, 가 검색 쿼리인지 아닌지를 판별할 수 있습니다).그런 다음 The Loop을 실행하여 원하는 게시물 정보를 표시합니다.이 경우는 the_permalink와the_title.
검색 상자 양식:
<form class="search" method="get" action="<?php echo home_url(); ?>" role="search">
<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' ) ?>" />
<button type="submit" role="button" class="btn btn-default right"/><span class="glyphicon glyphicon-search white"></span></button>
</form>
search.php템플릿 파일:
<?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$the_query = new WP_Query($search_query);
if ( $the_query->have_posts() ) :
?>
<!-- the loop -->
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
템플릿이 다음 위치에 있는지 확인합니다.theme폴더에 포함됨search.php그리고.searchform.php그렇지 않으면.
WordPress는 검색 결과에 태그, 카테고리 및 분류법을 포함합니다.
이 코드는 http://atiblog.com/custom-search-results/ 에서 가져온 것입니다.
여기서의 일부 기능은 29ineteen 주제에서 따왔다.이 테마로 만들어졌기 때문입니다.
이 코드 예는 태그, 범주 또는 사용자 지정 분류법을 검색에 포함시키는 데 도움이 됩니다.그리고 이러한 태그 또는 카테고리가 포함된 게시물을 표시합니다.
그러기 위해서는 테마의 search.php를 변경해야 합니다.
<?php
$search=get_search_query();
$all_categories = get_terms( array('taxonomy' => 'category','hide_empty' => true) );
$all_tags = get_terms( array('taxonomy' => 'post_tag','hide_empty' => true) );
//if you have any custom taxonomy
$all_custom_taxonomy = get_terms( array('taxonomy' => 'your-taxonomy-slug','hide_empty' => true) );
$mcat=array();
$mtag=array();
$mcustom_taxonomy=array();
foreach($all_categories as $all){
$par=$all->name;
if (strpos($par, $search) !== false) {
array_push($mcat,$all->term_id);
}
}
foreach($all_tags as $all){
$par=$all->name;
if (strpos($par, $search) !== false) {
array_push($mtag,$all->term_id);
}
}
foreach($all_custom_taxonomy as $all){
$par=$all->name;
if (strpos($par, $search) !== false) {
array_push($mcustom_taxonomy,$all->term_id);
}
}
$matched_posts=array();
$args1= array( 'post_status' => 'publish','posts_per_page' => -1,'tax_query' =>array('relation' => 'OR',array('taxonomy' => 'category','field' => 'term_id','terms' =>$mcat),array('taxonomy' => 'post_tag','field' => 'term_id','terms' =>$mtag),array('taxonomy' => 'custom_taxonomy','field' => 'term_id','terms' =>$mcustom_taxonomy)));
$the_query = new WP_Query( $args1 );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
array_push($matched_posts,get_the_id());
//echo '<li>' . get_the_id() . '</li>';
}
wp_reset_postdata();
} else {
}
?>
<?php
// now we will do the normal wordpress search
$query2 = new WP_Query( array( 's' => $search,'posts_per_page' => -1 ) );
if ( $query2->have_posts() ) {
while ( $query2->have_posts() ) {
$query2->the_post();
array_push($matched_posts,get_the_id());
}
wp_reset_postdata();
} else {
}
$matched_posts= array_unique($matched_posts);
$matched_posts=array_values(array_filter($matched_posts));
//print_r($matched_posts);
?>
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query3 = new WP_Query( array( 'post_type'=>'any','post__in' => $matched_posts ,'paged' => $paged) );
if ( $query3->have_posts() ) {
while ( $query3->have_posts() ) {
$query3->the_post();
get_template_part( 'template-parts/content/content', 'excerpt' );
}
twentynineteen_the_posts_navigation();
wp_reset_postdata();
} else {
}
?>
언급URL : https://stackoverflow.com/questions/14802498/how-to-display-wordpress-search-results
'source' 카테고리의 다른 글
| 워드프레스 카테고리.php 페이지 번호 404 오류 (0) | 2023.02.07 |
|---|---|
| 게시물을 클릭하면 WordPress의 데이터베이스에서 쿼리된 값이 전송되도록 하는 방법 (0) | 2023.02.07 |
| 문자열 출력에서 캐리지 리턴을 제거하려면 어떻게 해야 합니까? (0) | 2023.02.07 |
| 멀티프로세싱풀: map_async와 imap의 차이점은 무엇입니까? (0) | 2023.02.04 |
| npm은 패키지를 어디에 설치합니까? (0) | 2023.02.04 |