Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Looking to create a customized single.php for my posts. In one column I'd like to display all images added through the add media function (inserted into post). In the next column all other content associated with the post (except images).

What I need is the proper / best practice method of doing so. I am just beginning to decipher php but want to do things properly from the VERY beginning.

So how can look at the post, pull just the attachment images for display in one column, pull just the textual content (with markup) for another column?

Also, when would I consider creating this as a function for functions.php instead of the process residing inside of single.php or any other template file for that manner?

Here's what I have so far:

<div class="content">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div class="images">
    <?php
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); 
    $attachments = get_posts($args);
    if ($attachments) {
        foreach ( $attachments as $attachment ) {
            the_attachment_link( $attachment->ID , false );
        }
    }
    ?>
</div>
<div class="specs">
    <h2><?php single_post_title(); ?></h2>
    <?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria'); ?></p>
<?php endif; ?>

share|improve this question

2 Answers 2

This is really a CSS/HTML question as WordPress does not control your layout/styles.

You can use one loop and fling the data wherever you want basically.

For instance you can wrap individual functions in their own markup

//loop start

<div class="left"><?php the_title(); ?></div>  //float this title left
<div class="right"><?php the_content(); ?></div>  //float this content right
<div class="somewhere"><?php some_other_function(); ?></div> //position this where ever
<div class="image-box"><?php echo wp_get_attachment_image(); ?></div> 
//position a css box for images

//loop end
share|improve this answer
    
Thanks man. I understand css / html fully. Perhaps I wasn't clear in my description but nonetheless echo wp_get_attachment_image(); delivers an error. Using the_content will show images if inserted into post which is not what I want (read above). –  Devise Sep 22 '11 at 0:52
    
If you insert images into the post content, that is where they will be, the only way to style columns in content is with html tables. The real way to do this is to keep the content as just text and use a WordPress function to grab the images attached to the post, that way you can have way more control over the output, for instance codex.wordpress.org/Function_Reference/get_children The above was just an example of how you style a theme. –  Wyck Sep 22 '11 at 1:02
    
Yea man. "The REAL way to do it" is what I'm asking for. I know how to style things properly. SO using get_children (or get_posts) how would I achieve what I'm looking for? Thanks again. –  Devise Sep 22 '11 at 1:15
    
Posted what I have so far - perhaps that will help. I have the images showing. I figured out that if you upload images through the editor (but do not insert) they are still available as attachments. This way I can avoid stripping from the_content(); where I'm only looking for text & markup. However is this a good way to do this? And should I be moving the attachment functionality into functions.php? –  Devise Sep 22 '11 at 1:29
    
Ya, this is how you typically do this, when you upload an image it is attached to that post (you do not have to insert it or anything). Another more dirty but quick way its to use the '[gallery]' shortcode, echo do_shortcode('[gallery]');.You can move it to your functions or keep it in your template, honestly it doesn't really matter, it's up to you and how you organize your code/theme. Personally I would keep this in a theme file that is specific to this output. –  Wyck Sep 22 '11 at 2:03
up vote 0 down vote accepted
<div class="content">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div class="images">
    <?php
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); 
    $attachments = get_posts($args);
    if ($attachments) {
        foreach ( $attachments as $attachment ) {
            the_attachment_link( $attachment->ID , false );
        }
    }
    ?>
</div>
<div class="specs">
    <h2><?php single_post_title(); ?></h2>
    <?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria'); ?></p>
<?php endif; ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.