How to get thumbnail Wordpress with pure PHP?

Asked

Viewed 310 times

1

I would like to take the URL of thumbnails in the Wordpress database using pure PHP to put in a Magento project in order to show the posts on the home page.

Like that relationship Magento -> Wordpress it’s a little complicated, I’d like someone to help me catch the Featured Image of each post to insert on the Ecommerce homepage.

What I’ve already 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:

attachment no banco de dados

  • I recommend that in addition to image, post the real data, it is not always easy to test data that are in photos. I kicked an answer, there may be flaws, but the way is this.

  • It has either XMLRPC or the semi-official JSON API plugin, but it’s not pure PHP.. If the title was to "take Thumb externally (out of wp)", it could answer, otherwise it would be an answer outside the scope of what was asked.

1 answer

1


This metavalue seems to be a variable "serialized" with http://php.net/manual/en/function.serialize.php

Then just use the 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 the unserialize will return, because you posted an image of meta_value and I can’t reproduce the value, but I believe this is it, var_dump should help you detect the correct keys of the meta_value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.