Php Wordpress使用MySQL获取图像

Php Wordpress使用MySQL获取图像,php,mysql,wordpress,function,Php,Mysql,Wordpress,Function,我有一个旧函数,它使用post摘录来保存图像缩略图。这是一个有点黑客和工作了很长时间 现在我需要用这篇文章的节选,你知道的,节选。所以我想更新这个函数,直接从帖子附件中获取图像src信息 问题是: 我如何更新下面的$before\u sqlsql代码以获取post附件中的第一个附加图像 守则: (我想我只关心sql部分,因为其余部分应该自己清理一下吗?)还有更多的代码部分,但不是将所有的代码都粘贴到这里,这个片段应该足够了 $before_sql = "SELECT ID, post_title

我有一个旧函数,它使用post摘录来保存图像缩略图。这是一个有点黑客和工作了很长时间

现在我需要用这篇文章的节选,你知道的,节选。所以我想更新这个函数,直接从帖子附件中获取图像src信息

问题是:

我如何更新下面的
$before\u sql
sql代码以获取post附件中的第一个附加图像

守则:

(我想我只关心sql部分,因为其余部分应该自己清理一下吗?)还有更多的代码部分,但不是将所有的代码都粘贴到这里,这个片段应该足够了

$before_sql = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' and post_date < '$cur_post_date' ORDER BY post_date DESC LIMIT $thumbnum";
$before_results = $wpdb->get_results($before_sql);
if($before_results) {
    foreach ($before_results as $before_result) {
        $post_title = stripslashes($before_result->post_title);
        $permalink = get_permalink($before_result->ID);
        $post_excerpt = ($before_result->post_excerpt);
        $output="<div class=\"thumbnails\"><a href=\"" . $permalink . "\" title=\"Permanent Link: " . $post_title . "\">" . $post_excerpt . "</a><br />&lsaquo;</div>\n         " . $output;
    }
}
$before\u sql=“选择ID,post\u title,post\u摘录自$wpdb->post,其中post\u status='publish',post\u type='post'和post\u date<'$cur post\u date'ORDER BY post\u date DESC LIMIT$thumbnum”;
$before\u results=$wpdb->get\u results($before\u sql);
如果($在结果之前){
foreach($before\u result作为$before\u result){
$post\u title=stripslashes($before\u result->post\u title);
$permalink=get\u permalink($before\u result->ID);
$post\u extract=($before\u result->post\u extract);
$output=“
&lsaqo;\n”。$output; } }
要在一次查询中获得文章的第一个附件,您可以用这种方法

SELECT *,
(SELECT guid FROM `wp_posts`  WHERE post_type ='attachment' AND post_parent=wp.`ID` ORDER BY post_date ASC LIMIT 1 ) AS attachment
 FROM `wp_posts` wp 
按发布日期订购ASC将获得第一张图像如果您想要最新上传的图像,只需使用DESC按发布日期订购DESC

这是你的问题

$before_sql = "SELECT ID, post_title, post_excerpt,
(SELECT guid FROM $wpdb->posts  WHERE post_type ='attachment' AND post_parent=wp.`ID`
ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM $wpdb->posts wp WHERE wp.post_status = 'publish' AND wp.post_type = 'post'
and wp.post_date < '$cur_post_date' ORDER BY wp.post_date DESC LIMIT $thumbnum";
$before\u sql=“选择ID、post\u标题、post\u摘录、,
(从$wpdb->posts中选择guid,其中post_type='attachment'和post_parent=wp.`ID`
按邮寄日期订购ASC限额1)作为附件
从$wpdb->posts wp,其中wp.post_status='publish'和wp.post_type='post'
和wp.post_日期<'$cur_post_日期'wp.post_日期描述限额$thumbnum的订单”;
这对我来说很好

这是一个查询,它将只获取那些带有附件的帖子

$before_sql = "SELECT ID, post_title, post_excerpt,
(SELECT guid FROM $wpdb->posts  WHERE post_type ='attachment' AND post_parent=wp.`ID`
ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM $wpdb->posts wp WHERE wp.post_status = 'publish' AND wp.post_type = 'post'
and wp.post_date < '$cur_post_date' HAVING attachment IS NOT NULL ORDER BY wp.post_date DESC LIMIT $thumbnum";
$before\u sql=“选择ID、post\u标题、post\u摘录、,
(从$wpdb->posts中选择guid,其中post_type='attachment'和post_parent=wp.`ID`
按邮寄日期订购ASC限额1)作为附件
从$wpdb->posts wp,其中wp.post_status='publish'和wp.post_type='post'
和wp.post_date<“$cur_post_date”具有附件的顺序不为空,由wp.post_date DESC LIMIT$thumbnum”;

无法让它工作,并且
var\u dump($before\u sql)
显示
NULL
。我认为问题可能是
post\u parent=wp'ID'
。如何将其细化为帖子ID?我已定义了
global$id
post_parent=wp.'$id'
也会失败。为什么要使用var_dump(sql)??您需要执行
$before\u results=$wpdb->get\u results($before\u sql)
post\u parent=wp.
ID``是传递子查询的相关别名运行phpmyadmin中的第一个查询您可以看到它正在工作是的,现在我看到它正在工作。除了使用ID``或
'ID'
在foreach中的附件上给了我一个空值。如果文章没有附件,则附件为空。如果文章有附件,则它将显示图像或附件的url尝试在任何文章上上载图像,然后查看是的,这是在一个有500个图像的photoblog网站上。我可以看到帖子标题和ID,但附件为空。我甚至不能让它工作:
$beforeIMG=wp\u get\u attachment\u url($before\u result->ID)但是
echo$before\u result->ID工作正常。有上传的图像,它应该显示。