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.

I'm building a theme that display image according to a category. Exemple, There a Fashion section, which is a page. On that page I have uploaded photo related to fashion. Another page is Portraits, which display portrait photography and so on. On my home page I would like to display all the recent photo that I have uploaded through the other section/page.

Right now I can only show image related to a specific page or post with this script:

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

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

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li>';
           echo wp_get_attachment_image( $attachment->ID, 'full' );
           echo '<p>';
           echo apply_filters( 'the_title', $attachment->post_title );
           echo '</p></li>';

          }
     }

 endwhile; endif; ?>

How can I extend this script to fetch images/photos in other post/page?

I would like to specified that I'm kind of new in theme developement...

Thanks in advance for your help.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

You can achieve that in many ways , but providing that you use a relatively new wordpress, you should use the_post_thumbnail().

you define it in functions.php , like so :

add_theme_support( 'post-thumbnails' ); //thumnails set_post_thumbnail_size( 150,230, true ); // defaultpost thumbnails 
add_image_size( 'your-custom-name-2', 400, 9999 ); // some size thumbnail  
add_image_size( 'your-custom-name-2', 400, 300,false ); // another size 
add_image_size( 'whatEverName', 100, 100, false ); // yet another

The "false" or "true" parameters regard the cropping mode (read more below ) http://codex.wordpress.org/Function_Reference/the_post_thumbnail

and also here

Display thumbnail from custom field

you should then define a post default image when you upload an image (you will see it on the upload screen ) - then you can just call it in your theme like so :

the_post_thumbnail('whatEverName'); //depending on the size you want.

this however , will get you ONE defined image . It will also get you the IMAGE ITSELF . if you want the URL, size or something else, you should use

get_the_post_thumbnail($page->ID, 'whatEverName');

If you want multiple images - you can use this :

<?php //GET THE FIRST IMAGE
    $args = array(
    'order'          => 'ASC',
    'orderby'        => 'menu_order',
    'post_type'      => 'attachment',
    'post_parent'    => $post->ID,
    'post_mime_type' => 'image',
    'post_status'    => null,
    'numberposts'    => 1, //change if you want another number
    );
    $attachments = get_posts($args);
        if ($attachments) {

            foreach ($attachments as $attachment) {

            echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
    }
} ?>

// CONTENT AND OTHER STUFF...

<?php //GET ALL EXPECT FIRST IMAGE
    $args = array(
    'order'          => 'ASC',
    'orderby'        => 'menu_order',
    'post_type'      => 'attachment',
    'post_parent'    => $post->ID,
    'post_mime_type' => 'image',
    'post_status'    => null,
    'numberposts'    => -1,
    );
    $attachments = get_posts($args);
        if ($attachments) {
    $no_show=true;
            foreach ($attachments as $attachment) {
    if($no_show) {$no_show=false; continue; }
            echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
    }
} ?>
share|improve this answer
    
Hi, not sure you see exactly what I meant. I probably did not explain it well. You you go see at my site what I mean. chrisbergeronphoto.com/dummy you will see more what I mean. If you click on Fashion for exemple, you'll see all the images I uploaded in fashion (it's a slideshow). if you click on other link you'll see images that are related to there page. What I want is to show on "Recent work" image that belong to other page! I'm not shure how to achieve this, and the way you showed me I'm not sure it applied to what I want to do. –  Christian Dec 27 '11 at 4:49
1  
well, than just use it outside the loop , or put the categories/pages/categories you want inside the arguments in the same code I posted .. –  krembo99 Dec 27 '11 at 9:28
    
Nice site by the way –  krembo99 Dec 27 '11 at 9:29
    
Hi again, I'm glad that you like the site! Thanks! I don't know what I'm doing wrong, but the code you have posted work only if I upload images in the page the code reside. (on my theme it's called page-test.php, and it's a template page). All the other page I picked a Feature thumbnail and it's not working. I'm trying to find the argument to call a page instead of a category and find nothing... I don't believe that I'm the only one on the planet who want to do something like that, it most exist a solution. –  Christian Dec 27 '11 at 22:08
    
All right, I thing I'm getting there... I changed in the $args, $post->ID for $page->ID and it output all the pictures that have been post in the other pages. Nice, but I still want to control which picture will be shown is there a way to achieve this? –  Christian Dec 27 '11 at 22:24
show 5 more comments

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.