在插件中使用Direct$wpdb获取特色图片

时间:2014-01-20 作者:joshuaiz

我正在使用上次查看帖子插件的定制版本来抓取我的唱片标签网站上最近查看的发布。

我使用的代码捕获附件图像,但它并不总是页面特征图像-结果是间歇性的。代码如下:

function zg_recently_viewed() { // Output
echo \'<ul style="list-style:none; padding-left:0px;font-size:11px;" class="viewed_posts">\';
if (isset($_COOKIE["WP-LastViewedPosts"])) {
    //echo "Cookie was set.<br/>";  // For bugfixing - uncomment to see if cookie was set
    //echo $_COOKIE["WP-LastViewedPosts"]; // For bugfixing (cookie content)
    $zg_post_IDs = unserialize(preg_replace(\'!s:(\\d+):"(.*?)";!e\', "\'s:\'.strlen(\'$2\').\':\\"$2\\";\'", stripslashes($_COOKIE["WP-LastViewedPosts"]))); // Read serialized array from cooke and unserialize it
    //echo $_COOKIE["WP-LastViewedPosts"];
    foreach ($zg_post_IDs as $value) { // Do output as long there are posts
        global $wpdb;
        $zg_get_title = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE ID = \'$value+0\' AND ID IN ( SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = \'5\' ) LIMIT 1");
        $thumb = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent = \'$value\' and post_type = \'attachment\'");
        /* $thumb = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->postmeta} where post_parent = \'$value\' AND meta_key = \'_thumbnail_id\'" ); */
        $thumbnail = wp_get_attachment_image_src($thumb);
        foreach($zg_get_title as $zg_title_out) {
            if ($thumbnail) {
            echo "<li style=\'max-width:96px;float:left;margin-right:20px;min-height:200px;\'><a class=\'relatedimg\' style=\'text-decoration:none;\' href=\\"". get_permalink($value+0) . "\\" title=\\"". $zg_title_out->post_title . "\\"><img src=\'" .$thumbnail[0]. "\' alt=\'Release Image\' />" . $zg_title_out->post_title . "</a></li>\\n";
            } else {
            echo "<li style=\'max-width:96px;float:left;margin-right:20px;min-height:200px;\'><a class=\'relatedimg\' style=\'text-decoration:none;\' href=\\"". get_permalink($value+0) . "\\" title=\\"". $zg_title_out->post_title . "\\"><img src=\'http://vizualrecords.com/wp-content/uploads/2011/04/vizual_feature-150x150.png\' alt=\'Release Image\' />" . $zg_title_out->post_title . "</a></li>\\n";
            }

        }
    }
} else {
    //echo "No cookie found.";  // For bugfixing - uncomment to see if cookie     was not set
}
echo \'</ul>\';
}

此行:$thumb=$wpdb->get\\u var(“从$wpdb中选择ID->posts,其中post\\u父项=\'value\',post\\u类型=\'attachment\'”);

。。。抓取图像,通常会抓取页面的特色图像,但并不总是如此。下面的注释行不起作用,但我认为这更接近我想要的。

cookie是在上面的函数中设置的。请注意,我确实需要通过直接mysql调用($wpdb)而不是使用普通的WP函数来获取特色图像。

有什么想法吗?

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

这不是查询特色图像的方式。这是:

$thumb = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent = \'$value\' and post_type = \'attachment\'");
应为:

$thumb = $wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta where meta_key = \'_thumbnail_id\' and post_id = \'$value\'");
这是假设$value 是一个帖子ID。

“精选”图像是附件,但并非所有附件都是精选图像。这就是为什么这有时起作用,但其他时候却不起作用。

然而,您刚刚重新发明了方向盘。你所做的就是get_post_thumbnail_id 是的,尽管功能uses proper Core mechanisms.

结束

相关推荐

$wpdb->GET_RESULTS未返回数组

我以前没有真正弄乱过wpdb美元,但有一部分我被卡住了。我正在尝试获取关联数组中返回的值。这两种我都试过了$datarow = $wpdb->get_results(\"SELECT class_name FROM wp_wcs2_class WHERE id=$classnumber\"); $datarow = $wpdb->get_results(\"SELECT class_name FROM wp_wcs2_class WHERE id=$classnumber\",ARRA