0

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

<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,即使页面 url 是相同的,除了搜索查询,即mydomain.com/page_where_my_search_form_is.php?search=searchterm

4

1 回答 1

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' => true,'publicly_queryable' => true,'query_var' => true,

如果这仍然不起作用,您可以尝试添加:flush_rewrite_rules();在您注册自定义帖子后

于 2012-11-07T15:45:26.240 回答