[英]Display search results on the same page in Wordpress
I am trying to create a Wordpress page which contains a Search form, but I can't display the search results on the same page.我正在尝试创建一个包含搜索表单的 Wordpress 页面,但我无法在同一页面上显示搜索结果。 I've seen that I should include a Wordpress loop in my php file, but I can't figure out how.我已经看到我应该在我的 php 文件中包含一个 Wordpress 循环,但我不知道如何。
Here is my search form:这是我的搜索表单:
<form id="searchform" action="../search4.php" method="post"><input id="Cref" style="height: 20px; width: 140px;" name="Cref" type="text" value="" />
<input id="submit" name="search" type="submit" value="Search" />
</form>
The search4.php file is this one: search4.php 文件是这样的:
<?php
$servername = "localhost";
$username='root';
$password = "";
$dbname = "mydb";
$mysqli = new mysqli($servername,$username, Null, $dbname);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
if(! get_magic_quotes_gpc() ) {
$Cref = addslashes ($_POST['Cref']);
}else {
$Cref = $_POST['Cref'];
}
session_start();
$results="SELECT * FROM mytable WHERE CRF LIKE CONCAT ('%', $Cref, '%')";
$resultSet = $mysqli->query($results);
$numRows = $resultSet->num_rows;
if ($numRows > 0) {
while ($row = $resultSet->fetch_object()) {
echo "{$row->CRF} {$row->Name} {$row->Description} <br>";
}}
else
{
echo "No Results";}
?>
Here is what I did:这是我所做的:
Custom form:自定义表格:
<form role="search" method="GET" id="searchform" action="<?php echo get_permalink(); ?>">
<input type="text" name="search" id="search" value="search">
<input type="submit" id="searchsubmit" value="Search" />
</form>
On the page I wanted the search results:在我想要搜索结果的页面上:
I did a WP_query
like on the codex and changed the $args
to:我在codex上做了一个WP_query
并将$args
更改为:
if( isset( $_REQUEST['search'] ) ){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged' => $paged,
'posts_per_page' => 16, //or any number
'post_type' => 'post', //or your custom post type if needed
's' => $_REQUEST[ 'search' ]
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.