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

attachments - How do I get just the title from wp_get_attachment_image

programmeradmin0浏览0评论

I can get the image to appear fine in this bit of gallery code. However the flex slider gets its caption from a <p> tag (class="flex-caption") and I just need to echo out the image title inside that paragraph.

<?php if (!empty($flex_gallery)) {?>                    
    <div class="flex-container resize-620">
        <div class="flexslider">
            <ul class="slides">     
            <?php
                foreach ($flex_gallery as $att) {
                    echo ( '<li>' . wp_get_attachment_image( $att , 'blog-full-width' ).
                    '</li>
                    <p class="flex-caption">' . I NEED JUST THE IMAGE TITLE HERE . '</p>'); 
                }
            ?>
            </ul>
        </div>
    </div>              
<?php } else { ?>       
    <?php the_post_thumbnail(
        'blog-full-width',
        array('class' => 'featured-full-width-top'));
    ?>      
<?php } ?>

I can get the image to appear fine in this bit of gallery code. However the flex slider gets its caption from a <p> tag (class="flex-caption") and I just need to echo out the image title inside that paragraph.

<?php if (!empty($flex_gallery)) {?>                    
    <div class="flex-container resize-620">
        <div class="flexslider">
            <ul class="slides">     
            <?php
                foreach ($flex_gallery as $att) {
                    echo ( '<li>' . wp_get_attachment_image( $att , 'blog-full-width' ).
                    '</li>
                    <p class="flex-caption">' . I NEED JUST THE IMAGE TITLE HERE . '</p>'); 
                }
            ?>
            </ul>
        </div>
    </div>              
<?php } else { ?>       
    <?php the_post_thumbnail(
        'blog-full-width',
        array('class' => 'featured-full-width-top'));
    ?>      
<?php } ?>
Share Improve this question edited Mar 4, 2012 at 12:03 mor7ifer 8,6162 gold badges20 silver badges33 bronze badges asked Mar 4, 2012 at 7:59 john joejohn joe 2051 gold badge3 silver badges6 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

You should be able to get the image title with wp_get_attachment_metadata(). It will return an array that contains useful stuff like the width and height, as well as the title.

you should be able to get it using get_post_title($att)

I follow a few ways to get Title for Images.

Foreach Loop: If I need separate Title from Image then:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();

    $attachments = get_posts( array(
        'post_type'   => 'attachment',
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => $post->ID
    ) );

    if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
            ?>
            <li><?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
                <p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p>
            </li>
            <?php
        }
    }
endwhile; endif; ?>

Second way for Alt Images:

<?php the_title_attribute($post->ID);?>

Third way:

<?php echo get_the_title( $thumbnail_id );?>

For more details please check Official Documentation

发布评论

评论列表(0)

  1. 暂无评论