.htaccessでPHPのファイルアップロードサイズの上限値を変更
2022.02.5
PHPを使用して画像(動画・PDF等)をアップロードするフォームを作成した際に、ファイル…
WordPress(ワードプレス)を使ってサイトを作る際に、既に別サイト(同一サーバー内)で作成しているコンテンツなどを共有して制作したい場合(本サイトとは別のデータベースを読み込みたい場合)の対応方法をメモ。
//他のデータベースに接続
$another_db_user = 'データーベースユーザー名';
$another_db_pass = 'データーベースパスワード';
$another_db_name = 'データーベース名';
$another_db_host = 'データーベースホスト名';
$another_tb_prefix = 'wp○○_';
$another_wpdb = new wpdb($another_db_user, $another_db_pass, $another_db_name, $another_db_host);
$another_wpdb->set_prefix($another_tb_prefix);
こちらのサンプルでは追加で読み込むデータベースもワードプレスを使用し、投稿一覧の内容を表示しています。
/*
Template Name: 読み込みに使用するページのテンプレート名
*/
//SQLでデーターベースに直接アクセスして、情報をとる
$result = $another_wpdb->get_results('
SELECT * FROM '.$another_wpdb->posts.'
WHERE post_type = "post"
&& post_status = "publish"
ORDER BY post_date DESC
');
foreach ($result as $value) {
$the_post_id = $value->ID; // カテゴリとアイキャッチを取得したい投稿のID
//カテゴリーの取得
$categories = '';
$get_term_relationships_res = $another_wpdb->get_results('SELECT '.$another_wpdb->term_relationships.'.term_taxonomy_id as term_taxonomy_id FROM '.$another_wpdb->term_relationships.', '.$another_wpdb->term_taxonomy.' WHERE '.$another_wpdb->term_relationships.'.term_taxonomy_id = '.$another_wpdb->term_taxonomy.'.term_taxonomy_id && '.$another_wpdb->term_relationships.'.object_id = "'.$the_post_id.'" && '.$another_wpdb->term_taxonomy.'.taxonomy = "category"');
foreach($get_term_relationships_res as $row_term_relationships){
$get_terms_res = $another_wpdb->get_results('SELECT name FROM '.$another_wpdb->terms.' WHERE term_id = "'.$row_term_relationships->term_taxonomy_id.'"');
foreach($get_terms_res as $row_terms){
$categories .= $row_terms->name;
}
}
//アイキャッチの取得
$get_postmeta_res = $another_wpdb->get_results('SELECT meta_value FROM '.$another_wpdb->postmeta.' WHERE meta_key = "_thumbnail_id" && post_id = "'.$the_post_id.'"');
foreach ($get_postmeta_res as $row_get_postmeta ) {
$get_attachment_res = $another_wpdb->get_results('SELECT * FROM '.$another_wpdb->posts.' WHERE post_type = "attachment" && ID="'.$row_get_postmeta->meta_value.'"');
foreach($get_attachment_res as $row_get_attachment){
$eyecatch = $row_get_attachment->guid;
}
}
//表示内容
echo('<li class="post">
<span class="cat-name">'.$categories.'</span>
<a href="'.$value->ID.'">
<figure class="eyecatch"><img src="'.$eyecatch.'"></figure>
<div class="entry-content">
<h2 class="entry-title">'.$value->post_title.'</h2>
<p class="entry-meta">'.$value->post_date.'</p>
<p class="description">'.mb_substr(strip_tags($value->post_content), 0, 50, 'UTF-8').'</p>
</div>
</a>
</li>');
}
2022.02.5
PHPを使用して画像(動画・PDF等)をアップロードするフォームを作成した際に、ファイル…
2020.10.3
テキストエリア等で入力した文字の中にURLを含んでいた場合に、自動で認識してリンクタグを…
2024.03.30
外部ファイル(CSVファイル等)を読み込んでサイト内に表示する際に、保存時の文字コードが…
2020.12.5
メールフォームからメールを送信する際に、画像(ファイル)を添付できるようにしてほしいとの…
2023.12.2
ワードプレスの投稿のカテゴリー機能を、目的ごとに使い分けたいというご要望があった際に、カ…
2020.08.1
動画ファイルをCMS化して保存するときに、多くの場合はファイルパスをデータベース(MyS…
2019.12.29
メールフォーム等での入力値チェック(バリデーション)。PHPの「preg_match」(…
2022.02.19
画像データ等をサーバーにアップロードする際、一点づつアップロードしても良いけれど、ファイ…
2020.08.15
フォームの入力値やURLのパラメータ等、文字列や数値を受け取る際に正規表現を利用してバリ…
2022.09.24
パソコンやスマートフォンの戻るボタンを使って画面を戻す(ブラウザバックする)時があります…