サイト内に2つWordpressを設置しており、片方のwpのトップページにblogの記事を表示したいと考えています。
状況はこちらの質問者様とほぼ同様です。
(参考)https://teratail.com/questions/24274
まずfunctions.phpに以下を記入しました。
/*DB設定を行います。*/ $another_db_name = 'dbname'; $another_db_user = 'dbuser'; $another_db_pass = 'dbpass'; $another_db_host = 'another_host'; $another_tb_prefix = 'wp_hoge_'; $anoteher_wpdb = new wpdb($another_db_user, $another_db_pass, $another_db_name, $another_db_host); //プレフィックスの設定 $anoteher_wpdb->set_prefix($another_tb_prefix);
htmlには、以下のように記入しています。
php
1<?php 2 //一覧情報取得 3 $results = $anoteher_wpdb->get_results(" 4 SELECT post_title, guid, ID, post_date, post_content 5 FROM $anoteher_wpdb->posts 6 WHERE post_type = 'post' 7 AND post_status = 'publish' 8 ORDER BY post_date DESC 9 LIMIT 3 " 10 ); 11 12 $the_post_id = $value->ID;// カテゴリとアイキャッチを取得したい投稿のID 13 14 $sql_for_categories = "SELECT t.term_id, t.name, t.slug 15 FROM {$anoteher_wpdb->term_relationships} AS tr 16 INNER JOIN {$anoteher_wpdb->terms} AS t ON t.term_id = tr.term_taxonomy_id 17 WHERE tr.object_id = {$the_post_id}"; 18 19 $categories = $anoteher_wpdb->get_results($sql_for_categories, ARRAY_A); 20 21 $sql_for_eyecatch = "SELECT meta_value 22 FROM {$anoteher_wpdb->postmeta} 23 WHERE post_id = (SELECT meta_value FROM {$anoteher_wpdb->postmeta} WHERE post_id = {$the_post_id} AND meta_key = '_thumbnail_id') AND meta_key = '_wp_attached_file'"; 24 25 $eyecatch = $anoteher_wpdb->get_var($sql_for_eyecatch); 26 27 //表示 28 foreach ($results as $value) { 29 $date = str_replace('-', '/', mb_substr($value->post_date, 0, 10)); ?> 30 31 <div> 32 <p><?=$value->taxonomy?></p> 33 <p><?=$eyecatch?></p> 34 <p><?=$categories?></p> 35 <p><?=$date?></p> 36 <h3><a href="<?=$value->guid?>" target="_blank"><?=$value->post_title?></a></h3> 37 </div> 38 39 <?php } ?>
取得したい情報は、blogの投稿記事最新3件です。
【取得できた情報】
・ページタイトル
・投稿日時
【取得できていない情報】
・カテゴリ
・アイキャッチ画像
参考URLの質問者様と同じくカテゴリがすべてArrayになり、アイキャッチが表示されません。var_dump関数で調べても、NULLです。
質問者様はその後解決されているのですが、どのように記載を修正されたのか理解できず、、
どうぞよろしくお願いいたします。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/20 09:47
2016/06/20 10:15