我尝试了各种方法,但无法在我的图库中显示标题。我正在使用CMB2的文件列表输出图像附件。我希望这个问题对这个论坛足够相关,因为我想我只是缺少一些简单的php或wordpress函数。目前,它确实输出图像和空的p标签。谢谢你的帮助!
过去我用过echo get_post(get_post_thumbnail_id())->post_excerpt;
在帖子模板中。
function soth_output_gallery_file_list( $file_list_meta_key, $img_size ) {
// Get the list of files
$files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );
echo \'<div class="entry-content row">\';
foreach ( (array) $files as $attachment_id => $attachment_url ) {
echo \'<div class="small-6 medium-4 column text-center">\';
echo wp_get_attachment_image( $attachment_id, $img_size );
echo \'<p class="wp-caption-text">\';
//what am I missing?
echo $missingSomething->post_excerpt;
echo \'</p></div>\';
}
echo \'</div>\';
}
最合适的回答,由SO网友:Sumit 整理而成
你只需要通过$attachment_id
而不是get_post_thumbnail_id()
在里面echo get_post(get_post_thumbnail_id())->post_excerpt;
具有wptexturize
以防止标记破坏其他内容。
Update: 正如@birgire所建议的,我认为使用get_post_field()
而不是直接访问post属性。
更新的代码:-
function soth_output_gallery_file_list( $file_list_meta_key, $img_size ) {
// Get the list of files
$files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );
echo \'<div class="entry-content row">\';
foreach ( (array) $files as $attachment_id => $attachment_url ) {
echo \'<div class="small-6 medium-4 column text-center">\';
echo wp_get_attachment_image( $attachment_id, $img_size );
echo \'<p class="wp-caption-text">\';
//Get post excerpt by attachment ID
echo wptexturize( get_post_field( \'post_excerpt\', $attachment_id ) );
echo \'</p></div>\';
}
echo \'</div>\';
}