我正在使用Tribulant Gallery Voting 在我的网站上举办宠物比赛。
该插件在库中的每个图像下方添加投票按钮。
我想在侧边栏中显示投票最多的“前五名”宠物照片。
我的朋友编写了一些代码,从Tribrent gallery投票数据库表中提取投票、图像id、标题和描述,并按降序排序(多数投票先排序)
<?php
global $wpdb;
$result = $wpdb->get_results( "SELECT attachment_id, COUNT(attachment_id) AS qty FROM wp_5vt0txsx66_galleryvotes GROUP BY attachment_id ORDER BY qty DESC" );
echo "<table>";
echo "<tr><td>Qty</td><td>image_id</td><td>Project Title</td><td>Project Description</td></tr>";
foreach ($result as $row)
{
echo "<tr>";
echo "<td>".$row->qty."</td><td>".$row->attachment_id."</td>";
$attachment_meta = wp_get_attachment($row->attachment_id);
echo "<td>".$attachment_meta[\'title\']."</td>";
echo "<td>".$attachment_meta[\'caption\']."</td>";
echo "</tr>";
}
echo "</table>";
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
\'alt\' => get_post_meta( $attachment->ID, \'_wp_attachment_image_alt\', true ),
\'caption\' => $attachment->post_excerpt,
\'description\' => $attachment->post_content,
\'href\' => get_permalink( $attachment->ID ),
\'src\' => $attachment->guid,
\'title\' => $attachment->post_title
);
}
此处数量表示投票
上述代码在侧栏中显示以下输出:
我想显示图像本身而不是Image\\u id。请任何人指导我如何从数据库中获取图像并显示它们。
最合适的回答,由SO网友:montrealist 整理而成
您可以使用get_attachment_image():
$att_markup = wp_get_attachment_image( $attachment->ID, \'thumbnail\' );
或
get_attachment_link():
$att_link = wp_get_attachment_link( $attachment->ID, \'thumbnail\' );