Skip to content

Instantly share code, notes, and snippets.

@mr-gradation
Created March 10, 2021 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr-gradation/f3c44fc5b93553ffd52f299f13eed99e to your computer and use it in GitHub Desktop.
Save mr-gradation/f3c44fc5b93553ffd52f299f13eed99e to your computer and use it in GitHub Desktop.
a-blog cms zoomup 2021/03 followup
/**
* wpContentThumbnail
* 本文中に /wp/wp-content/uploads/ の文字が含まれていたらその画像パスを取得する
* <img src="{eid}[wpContentThumbnail]" alt=""> のように使用する
*
* @param String $eid - 記事のEID
* @return String - 本文中に使用されている画像のパス
*/
function wpContentThumbnail($eid)
{
$DB = DB::singleton(dsn());
$SQL = SQL::newSelect('column');
$SQL->addSelect('column_field_1');
$SQL->addWhereOpr('column_field_2', 'wysiwyg', '=');
$SQL->addWhereOpr('column_entry_id', $eid, '=');
$q = $SQL->get(dsn());
$content = $DB->query($q, 'one');
if( $content ){
preg_match('/src=\"(http.*?\/wp\/wp-content\/uploads.*?)\"/', $content, $matches);
if( count($matches) > 0 ){
$thumbnail_path = $matches[1];
} else {
$thumbnail_path = "";
}
} else {
$thumbnail_path = "";
}
return $thumbnail_path;
}
/**
* wpThumbnail
* WordPressのデータベースからサムネイルのパスを取得する
* <img src="{thumbnail_id}[wpThumbnail]" alt="">のように使用する
*
* @param String $thumbnail_id - アイキャッチ画像ID
* @return String - アイキャッチ画像のパス
*/
function wpThumbnail($thumbnail_id)
{
$dsn = array(
"host" => "ホスト名",
"user" => "ユーザ名",
"pass" => "パスワード",
"name" => "データベース名",
"charset" => "UTF-8",
"prefix" => "wp_",
"type" => "mysql",
);
$DB = DB::singleton($dsn);
$SQL = SQL::newSelect('posts');
$SQL->addSelect('guid');
$SQL->addWhereOpr('ID', $thumbnail_id, '=');
$content = $DB->query($SQL->get($dsn), 'one');
if( $content ){
return $content;
} else {
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment