0 レビュー
3 回答
php-Wordpressの添付画像のキャプションを取得
ここに記載されているように添付ファイルのメタキャプション値を取得しようとしましたが、出力を取得できませんでした。 [created_timestamp]や[iso]のような他のメタ配列はそれらの値を与えました。
$img_meta = wp_get_attachment_metadata( $id );
echo $img_meta[image_meta][caption];
この問題は、[caption]と[title]の両方で発生します。どんな助けでも大歓迎です。
わからない
0
レビュー
答え :
解決策:
wp_get_attachment_metadataから取得しようとしているキャプションとタイトルは、WordPressで追加するタイトルとキャプションではなく、実際の画像自体からのメタデータです。 WordPressデータを取得するには、次のようなものを使用します($ idが画像のIDであると想定します)。
$image = get_post($id);
$image_title = $image->post_title;
$image_caption = $image->post_excerpt;
わからない
0
レビュー
答え :
解決策:
これをfunctions.phpファイルに入れてください:
function show_caption_image($type='title'){
global $post;
$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'numberposts' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;
}
}
return $type == 'title' ? $image_title : $caption.$description;
}
テーマ内の画像の下、または画像を配置したい場所、通常はsingle.phpファイル:
<?php if ( has_post_thumbnail() ) :
?>
<span class="image main"><img src="<?php echo get_the_post_thumbnail_url()?>" alt="<?php echo get_the_title()?>" /><i><?php echo show_caption_image();?></i></span>
<?php endif; ?>
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。