是否有一个函数可以在 wordpress 中获取图像的标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1616281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:32:53  来源:igfitidea点击:

Is there a function to get the caption for an image in wordpress

wordpressimagecaptions

提问by Paul Sheldrake

I'm trying to get the captions from images in Wordpress but I can't find an easy function to grab this bit of information.

我正在尝试从 Wordpress 中的图像中获取标题,但我找不到一个简单的函数来获取这些信息。

Anyone know a way to get this?

有谁知道有什么办法得到这个吗?

Thanks

谢谢

采纳答案by Paul Sheldrake

Turns out captions are stored as post excerpts. So,

原来字幕存储为帖子摘录。所以,

<?php echo $post->post_excerpt; ?>

will print out the caption if you are on the attachment image page (image.php in your theme) and inside the Loop.

如果您在附件图像页面(主题中的 image.php)和循环内,将打印出标题。

回答by unscripted

If you are trying to get the caption while in a post you can echo it out inside your "the_post_thumbnail" tag.

如果您想在帖子中获取标题,您可以在“the_post_thumbnail”标签中将其回显。

<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>

You can also use the same method to show the image description. This is a little better feature in WordPress 3.5

您也可以使用相同的方法来显示图像描述。这是 WordPress 3.5 中的一个更好的功能

<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_content; ?>

If you need to style the caption or description you can wrap it in a div like below.

如果您需要设置标题或描述的样式,您可以将其包装在如下所示的 div 中。

<?php the_post_thumbnail();
    echo '<div class="myDiv">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>'
; ?>

Hope that helps.

希望有帮助。

回答by ringstaff

Using Wordpress 4.8, this little guy worked for me:

使用 Wordpress 4.8,这个小家伙对我来说有效:

<?php the_post_thumbnail_caption(); ?>

回答by user3794429

Put this inside figure tag of your single.php file

把它放在你的 single.php 文件的图形标签中

$image_caption = get_post(get_post_thumbnail_id())->post_excerpt;
if(!empty($image_caption)) { 
    echo '<figcaption itemprop="caption">' . $image_caption . '</figcaption>'; 
}

回答by Rubel Miah

I'm using this code, It works fine.

我正在使用此代码,它工作正常。

$get_description = get_post(get_post_thumbnail_id())->post_excerpt; if(!empty($get_description)){//If description is not empty show the div    echo '<div class="img-caption">' . $get_description . '</div>'; }