【问题标题】:Have wordpress search results on the same page在同一页面上有 wordpress 搜索结果
【发布时间】:2023-03-16 13:02:01
【问题描述】:

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

<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

【问题讨论】:

  • 我认为如果你在表单中省略 action 属性,提交将转到相同的 url。我不知道你为什么会收到 404 错误,除非页面上有错误,否则 url 应该可以在没有查询参数的情况下工作。
  • 在没有查询属性的情况下,url 确实可以正常工作,但是当提交搜索并且页面重新加载查询时,这就是我得到 404 的时候。
  • 如果存在搜索参数,您是否正在进行任何类型的处理?有时在使用框架时,PHP 错误会通过显示“未找到”或“内部服务器错误”页面来掩盖。
  • 啊也许是这样,它是wordpress,所以我认为wp可能正在做一些事情
  • ok 似乎是一个永久链接的想法,一旦我将永久链接恢复为默认值,我的搜索页面就会工作

标签: php wordpress search


【解决方案1】:

你的 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' => 真, 'publicly_queryable' => 真, 'query_var' => 真,

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

【讨论】:

  • 试过了,它直接进入404...我不知道wordpress在后台做什么,但它不喜欢我的页面在它的末尾有一个查询,它使它成为 404
  • 如果您可以检查错误日志并显示任何可能有帮助的错误
  • 这是 PHP 错误日志,可以是名称为 error_log 或类似名称的文件
  • 不,这不是自定义帖子的注册方式,它在正常的搜索结果页面中显示良好。
  • flush_rewrite_rules() 是做什么的,我应该把它放在哪里?添加后要删除吗?
猜你喜欢
  • 2016-06-25
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-06
相关资源
最近更新 更多