📜 ⬆️ ⬇️

Output WordPress posts outside the site



Task:


Recently, I faced an interesting task for myself: Write a script that would display a certain number of posts from my site in the form of thumbnails. It would seem that what could be simpler, however, the essence of the task is precisely to make this script universal and accessible to use on any site from any engine.

You can see an example of implementation on my game.tobefun.org website by opening any game.
')
As you can see, I applied this algorithm to output a certain number (it is set by the parameters of your window) random thumbnails, which are links to records for which they are specified.


Step 1 - html Document


The general structure of the html-document is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script src="http://code.jquery.com/jquery-2.0.0.js"></script> <!--     --> <script src="http://game.tobefun.org/play/more.js"></script> </head> <body> <div class="more"> <span class="more_game" lang="ru"></span> </div> </body> </html> 

Step 2 - Javascript and Ajax Request Formation


First we need to determine the size of the working area of ​​the browser window. For this we use the code:

  var width = $(window).width(); var height = $(window).height(); 


Then we transfer the received data by the POST method.
  var width = $(window).width(); var height = $(window).height(); $.post( "more.php", // php-,    { width: logo_width, height: height }, onAjaxSuccess // ,       ); function onAjaxSuccess(data) { //    ,           id="more_game". $("#more_game").html(data); //         $('#more_game').css('height', height); } 


Step 3 - PHP - Handler


 <?php /*    $_POST['height'] -   64 -    */ $img_kil = floor($_POST['height']/64); /*        ... */ if ($img_kil >= 1) { // <-   IF /*     ,      */ $db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD); //    /*        ,      $img_kil         */ $k=$img_kil*3; 


Step 3.1 - SQL query for image links

It will consist of three parts:

1. Request for ID selection of published records
 SELECT `ID` FROM `wp_posts` WHERE `post_type` = 'post' AND `post_status` = 'publish' 


2. Request a sample of ID's photos that are set as a miniature
 SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = '_thumbnail_id' AND `post_id` IN () 


3. Request to select the necessary data of selected photos.
 SELECT `post_id`, `meta_value` FROM `wp_postmeta` WHERE `post_id` IN () ND `meta_key` = '_wp_attachment_metadata' 


The general query that will produce $ k random entries looks like this:
 $sql=' SELECT `post_id`, `meta_value` FROM `wp_postmeta` WHERE `post_id` IN ( SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = \'_thumbnail_id\' AND `post_id` IN ( SELECT `ID` FROM `wp_posts` WHERE `post_type` = \'post\' AND `post_status` = \'publish\' ) ) AND `meta_key` = \'_wp_attachment_metadata\' ORDER BY RAND() LIMIT '.$k; 


Now we need to process it.
 $stmt = $db->query($sql); // fetch mode $stmt->setFetchMode(PDO::FETCH_ASSOC); while($row = $stmt->fetch()) { $img_mass = unserialize($row['meta_value']); /*  $img_mass      Array ( [width] => 1000 [height] => 651 [file] => 2013/01/-.jpg [sizes] => Array ( [thumbnail] => Array ( [file] => --538x350.jpg [width] => 538 [height] => 350 [mime-type] => image/jpeg ) [medium] => Array ( [file] => --300x195.jpg [width] => 300 [height] => 195 [mime-type] => image/jpeg ) [yarpp-thumbnail] => Array ( [file] => --120x120.jpg [width] => 120 [height] => 120 [mime-type] => image/jpeg ) ) [image_meta] => Array ( [aperture] => 0 [credit] => [camera] => [caption] => [created_timestamp] => 0 [copyright] => [focal_length] => 0 [iso] => 0 [shutter_speed] => 0 [title] => ) ) */ list($year, $mount, $name) = explode("/", $img_mass['file']); /* $img_mass['file']='  2013/01/-.jpg'        $year = '2013' $mount = '01' $name = '-.jpg'*/ $img_dir = $year.'/'.$mount.'/'.$img_mass['sizes']['yarpp-thumbnail']['file']; /*        (   'yarpp-thumbnail')*/ $base [] = array ( 'post_id' => $row['post_id'], 'img_dir' => $img_dir); /*  */ } 


Step 3.2 - SQL query to get references to records

 $k_post = sizeof($base); //     for ($i=0; $i<$k_post; $i++) { /*         */ $sql=' SELECT `post_title`, `post_name` FROM `wp_posts` WHERE `ID` IN ( SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` = '.$base[$i]['post_id'].' )'; /*   */ $stmt = $db->query($sql); $stmt->setFetchMode(PDO::FETCH_ASSOC); /* */ while($row = $stmt->fetch()) { echo '<a href="http://game.tobefun.org/'.$row['post_name'].'.html" title="'.$row['post_title'].'"><img width="120" height="120" src="http://game.tobefun.org/wp-content/uploads/'.$base[$i]['img_dir'].'"></a>'; } } $db = null; //  } // <-    IF ?> 

Source: https://habr.com/ru/post/180001/


All Articles