最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post thumbnails - array set title and alt in the_post_thumbnail

programmeradmin1浏览0评论

Using this code:

<?php
if ( has_post_thumbnail() ) {
    the_post_thumbnail("mini-me", array(
        'class' => 'x-img smpic x-img-circle',
        'alt' => the_title(),
        'title' => the_title()
    ));
}
?>

However, alt and title are being output as text on website instead of enclosed tags. What should the syntax be instead?

Using this code:

<?php
if ( has_post_thumbnail() ) {
    the_post_thumbnail("mini-me", array(
        'class' => 'x-img smpic x-img-circle',
        'alt' => the_title(),
        'title' => the_title()
    ));
}
?>

However, alt and title are being output as text on website instead of enclosed tags. What should the syntax be instead?

Share Improve this question edited Feb 24, 2018 at 10:47 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Feb 23, 2018 at 16:44 JoaMikaJoaMika 6986 gold badges27 silver badges58 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 2

Try using get_the_title instead of the_title - from the Function Reference:

  • the_title - Display or retrieve the current post title with optional markup.

  • get_the_title - Retrieve post title.

You might notice that the_title says "Display or retrieve" - and it's true, you can pass false to the third parameter of the_title to get it's output as a return value instead of it echo'ing directly to the page, i.e.

$mytitle = the_title( '', '', false );

EDIT: Updated your code to show this in action:

<?php
if ( has_post_thumbnail() ) {
    $title = get_the_title();
    the_post_thumbnail("mini-me", array(
        'class' => 'x-img smpic x-img-circle',
        'alt'   => $title,
        'title' => $title,
    ) );
}
?>
发布评论

评论列表(0)

  1. 暂无评论