简体   繁体   English

如何在Wordpress中添加特色图像标题

[英]How to Add a Featured Image Caption in Wordpress

I am hoping someone may be able to help me with a problem. 我希望有人可以帮我解决问题。 I am building a news web site for a friend of mine. 我正在为我的一个朋友建立一个新闻网站。 The site is starting to come together, but I cannot find out how to add captions to featured images. 该网站开始走到一起,但我无法找到如何为特色图片添加字幕。 I have been looking all over the web today, there are loads of ways to do it by adding code to php sheets, but I am not sure what I am doing. 今天我一直在寻找网络,有很多方法可以通过向php工作表添加代码来实现,但我不确定我在做什么。

I have added so code to the functions.php code, but the everything goes pair shaped. 我已将这些代码添加到functions.php代码中,但所有内容都是成对的。

I am keeping my fingers crossed, someone will be able to help me by typing me through what to do. 我保持手指交叉,有人可以通过键入我做什么来帮助我。

Thank you in advance for your help. 预先感谢您的帮助。

Kind regards 亲切的问候

John 约翰

First, you'll need to drop the following code in your functions.php file: 首先,您需要在functions.php文件中删除以下代码:

function the_post_thumbnail_caption() {
  global $post;

  $thumbnail_id    = get_post_thumbnail_id($post->ID);
  $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));

  if ($thumbnail_image && isset($thumbnail_image[0])) {
    echo '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
  }
}

Paste it right before the closing PHP tag in that file, if there isn't a closing PHP tag then make sure there's no empty lines below the code you paste as that can cause problems. 将其粘贴到该文件中关闭的PHP标记之前,如果没有关闭的PHP标记,则确保在您粘贴的代码下方没有空行,因为这可能会导致问题。

Then, where you'd like the caption to be displayed, you'll need to call it with this: 然后,在您希望显示标题的位置,您需要使用以下方法调用它:

<?php the_post_thumbnail_caption(); ?>

If you're unsure where to put the call in your template files, you'll need to find where <?php the_post_thumbnail(); ?> 如果您不确定将调用放在模板文件中的哪个位置,则需要找到<?php the_post_thumbnail(); ?> <?php the_post_thumbnail(); ?> is being called. <?php the_post_thumbnail(); ?>被调用。 Just look for that line in your template file, and place the function call near it, where ever you'd like the caption to be displayed at. 只需在模板文件中查找该行,并将函数调用放在它附近,您可以在哪里显示标题。 The function wraps the caption in a span tag automatically so you can target it with CSS, but you can also wrap the function call in any tag you'd like to. 该函数自动将标题包装在span标记中,以便您可以使用CSS对其进行定位,但您也可以将函数调用包装在您想要的任何标记中。

So for example, if your template file is calling the featured image with this or something very similiar: 因此,例如,如果您的模板文件使用此类或非常类似的方式调用特色图像:

<?php 
    if ( has_post_thumbnail() ) {
        the_post_thumbnail();
} ?>

You'd want to add the caption call to it like this: 你想要像这样添加标题调用:

<?php 
    if ( has_post_thumbnail() ) {
        the_post_thumbnail();
} ?>
<?php the_post_thumbnail_caption(); ?>

Let me know if you need any other clarification. 如果您需要任何其他说明,请与我们联系。

I know this is an old question, but I wanted to offer another solution that others might prefer, if you would rather deal with data that is already available (less database calls) while using fields already available in WordPress. 我知道这是一个老问题,但我想提供其他人可能更喜欢的另一种解决方案,如果您希望在使用WordPress中已有的字段时处理已经可用的数据(减少数据库调用)。

Since the output of get_the_post_thumbnail() returns the entire img tag as a string with the alt tag pre-populated from the caption field, you can simply extract the alt tag of the image tag by using xml parser or regex. 由于get_the_post_thumbnail()的输出将整个img标记作为字符串返回,并且从标题字段预先填充了alt标记,因此您可以使用xml解析器或正则表达式简单地提取图像标记的alt标记。

I did this: 我这样做了:

    <?php 
    if ( $featured = get_the_post_thumbnail( get_the_ID() ) ):

        echo $featured; 
        preg_match('/alt="(.*)"/i', $featured, $caption);

        if ($caption) {
            echo wpautop(array_pop($caption));
        }
    endif;
    ?>

If you want it in a function like the originally accepted answer, you pass the img tag directly to your function: 如果你想在最初接受的答案中使用它,你可以将img标签直接传递给你的函数:

    function get_the_post_thumbnail_caption($img_tag) {
        preg_match('/alt="(.*)"/i', $img_tag, $caption);
        return array_pop($caption);
    }

And in the template: 在模板中:

    $img = get_the_post_thumbnail( get_the_ID() ); // or any id
    echo wpautop( get_the_post_thumbnail_caption( $img ) );

Note that if you populate the 'alt text' field, it will overwrite the caption field data when outputting the img tag. 请注意,如果填充“替代文字”字段,它将在输出img标记时覆盖标题字段数据。 You can override that behavior with filters if necessary. 如有必要,您可以使用过滤器覆盖该行为。

the_post_thumbnail();                  // without parameter -> Thumbnail

the_post_thumbnail('thumbnail');       // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium');          // Medium resolution (default 300px x 300px max)
the_post_thumbnail('large');           // Large resolution (default 640px x 640px max)
the_post_thumbnail('full');            // Original image resolution (unmodified)

the_post_thumbnail( array(100,100) );  // Other resolutions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM