我正在使用 Wordpress,我必须弄清楚如何从同一列中获取多个值并将它们转换为变量。
最后一步是我只希望数据显示在一个表格中,如下所示:
Bob Company1 <br>
Alex Company2
相反,我得到Bob Alex Company1 Company 2
要么
Bob <br>
Alex<br>
Company1<br>
Company2
这是我正在使用的两个不同版本:
$sql = "SELECT meta_value as guest from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guest_name' UNION SELECT meta_value as company from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guests_company'";
$result = mysql_query($sql);
$NumberOfResults=mysql_num_rows($result);
if (mysql_num_rows($result) == 0) {
echo "No Guests yet... stay tuned!";
exit;
}
while(list($guest,$company)= mysql_fetch_row($result))
{
echo "<table><tr><td>".$guest."</td><td>".$company."</td></tr></table>";
}
或者
$sql = mysql_query("SELECT meta_value as guest from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guest_name' LIMIT 2 UNION SELECT meta_value as company from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guests_company'");
$i = 1;
while ($get = mysql_fetch_array($sql))
{
echo '<table><tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td></tr></table>';
$i++;
}
任何帮助将不胜感激!!!谢谢!
编辑:如果将来有人想使用 Wordpress 中的高级自定义字段创建自己的小部件,这里或多或少是最终产品:
$sql = "SELECT p.ID AS post_id, g.meta_value as guest, c.meta_value as company, d.meta_value as date
FROM wp_posts p
JOIN wp_postmeta g ON g.post_id = p.id AND g.meta_key = 'guest_name'
JOIN wp_postmeta c ON c.post_id = p.id AND c.meta_key = 'guests_company'
JOIN wp_postmeta d ON d.post_id = p.id AND d.meta_key = 'show_date'
WHERE p.post_status = 'publish'
ORDER by d.meta_value DESC";
$query = mysql_query($sql);
echo '<table><tr><th>Guest</th><th>Company</th><th>Show Date</th></tr>';
while ($get = mysql_fetch_array($query)) {$newDate = date("m-d-Y", strtotime($get["date"]));
echo '<tr><td><a href="http://yoururl.com/?p='.$get["post_id"].'">'.$get["guest"].'</a></td><td>'.$get["company"].'</td><td>'.$newDate.'</td></tr>';
}
echo '</table>';
非常感谢 doublesharp 在这方面的帮助。