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 need to know how to display all images from the current page siblings.

for eg. I got two parents (Hockey and football) with children. In all this children I got one or several images attached (I've simply uploaded them through wordpress deafult uploader). Now I want a page (called "All images from all teams" ) that display all images that are attached to their siblings / has the same parent as current. AND if you click on let say a images taken from the "New York Rangers" page, you will be sent to that page.

Hockey

  • Detroit Red Wings
  • Toronto Maple Leafs
  • New York Rangers
  • All images from all hockey teams

Football

  • Liverpool
  • Arsenal
  • Manchester United
  • All images from all football teams

Help! Please!

My code:

<?php get_header(); ?>   <?php if (have_posts()) : ?>   <?php while (have_posts()) : the_post(); ?>  <div class="big-img"> <?php echo do_shortcode('[portfolio_slideshow size=full navpos=disable trans=scrollHorz pagerstyle=bullets autoplay=true]'); ?> </div>     <div class="post-content"> <div class="left-nav">  <?php include "left-sidebar.php";?>  </div>  <div class="content">  <div class="post-title"> <h1><?php the_title(); ?></h1></div>  <?php the_content('read the entry &#187;'); ?> <?php the_tags( '<div class="main-meta"> Tags: ', ', ', '</div>'); ?> <?php echo do_shortcode('[portfolio_slideshow size=thumbnail carousel=false navpos=disable ]'); ?>  <div class="clear"></div> </div>   <?php endwhile; ?>  <?php next_posts_link('&laquo; old Posts') ?> <?php previous_posts_link('new Posts &raquo;') ?>  <?php else : ?>  Not Found  <?php include (TEMPLATEPATH . "/searchform.php"); ?>   <?php endif; ?>  </div>  </div>  <div class="right-nav">  <?php include "right-sidebar.php";?>  </div>  <?php include "footer.php";?>
share|improve this question
    
Can you post the code that you already have, or where you are stuck on exactly? Am I right that you need a function where you retrieve the attachmentchildren of the pages that have the same parentpage as the current one? –  fischi Feb 26 '13 at 10:27
add comment

1 Answer

Nice to see Liverpool at the top there!

I'd go about this using a page template. Given that as far as I'm aware we only know the page names I'll add it in that way.

The process is this:

  1. Loop through top level pages
  2. Collect child pages as array
  3. Loop through child pages
  4. Loop through child attachments

    <?php
    
    /*
    Template Name: All images
    */
    
    get_header();
    
    // pages to loop through
    $sports = array( 'Hockey', 'Football' );
    
    // empty array where we collect teams
    $teams = array();
    
    // collect teams
    foreach( $sports as $sport ) {
    
        $page = get_page_by_title( $sport );
    
        $teams = array_merge(
                    get_children( array(
                        'post_parent' => $page->ID,
                        'numberposts' => -1,
                        'post_type' => 'page'
                    ) ),
                    $teams
                );
    
    }
    
    // you could do some custom sorting or randomisation of the order here
    
    // loop through teams
    foreach( $teams as &$team ) {
    
        // get image attachments for each team
        $images = get_children( array(
                'post_parent' => $team->ID,
                'numberposts' => -1,
                'post_type' => 'attachment',
                'post_mime_type' => 'image'
                ) );
    
        // output images
        foreach( $image as &$image ) {
    
            // link to team page
            echo '<a href="' . get_permalink( $team->ID ) . '">';
            echo wp_get_attachment_image( $image->ID, 'medium' );
            echo '</a>';
    
        }
    
    }
    
    get_sidebar();
    
    get_footer();
    

You'll want to modify the markup a bit and bear in mind this will be quite slow so you may want to consider a caching plugin.

share|improve this answer
    
Thank you so much, i'll try your code asap! YNWA –  Andy-with-love Feb 26 '13 at 13:00
    
If you want to display the portfolio slideshow thing for each team you can use the do_shortcode() snippets from your code in the teams loop rather than looping over the images. I'm assuming the shortcode will loop through the images for you –  sanchothefat Feb 26 '13 at 14:10
add comment

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.