• Hi there,

    I`ve got the following code:

    <?php if (have_posts()) : while (have_posts()) : the_post();?>
               <h2 id="post-<?php the_ID(); ?>"><?php the_title();?></h2>
            <div class="galeryColumn">
                <?php the_content(); ?>
            </div>
        <?php endwhile; endif; ?>
        <?php edit_post_link('Edit'); ?>

    How do I make the <?php the_content(); ?> pull only the attachments in the content of the page?

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • You will need to modify the Loop to use a function such as get_children(), and then output the results.

    This function will return an array of attachment IDs, that you can use to output the attachments, or links to the attachments, or whatever you’re wanting to do.

    Thread Starter Gesp

    (@gesp)

    Can you show me how I can do it please?

    Not really, without knowing exactly what you want to do.

    Did you read the linked Codex page? It explains how to use the function in general.

    Thread Starter Gesp

    (@gesp)

    I wanted to pull every attachment from a page and hang it on the masonry grid, but I need that code to do it. I can do it with posts because the usual loop pulls the posts already.

    I wanted a loop that only pulled the attachments.

    Then you want to use something like get_posts() to create an ad-hoc Loop. e.g.

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

    This will add an array containing all of the attachments for the current post.

    Then you need to output them:

    foreach ( $postattachments as $attachment ) {
         // Add your HTML markup here
         // I don't know what it is, so you'll have to figure this part out
         // However, this will output the 'thumbnail' size image:
         echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
     }

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Attachments?’ is closed to new replies.