我最终接受了西蒙·斯卡夫的建议get_template_part
.
为了防止任何人需要它,此页面(正在进行的工作)创建一个高级搜索表单,然后构建一个WP\\U查询来搜索自定义帖子类型:
<?php if (isset($_POST["act"]) && $_POST["act"] == "s") : ?>
<?php
$meta_query = array(\'relation\' => \'AND\');
if (!empty($_POST["s_genus"])) {
$genus = array(
\'key\' => \'genus\',
\'value\' => $_POST["s_genus"],
\'compare\' => \'LIKE\'
);
$meta_query[] = $genus;
}
if (!empty($_POST["s_species"])) {
$species = array(
\'key\' => \'species\',
\'value\' => $_POST["s_species"],
\'compare\' => \'LIKE\'
);
$meta_query[] = $species;
}
$args = array(
\'post_type\' => \'species\',
\'meta_query\' => $meta_query
);
get_template_part(\'searchresults\');
?>
<?php else : ?>
<div id="content">
<h2>ADVANCED<span class="lblue">SEARCH</span></h2>
<form action="" method="post">
<input type="hidden" name="act" value="s" />
<fieldset id="taxonomy" style="margin-right: 15px;">
<legend>Taxonomy</legend>
<label for="genus">Genus</label>
<input class="full" type="text" name="s_genus" />
<br />
<label for="species">Species</label>
<input class="full" type="text" name="s_species" />
<br />
<label for="family">Family</label>
<input class="full" type="text" name="s_family" />
</fieldset>
</form>
</div>
<?php endif; ?>
然后,在
searchresults.php
我有一段代码,只需几行代码,然后对标准页面模板进行一次更改:
<?php global $args; ?>
<?php $query = new WP_Query ( $args ); ?>
<?php if ($query->have_posts()) : ?>
<div class="post" id="post-<?php the_ID(); ?>">
<p class="info">Search results for <em>‘<?php echo $s ?>’</em></p>
</div>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time(\'F jS, Y\') ?> — <?php the_time(\'h:i a\') ?> <?php edit_post_link(\'Edit\',\'<strong> |</strong> \',\'\'); ?> </small>
<div class="entry">
<?php the_content(\'Continue reading »\'); ?>
</div>
<p class="info"><?php comments_popup_link(\'Comment »\', \'1 comment »\', \'% comments »\'); ?> <strong>|</strong> <?php the_category(\', \') ?></p>
</div>
<?php endwhile; ?>
<p align="center"><?php next_posts_link(\'« Previous Entries\') ?> <?php previous_posts_link(\'Next Entries »\') ?></p><?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn\'t here.</p>
<?php endif; ?>