WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] List current pages subpages and first attached image. (2 posts)

  1. drewbe121212
    Member
    Posted 3 years ago #

    Hey all, I am just getting my feet wet with wordpress for the first time, but I am having some issuse getting this to work exactly as I want.

    Basically, my client wants a 'portfolio' section on there website to show off different categories of there work.

    I set the portfolio up like this using pages:

    Portfolio (/portfolio/)
    -- category 1 (/portfolio/portfolio1/)
    -- category 2 (/portfolio/portfolio2/)
    -- category 3 (/portfolio/portfolio3/)

    On the main landing portfolio page, I am using a theme template to get all the children and display the page title, along with the first image from the gallery.

    This almost works as I want it too. Each of the category names shows up correctly along with the link to the correct category. The part that is not working, is that it is using the image from the first category and redisplaying it for every category (even though the title/link is fine, the image is not). What's up with that? Am I just using a wrong function call here?? get_the_ID() is in fact returning different ID's for the get_children() call.

    <?php
    query_posts('post_type=page&post_parent='.$post->ID);
    while (have_posts()) {
    
    	the_post();
    ?>
    
    	<?php 
    
    	$arrImages = get_children('post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID() );
    
    	// If images exist for this page
    	if($arrImages) {
    
    	// Get array keys representing attached image numbers
    	$arrKeys = array_keys($arrImages);
    
    		// Put all image objects into new array with standard numeric keys (new array only needed while we sort the keys)
    		foreach($arrImages as $oImage) {
    			$arrNewImages[] = $oImage;
    		}
    
    		// Bubble sort image object array by menu_order
    		for($i = 0; $i < sizeof($arrNewImages) - 1; $i++) {
    			for($j = 0; $j < sizeof($arrNewImages) - 1; $j++) {
    				if((int)$arrNewImages[$j]->menu_order > (int)$arrNewImages[$j + 1]->menu_order) {
    					$oTemp = $arrNewImages[$j];
    					$arrNewImages[$j] = $arrNewImages[$j + 1];
    					$arrNewImages[$j + 1] = $oTemp;
    				}
    			}
    		}
    
    		// Reset arrKeys array
    		$arrKeys = array();
    
    		// Replace arrKeys with newly sorted object ids
    		foreach($arrNewImages as $oNewImage) {
    			$arrKeys[] = $oNewImage->ID;
    		}
    
    		// Get the first image attachment
    		$iNum = $arrKeys[0];
    
    		// Get the thumbnail url for the attachment (wp_get_attachment_url($iNum))
    		$sThumbUrl = wp_get_attachment_thumb_url($iNum);
    
    		$sImgString = '<div style="padding:5px; width:150px;">' . get_the_title() . '<a href="' . get_permalink() . '"><img src="' . $sThumbUrl . '" width="150" height="150" alt="' . get_the_title() . '" title="' . get_the_title() . '" /></a></div>';
    
    		// Print the image
    		echo $sImgString;
    
    	}
    
    }
    ?>
    
    </div>
    
    <?php wp_reset_query(); ?>
  2. drewbe121212
    Member
    Posted 3 years ago #

    ... and nevermind. I'm an idiot.

    I needed to reset the $arrNewImages = array(); It was appending the new images to the previous array and since I was always looking to grab from postition 1, it pulled the first posts images.

    $arrImages = get_children('post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID() );
    $arrNewImages = array();

Topic Closed

This topic has been closed to new replies.

About this Topic