这是我的HTML:
<input type="text" class="keyword" name="keyword" id="keyword" placeholder="Search video...">
<button id="search-keyword">Search</button>
当我单击“搜索”按钮时,启动如下脚本:
if($(\'.current\').hasClass(\'search-select\')){
action = \'my_create_posts_search\'; //php function to do
ab = document.getElementsByClassName(\'keyword\')[0].value; //get the value of input
}
然后,Ajax函数启动:
$.ajax({
type: "POST",
url: ajax_params.ajax_url, //wordpress ajax
data: {
\'action\': action, //php function to do
\'ab\': ab, //value of input
},
然后操作开始,执行的代码如下:
function my_create_posts_search($name) {
//Pagination for posts
$response = \'\';
$paged = (int) (!is_null($_POST[\'clicked_times\'])) ? $_POST[\'clicked_times\'] : 0;
$postsPerPage = 18;
$postOffset = $paged * $postsPerPage;
$args = array(
\'posts_per_page\' => $postsPerPage, //pagination
\'offset\' => $postOffset, //pagination
\'category\' => 42, //post category
\'s\' => $name, //value of input
\'orderby\' => \'date\',
\'order\' => \'DESC\',
);
... other things...
}
当我想通过Post标签搜索时,一切都很完美。
如何实现此功能?
我希望我的代码能帮助你。谢谢