How to get the WordPress thumbnail with pure PHP?

1

I would like to get the URL of the thumbnails in the WordPress database using pure PHP to put in a Magento project in order to show the posts on the homepage.

How this relationship Magento - > Wordpress is a bit complicated, I would like someone to help me get the Featured Image of each post to enter on the ecommerce home page.

What I've done:

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>';

    }

I'll probably get this image here:

    
asked by anonymous 24.09.2015 / 13:58

1 answer

1

This metavalue seems to be a "serialized" variable with link

So just use unserialize

It would look something like:

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

        ?>
            <div class="col-md-6">
                <?php
                     //Verifica se o campo é vazio ou nulo e então converte
                     $meta_value = empty($linha->meta_value) ? NULL : unserialize($linha->meta_value);

                     //Resultado da conversão
                     var_dump($meta_value);

                     //Pega o nome da imagem
                     $imagem_url = $meta_value ? $meta_value['file'] : NULL;

                     echo $i++;
                     echo $imagem_url ? $imagem_url : 'Sem foto';
                     echo $linha->post_title;
                     echo $linha->post_date;
                     echo $linha->post_content;
                ?>  
            </div>

        <?php

    }

I'm not sure what unserialize will return, since you posted an image of meta_value and I can not reproduce the value, but I believe it is, var_dump should help you to detect the correct meta_value keys.

    
27.09.2015 / 01:24