您可以创建新页面。假设您想要:http://example.com/mysearch/
创建一个具有该URL结构的页面。接下来,搜索表单->转到并执行搜索表单操作:
<form role="filter-search" method="get" id="sd_searchform_filter" action="<?php
echo home_url( \'/mysearch/\' ); ?>">...
现在转到
functions.php
(或者你想在哪里使用这个函数)现在我们在玩把戏!
function isu_search_url( $query ) {
$page_id = 12; // This is ID of page with your structure -> http://example.com/mysearch/
$per_page = 10;
$post_type = \'activity\'; // I just modify a bit this querry
// Now we must edit only query on this one page
if ( !is_admin() && $query->is_main_query() && $query->queried_object->ID == $page_id ) {
// I like to have additional class if it is special Query like for activity as you can see
add_filter( \'body_class\', function( $classes ) {
$classes[] = \'filter-search\';
return $classes;
} );
$query->set( \'pagename\', \'\' ); // we reset this one to empty!
$query->set( \'posts_per_page\', $per_page ); // set post per page or dont ... :)
$query->set( \'post_type\', $post_type ); // we set post type if we need (I need in this case)
// 3 important steps (make sure to do it, and you not on archive page,
// or just fails if it is archive, use e.g. Query monitor plugin )
$query->is_search = true; // We making WP think it is Search page
$query->is_page = false; // disable unnecessary WP condition
$query->is_singular = false; // disable unnecessary WP condition
}
}
add_action( \'pre_get_posts\', \'isu_search_url\' );
现在成功了,你不需要改变
.htaccess
, 等。分页将正常工作。:)