我使用wordpress中的内置图库在自定义帖子类型中存储和排序图像。在我使用的主题中:
$attachments = get_posts(
array(\'post_type\' => \'attachment\',
\'post_parent\' => $post->ID,
\'orderby\' => \'menu_order\'
)
);
以上代码正确返回库图像,但顺序错误。当我查看包含库的帖子的内容字段时,它包含库数据:[gallery ids="2,5,7,8"]
因此,我猜测内置库不会将排序存储在单独的字段中,如menu_order
相反,它将附件ID的排序列表存储在父帖子的内容字段中。所以,我的问题是,要让画廊图片从主题中正确排序,最好的方法是什么?
我试过这样的方法:
$matches = array();
if(preg_match(\'/ids="(.*)"/\', $post->post_content, $matches)) {
$ids = $matches[1];
$query = "SELECT * FROM $wpdb->posts ".
"WHERE post_type = \'attachment\' &&
post_parent = $post->ID ".
"ORDER BY FIELD(ID, $ids)";
$attachments = $wpdb->get_results($query);
}
这似乎可行,但有没有更干净的方法来做到这一点。