• Resolved MDavid-ITA

    (@mdavid-ita)


    I’m working on a WP template and I would like make a feature in which the first image of a post is shown and the excerpt of the post is overlapped to the image on a opaque white background.

    I know there are a lot of Plugins doing that, but I’d prefer to build my own template bottom up in order to deeply understand WP poetry ??

    Unfortunately I am unable to retrieve attached image!

    The first attempt was to use the_attachment_link() function. Following is the code:

    <?php query_posts('posts_per_page=1'); ?>
        <?php if ( have_posts() ) : the_post(); ?>
        <h1><?php the_title(); ?></h1>
        <?php the_attachment_link(1); ?>
        <?php the_excerpt(); ?>
        <?php endif; ?>

    Probably I did not really understand the differences between attached images and/or inserted images.

    Can someone help?
    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • You might be better off using get_posts() to grab the children (attachments).

    Thread Starter MDavid-ITA

    (@mdavid-ita)

    I changed:

    <?php the_attachment_link(1); ?>

    to:

    <?php
    $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => get_the_ID() );
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ( $attachments as $attachment ) {
    		the_attachment_link( $attachment->ID , true );
    	}
    }
    ?>

    The images is displayed properly but, unfortunately, I only can choose between original size image or thumbnail. I need to show the image the same size has been attached. :-/ I thought it was simpler.

    Thread Starter MDavid-ITA

    (@mdavid-ita)

    A small code change and the problem is solved.

    Changed:
    the_attachment_link( $attachment->ID , true );
    To
    echo wp_get_attachment_image( $attachment->ID, 'large');

    Now it works!
    ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Function: the_attachment_link ()’ is closed to new replies.