如何在WordPress数据库中获取纯PHP缩略图?

时间:2015-09-24 作者:Marcos Vinicius

我需要帮助获得纯PHP WordPress数据库中的缩略图,因为需要将其插入Magento应用程序(Feed)。有人能帮我吗?

我需要这个:

http://luckifas.nextmp.net/wp/wp-content/uploads/2015/09/fashionista_web_header_by_lstar92-d3gkahi-263x130.jpg

或部分,例如:

上传/2015/09/fashionista\\u web\\u header\\u作者:lstar92-d3gkahi-263x130。jpg/2015/09/fashionista\\u web\\u header\\u作者:lstar92-d3gkahi-263x130。jpg更多详情:

add_image_size(\'magento-image\', 263, 130, true);
使用的PHP脚本:

function SimpleLoopWordpress (){

    try {

        // PDO em ação!
        $pdo = new PDO ( "mysql:host=localhost;dbname=", "", "", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "SELECT * FROM wp_posts WHERE post_status = \'publish\' AND post_type = \'post\' ORDER BY post_date ASC";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();

        $i = 1;
        echo \'<div class="row">\';

        while ( $linha = $stmt->fetch ( PDO::FETCH_OBJ ) ) {

            ?>
                <div class="col-md-6">
                    <?php 
                         echo $i++;
                         echo FEATURED IMAGE <--------
                         echo $linha->post_title;
                         echo $linha->post_date;
                         echo $linha->post_content;
                    ?>  
                </div>

            <?php

        }

        echo \'</div>\';

    }
我在pastebin中发布了完全更新的php脚本:

http://pastebin.com/49Mbj484

<到目前为止,这一点还可以--->$meta->execute( $linha->thumbnail_id ); // ids 15 - 16

Thanks

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

好的,有几件事-首先,我建议修改您的主查询以同时获取缩略图ID(我还将其更改为仅获取有拇指的帖子):

$sql = "SELECT wp_posts.*, meta_value AS thumbnail_id FROM wp_posts " .
       "LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id " .
       "WHERE post_type = \'post\' AND meta_key = \'_thumbnail_id\' AND meta_value != \'\' " .
       "ORDER BY post_date ASC";
现在进行处理:

while ( $linha = $stmt->fetch ( PDO::FETCH_OBJ ) ) {

    $meta = $pdo->prepare( \'SELECT meta_value FROM wp_postmeta WHERE meta_key = "_wp_attachment_metadata" AND post_id = ?\' );
    $meta->execute( array( $linha->thumbnail_id ) );

    $file = null; // Failsafe in case anything below fails
    if ( $data = $meta->fetchColumn() ) {
        if ( $data =@ unserialize( $data ) ) {
            $file = $data[\'file\'];

            if ( ! empty( $data[\'sizes\'] ) ) {
                $sizes = $data[\'sizes\'];

                if ( ! empty( $sizes[\'magento-image\'] ) )
                    $file = $sizes[\'magento-image\'][\'file\'];
                elseif ( ! empty( $sizes[\'thumbnail\'] ) ) // You might want to fallback, up to you
                    $file = $sizes[\'thumbnail\'][\'file\'];
            }

            if ( $file !== $data[\'file\'] ) {
                // Files stored in sizes are only the basename
                // If you use year/month uploads, we need to grab that path

                if ( \'.\' !== $path = dirname( $data[\'file\'] ) )
                    $file = "$path/$file";
            }
        }
    }

    if ( $file ) {
        // Gotcha!
        echo "<img src=\'http://example.com/wp-content/uploads/$file\' />";
    }

}
这段代码没有经过测试,所以请查看如何返回并报告任何错误。

相关推荐

无法在WordPress中通过PHP显示图像

我有一个奇怪的问题,当我尝试使用PHP输入图像标记的源代码时,它会在检查器中显示以下错误<img src=(unknown) alt=\"\"> 这个代码片段为我提供了正确的url,通过查看CPanel并粘贴和复制地址进行检查,但当我尝试通过php输入它时,不会显示图像$image = wp_get_attachment_image_src( $post_thumbnail_id); //echo($image[0]); ?> <img src=