显示搜索结果的文件是search.php 主主题文件夹中的文件。
在下面,您可以找到一个搜索默认帖子和页面的工作示例:
\'post_type\' => array(\'post\', \'page\')
例如,如果自定义帖子类型具有\'
portfolio\' 名称,然后将其添加到数组将是:
\'post_type\' => array(\'post\', \'page\', \'portfolio\')
整体
search.php 示例:
<?php
/* The template for displaying Search Results pages.*/
get_header();
?>
<!-- main -->
<div id="main">
<!-- intro -->
<section class="intro">
<!-- search -->
<form action="<?php echo home_url(); ?>" method="get" class="search">
<fieldset>
<input type="text" name="s" id="s" value="<?php _e(\'Click or type here to search\',\'text_domain\'); ?>" class="text" >
<input type="submit" value="go" class="submit" >
</fieldset>
</form>
<p><?php printf( __( \'Search Results for: %s\', \'text_domain\' ), \'<strong>\' . get_search_query() . \'</strong>\' ); ?></p>
</section>
<div class="main-holder">
<!-- search results list -->
<section class="col" id="content">
<?php
$s = $_GET[\'s\'];
$args=array(
\'post_type\' => array(\'post\', \'page\'),
\'post_status\' => \'publish\',
\'s\' => $s,
\'orderby\' => \'ID\',
\'order\' => \'desc\',
\'paged\' => $paged
);
$temp = $wp_query; // assign original query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post();
$thumb_url = get_the_post_thumbnail($post->ID, \'\', array(\'alt\' => the_title_attribute(\'echo=0\')));
?>
<!-- article -->
<article class="article article-alt">
<div class="heading">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
</div>
<nav class="add-info">
<ul>
<li><?php echo get_the_time(\'F d, Y\'); ?></li>
<li class="data"><?php the_category(\', \') ?></li>
<li class="data"><?php echo $cat_slug; ?></li>
<li><?php comments_popup_link(__(\'0 Comments\', \'text_domain\'),__(\'1 Comment\', \'text_domain\'), __(\'% Comments\', \'text_domain\')); ?></li>
</ul>
</nav>
<?php if ($thumb_url) { ?><figure class="visual"><?php echo $thumb_url; ?></figure><?php } ?>
<?php the_excerpt(); ?>
<a class="more" href="<?php the_permalink(); ?>"><?php _e(\'Read more ...\',\'text_domain\'); ?></a>
</article>
<?php
endwhile;
else:
_e(\'<h2>Nothing Found</h2>\',\'text_domain\');
echo \'<p>\'.__( \'Sorry, but nothing matched your search criteria. Please try again with some different keywords.\', \'text_domain\' ).\'</p>\';
endif;
?>
<!-- paging -->
<nav class="paging">
<ul>
<?php
if(function_exists(\'wp_pagenavi\')) { wp_pagenavi(); }
?>
</ul>
</nav>
<?php
$wp_query = null;
$wp_query = $temp;
?>
</section>
<!-- sidebar -->
<aside class="col" id="sidebar">
<?php dynamic_sidebar("Blog Sidebar") ?>
</aside>
</div>
</div>
<!--/ main -->