2017-07-29 68 views
0

我做了一個WordPress的預先jQuery的搜索。 當用戶點擊搜索按鈕,通過使用jquery我得到我所有的搜索參數,併發送$ .post()函數admin-ajax.php ... 我在functions.php文件中創建一個函數並接收所有搜索值並寫wp_query來獲取wp文章。回調函數爲ajax形式在wordpress

$args = array(
'category_name' => '3d');$query = new WP_Query($args); 

if ($query->have_posts()) { 
while ($query->have_posts()) { 
    $query->the_post(); 
    echo '<a href="'. the_permalink() .'">test</a>'; 
} 
wp_reset_postdata(); 

}

時,我打印的標籤上面的代碼

,輸出是這樣的:

http://localhost/site/%d8%af%d8%a7%d9%86%d9%84%d9%88%d8%af-%d9%81%db%8c%d9%84%d9%85-zootopia/test0 

問題1:測試是一個鏈接,但沒有任何HREF!鏈接外鏈接的href就像一個字符串一樣打印出來! 問題2:在所有輸出值自動打印後輸出值爲「0」的字符在PHP中甚至空回顯將打印「0」字符! 請幫助我,謝謝...

回答

0

您需要用此替換代碼。因爲您在使用the_permalink()而不是get_the_permalink()echo

https://developer.wordpress.org/reference/functions/get_the_permalink/ https://developer.wordpress.org/reference/functions/the_permalink/

$args = array('category_name' => '3d'); 
$query = new WP_Query($args); 

if ($query->have_posts()) { 
    while ($query->have_posts()) { 
     $query->the_post(); 
     echo '<a href="'. get_the_permalink() .'">test</a>'; 
    } 
    wp_reset_postdata(); 
} 
+0

非常感謝......但什麼是 「0」 字?這出現在從服務器到jquery的所有響應中 –

+0

您需要添加die()方法的功能,如下所示:function yourFunction(){ //在此處寫入代碼... die(); }; –

+0

幫我用正確的查詢替換$ post-> ID()?像這樣:get_post_meta($ post-> ID,'movie250',true) –