将wordpress搜索结果放在同一页面上

时间:2012-11-07 15:35:57

标签: php wordpress search

我正在尝试在我当前所在的页面中显示自定义帖子搜索的结果。我回复到同一页面,这是我的表单:

<form role="search" method="get" id="searchform-conferences" action="<?php the_permalink();?>">
    <input type="text" name="search"  value="Enter keywords ..." onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/><br />
    <input type="hidden" name="post_type" value="posttypeconference" />
    <input type="submit" id="searchsubmit" value="Search" />
</form>

然而,每当我提交搜索时,即使页面网址与搜索查询除外,它也会显示404,即mydomain.com/page_where_my_search_form_is.php?search=searchterm

1 个答案:

答案 0 :(得分:0)

您的HTML应该是那样的

<form role="search" method="get" id="searchform-conferences" action="">
    <input type="text" name="search"  value="Enter keywords ..." onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/><br />
    <input type="hidden" name="post_type" value="posttypeconference" />
    <input type="submit" id="searchsubmit" value="Search" />
</form>

你的PHP可能是那样的

<?php
$args = array( 'post_type' => $_POST['post_type'], 'posts_per_page' => 10, 'post_title' => 'LIKE %'.$_POST['search'].'%' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    the_title();
    echo '<div class="entry-content">';
    the_content();
    echo '</div>';
endwhile;
?>

如果您注册自定义帖子,请确保它类似于:

add_action('init', 'usb_init');
function usb_init()
{
    $labels = array(
    'name' => _x('USB', 'post type general name'),
    'singular_name' => _x('USB', 'post type singular name'),
    'add_new' => _x('Add New', 'usb'),
    'add_new_item' => __('Add New USB'),
    'edit_item' => __('Edit USB'),
    'new_item' => __('New USB'),
    'view_item' => __('View USB'),
    'search_items' => __('Search USBs'),
    'not_found' =>  __('No usb found'),
    'not_found_in_trash' => __('No usb found in Trash'),
    'parent_item_colon' => ''
);
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'product','with_front' => FALSE),
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => '5',
    'supports' => array('title','excerpt','editor','thumbnail','page-attributes'),
    'taxonomies' => false
);
register_post_type('usb',$args);
}

'public'=&gt;真正, 'public_queryable'=&gt;真正, 'query_var'=&gt;真,

如果仍然无效,您可以在注册自定义帖子后尝试添加:flush_rewrite_rules();