私のワードプレスブログでは、phpThumbnail
ライブラリを使用して投稿内のすべての画像をサムネイルに変換するコード/関数を設定しようとしています。
これまでのところ、投稿で画像の配列を生成し、_thumbnail_id
メタキーを使用して最初の画像からサムネイルを作成するこの関数があります。
function sThumbnail() {
$post_id = get_the_ID();
$post = get_post($post_id);
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
var_dump($matches);
$first_img = substr($first_img, strlen(site_url()));
$post_thumbnail = get_post_meta( $post_id, '_thumbnail_id', true );
if (!wp_is_post_revision($post_id)) { // Verify that post is not a revision
if (empty($post_thumbnail)) { // Check if Thumbnail does NOT exist!
if(empty($first_img)){
update_post_meta( $post_id, '_thumbnail_id', 'http://static.guim.co.uk/sys-images/Guardian/About/General/2012/11/1/1351779022327/Nicki-Minaj---You-ve-neve-010.jpg' );
} else {
update_post_meta( $post_id, '_thumbnail_id', $first_img );
} // Get the first image of a post (if available)
}
}
}
いくつかの変更を加えると、関数を使用して投稿内のすべての画像を取得し、それらに対して何かを行うことができます。
問題はsrc
、タグの属性を変更する方法<img>
や削除して新しいものを入力する方法がわからないことです。
基本的に必要なのは、属性を変更する 1 ~ 2 行、src
またはタグとその属性を変更/操作する方法に関する一般的な情報だけです。
いくつかのポイント:
- 明確にするために、src
、つまり<img src="..." />
.
- 上記の関数は、私が使用する関数ではありません!
最初の 6/7 行はイメージタグのsrc
属性の内容を取得する方法であるため、参考用です。