Add title to image post

时间:2013-05-17 作者:Krathos

在最近的帖子中,我无法在特色图片上添加标题。

尝试此操作,但仅获取alt

<?php           
$default_attr = array(
    \'src\'   => $src,
    \'class\' => "attachment-$size",
    \'alt\'   => trim(strip_tags( $attachment->post_excerpt )),
    \'title\' => trim(strip_tags( $attachment->post_title )),
);
$pages = wp_get_recent_posts(); 
?> 
<ul>
    <?php foreach ($pages as $page): ?>
        <li>
            <?php echo get_the_post_thumbnail($page["ID"],\'\',$default_attr); ?>
        </li>
    <?php endforeach; ?>
</ul>

EDIT OUTPUT FIXED:

<ul>
    <li><img width="650" height="383" title="" alt="" class="attachment- wp-post-image" src=""></li>
    <li><img width="650" height="383" title="" alt="" class="attachment- wp-post-image" src=""></li>
    <li><img width="650" height="383" title="" alt="" class="attachment- wp-post-image" src=""></li>
    <li><img width="650" height="383" title="" alt="" class="attachment- wp-post-image" src=""></li>
</ul>
有什么想法吗?

SOLUTION

这是我的工作

<?php $pages = wp_get_recent_posts(); ?> 
<ul>
    <?php foreach ($pages as $page): ?>
        <?php
        $attachment_id = get_post_thumbnail_id( $page->ID );
        $attachment = get_post( $attachment_id );

        $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        ?>
        <li>
            <?php echo get_the_post_thumbnail($page["ID"],\'\', array( \'title\' => esc_attr($page["post_title"]), \'data-thumb\' => $url)); ?><br />
            <?php echo esc_attr($page["post_title"]) ?>
        </li>
    <?php endforeach; ?>
</ul>

3 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

抱歉,在的参数列表中,我没有看到任何这些参数wp_get_recent_posts.

$args = array(
    \'numberposts\' => 10,
    \'offset\' => 0,
    \'category\' => 0,
    \'orderby\' => \'post_date\',
    \'order\' => \'DESC\',
    \'include\' => ,
    \'exclude\' => ,
    \'meta_key\' => ,
    \'meta_value\' =>,
    \'post_type\' => \'post\',
    \'post_status\' => \'draft, publish, future, pending, private\',
    \'suppress_filters\' => true );
与您的代码进行比较。

    $default_attr = array(
        \'src\'   => $src,
        \'class\' => "attachment-$size",
        \'alt\'   => trim(strip_tags( $attachment->post_excerpt )),
        \'title\' => trim(strip_tags( $attachment->post_title )),
    );
    $pages = wp_get_recent_posts($default_attr);
这些是正确的参数get_the_post_thumbnail...

// straight from the Codex
$default_attr = array(
    \'src\'   => $src,
    \'class\' => "attachment-$size",
    \'alt\'   => trim(strip_tags( $attachment->post_excerpt )),
    \'title\' => trim(strip_tags( $attachment->post_title )),
);
。。。但你没有用它们,这在我看来是个错误。您的呼叫get_the_post_thumbnail 需要。。。

echo get_the_post_thumbnail($page["ID"],\'\',$default_attr);
。。您不需要将这些参数传递给wp_get_recent_posts.

看起来您已经从Codex中复制了默认参数,并试图使用这些未更改的参数。那是行不通的。这些默认参数取决于没有为代码设置的内容。其次,只需传递要更改的参数。例如

// straight from the Codex
$default_attr = array(
    \'class\' => "nifty-class", // this is added to other classes. It does not replace them
    \'alt\'   => "I am soooo alt",
    \'title\' => "And I am a title",
);
echo get_the_post_thumbnail($page["ID"],\'\',$default_attr);
如果你通过src 必须正确,否则图像将无法加载。

SO网友:Ravinder Kumar

来解决你的问题

<?php           
$pages = wp_get_recent_posts(); 
?>
<ul>
    <?php foreach ($pages as $page): ?>
        <li>
            <?php
            $attachment_id = get_post_thumbnail_id( $page->ID );
            $attachment = get_post( $attachment_id );
            ?>
            <img src="<?php echo $attachment->guid; ?>" title ="<?php echo $attachment->post_title ; ?>" alt="image for: <?php echo $attachment->post_title ; ?>" />
        </li>
    <?php endforeach; ?>
</ul>
?>

SO网友:Sven

仔细查看您的echo: 您需要添加$default_attr 到您的get_the_post_thumbnail:

<?php echo get_the_post_thumbnail( $post_id, $size, $default_attr ); ?> 
法典中也有很多很好的例子:get_the_post_thumbnail

更新:正如@Kratos指出的,代码aboce只会回应图像(标题$default_attr 仅显示在类名中),但不作为title 属性

解决方法是将title属性设置为the_post_thumbnail:

<?php the_post_thumbnail( $size, array( \'title\' => esc_attr( $page["post_title"] ) ) ); ?>

结束

相关推荐