回答編集履歴
2
追記しました。
answer
CHANGED
@@ -1,7 +1,22 @@
|
|
1
|
+
本件、`wp_get_attachment_image_src()`を使って、srcに挿入する画像のURLを取得し、
|
2
|
+
HTML側でそのURLを好きなようにコーディングしたほうが楽かもしれません。
|
3
|
+
|
4
|
+
```PHP
|
5
|
+
<?PHP
|
6
|
+
$attachment_id = 画像のidを指定してください;
|
7
|
+
echo wp_get_attachment_image_src( $attachment_id )[0]; // 画面にURLが描画
|
8
|
+
echo '<img src="' . wp_get_attachment_image_src( $attachment_id )[0] . '">': // 画面に画像が描画
|
9
|
+
?>
|
10
|
+
```
|
11
|
+
|
12
|
+
[関数リファレンス/wp get attachment image src](https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/wp_get_attachment_image_src)
|
13
|
+
|
14
|
+
---
|
15
|
+
|
1
|
-
私が普段使いまわしているWordPressのfunctions.phpからコピペしてきました。
|
16
|
+
以下、私が普段使いまわしているWordPressのfunctions.phpからコピペしてきました。
|
2
17
|
(width、heightなどは当然出力されないようにしています。)
|
3
18
|
|
4
|
-
|
19
|
+
参考になるかもしれないので貼り付けておきます。
|
5
20
|
|
6
21
|
↓投稿時にwidthなどが挿入されないようにする仕組み、のはず
|
7
22
|
```PHP
|
1
追記しました。
answer
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
+
私が普段使いまわしているWordPressのfunctions.phpからコピペしてきました。
|
1
|
-
|
2
|
+
(width、heightなどは当然出力されないようにしています。)
|
2
3
|
|
4
|
+
以下、参考になるかもしれません。
|
5
|
+
|
6
|
+
↓投稿時にwidthなどが挿入されないようにする仕組み、のはず
|
3
7
|
```PHP
|
4
8
|
function remove_image_attribute($html) {
|
5
9
|
$html = preg_replace('/(width|height)="\d*"\s/', '', $html);
|
@@ -13,4 +17,21 @@
|
|
13
17
|
}
|
14
18
|
add_filter('image_send_to_editor', 'remove_image_attribute', 10);
|
15
19
|
add_filter('post_thumbnail_html', 'remove_image_attribute', 10);
|
20
|
+
```
|
21
|
+
|
22
|
+
↓追加。記事の画像を取得するための関数を、おそらく独自実装したもの
|
23
|
+
```PHP
|
24
|
+
function catch_that_image() {
|
25
|
+
global $post;
|
26
|
+
|
27
|
+
$tmp_content = preg_replace('/>\s*<img/', ">\n<img", $post->post_content);
|
28
|
+
|
29
|
+
if (preg_match_all('/<img.+src=[\'"]([^\'"]+[.jpg]+)[\'"].*>/', $tmp_content, $matches)) {
|
30
|
+
$first_img = $matches[1][0];
|
31
|
+
}
|
32
|
+
else {
|
33
|
+
$first_img = false;
|
34
|
+
}
|
35
|
+
return $first_img;
|
36
|
+
}
|
16
37
|
```
|