2017-08-08 27 views
0

我是新的php和wordpress。wordpress從帖子到頁面發送價值

我想分析xml並在後期顯示結果。

這是XML:

<?xml version="1.0" encoding="utf-8"?> 
<all_emp> 
<emp_detail> 
<emp emp_name="john"><img>john_1.jpg</img></emp> 
<emp emp_name="john"><img>john_2.jpg</img></emp> 
<emp emp_name="john"><img>john_3.jpg</img></emp> 
<emp emp_name="marry"><img>marry_1.jpg</img></emp> 
<emp emp_name="marry"><img>marry_2.jpg</img></emp> 
<emp emp_name="david"><img>david_1.jpg</img></emp> 
</emp_detail> 
</all_emp> 

我使用簡碼在functions.php中顯示它在後。

$url = 'https://.../emp_test.xml'; 
$xml = simplexml_load_file("$url") or die("Error: Cannot create object"); 
$names = array(); 
$emps = $xml->children()->emp_detail->emp ; 
foreach($emps as $emp_detail) 
{ 
    $source = (string)$emp_detail->attributes()->emp_name; 
    $names[]= $source; 
} 
$names = array_unique($names); 
return implode("<br />", $names); 

所以,在文章中,我已經導致這樣的:

john 
marry 
david 

之後,如何點擊約翰,它會發送字符串是約翰頁面?

+0

能否請您添加代碼,以便我們能得到的想法? – Jalpa

+1

你能否更清楚地描述它?這個結果應該顯示在哪裏?前端還是後端?從你插入這個數據的地方'john:male:26'? 我可以認爲,如果jhon是一個用戶,並且您從usermeta獲取數據,那麼只需使用ajax調用來完成該顯示部分即可。 –

+0

@Jalpa:我正在更新我的問題。 – 1234abcd

回答

0

讓我成爲第一個說這不是最理想的方式來做到這一點。但是,您可以爲處理這些名稱的頁面創建模板。然後創建一個使用此模板的wordpress頁面。比方說,您所創建的頁面:http://[SITEURL]/name-handling-page

然後,你可以修改上面的短代碼,而不是返回jsutthe名稱的網址:

$names = array_unique($names); 
$links = array(); 
foreach ($names as $name) 
{ 
     $links[] = get_permalink('name-handling-page')."?name=$name"; //replace name-handling-page with slug or id of newly created page 
} 
return implode("<br />", $links); 
+0

您的意思是我需要在頁面中顯示所有的emp名稱並在另一個頁面中顯示img?頁面 - >頁面,不是後 - >頁面? – 1234abcd