【发布时间】:2013-11-26 00:02:27
【问题描述】:
我正在制作一个过滤器插件,我想使用 search.php 作为结果,但是如果我在我的插件中将文本输入名称设置为“s”,那么它使用常规搜索功能,而不是我的 WP_Query 方法在我的插件中设置。
如果我更改名称并让插件使用该名称,我会得到结果,但与插件本身出现的位置相同。
那么,我可以让 search.php 页面使用我插件的结果吗?
表格和我的查询:
<?php
$blog_url = get_bloginfo('url');
$form = <<<EOH
<div id="sbc">
<form method="get" action="{$blog_url}" id="ss-search">
<input type="text" value="{$search_text}" name="ss" onblur="if (this.value == '') { this.value = '{$search_text}';}" onfocus=if (this.value == '{$search_text}') { this.value = '';}" />
{$list}
<input type="submit" id="sbc-submit" value="Search" />
</form>
</div>
EOH;
if (isset($_GET['ss'])) {
$args = array(
// 'category__not_in' => 1,
's' => $_GET['ss']
);
$q = new WP_Query($args);
if ( $q->have_posts() ) {
echo '<ul>';
while ( $q->have_posts() ) {
$q->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
echo get_pagination_links();
} else {
echo 'no posts found';
}
/* Restore original Post Data */
wp_reset_postdata();
}
echo $form;
?>
搜索.php
<p class="search_results">Your search for <span class="searchterm"><?php the_search_query(); ?></span> has returned <?php echo $totalresults; ?> results.</p>
<?php
global $paged;
if(empty($paged)) $paged = 1;
$loop_counter = 1;
$results_per_page = get_query_var('posts_per_page');
echo '<ul class="search-list">';
if ( have_posts() ) : while ( have_posts() ) : the_post();
if( $paged == 1 ) {
$real_count = $loop_counter;
} else {
$real_count = $loop_counter + ( $paged * $results_per_page - $results_per_page);
}
echo '<li><span class="listnum">' . $real_count . '.</span>';
// The Post
?>
<a href="<?php the_permalink(); ?>"><?php echo substr(strip_tags($post->post_content), 0, 46);?>...</a>
<?php
echo '</li>';
// genesis_after_post();
$loop_counter++;
endwhile;
// genesis_after_endwhile();
else :
// genesis_loop_else();
echo '<p>No results found. Search for "page" for best results.';
endif;
echo '</ul>';
?>
此外,分页不适用于自定义 WP_Query ($q)。我确实得到了分页链接,但它们不起作用。例如,如果我单击第二页,则没有任何反应。如果我将$q 更改为$wp_query 它可以工作,但是我无法将我想要的参数传递给它。
【问题讨论】:
标签: wordpress