• Resolved David Gard

    (@duck_boy)


    Hey all,

    I have a page that displays the profile of a random member of staff (all details taken from a Post), which is reasonably simple. Each of these staff members can have images attached to them, and I would like to display the first attached image in the gallery of the randomly displayed post. Trouble is, I have no idea how to do this!

    Any help would be appriciated.

    Thanks.

    <!-- Random employee displayed here -->
    <div class="pinkLinks floatLeft staffPreview">
    
    	<?php
    	global $post;
    	$randomStaff = get_posts('numberposts=1&category=64&orderby=rand');
    	foreach($randomStaff as $post) :
    		setup_postdata($post); ?>
    		<h2 href="<?php the_permalink(); ?>"><?php the_title(); ?></h2>
    		<?
    		$content = explode('.', strip_tags(get_the_content()));
    		$content = $content[0];
    		?>
    		<p><?php echo $content; ?>. <a href="<? the_permalink(); ?>">Read more...</a></p>
    		<br />
    		<h3 class="pink">Services Offered</h3>
    			<ul class="staffPageSmallContent">
    			<?php
    			$all_cats = get_the_category();
    
    			foreach($all_cats as $category){
    				if ($category->category_parent == "51" || $category->category_parent == "38"){
    				$categories[] = $category;
    				}
    			}
    
    			if(!empty($categories)){
    
    				foreach($categories as $category){
    					$guid = $wpdb->get_var('SELECT guid FROM '.$wpdb->posts.' WHERE post_title="'.$category->cat_name.'" AND post_type="post"');
    					if ($category->category_parent == "51" || $category->category_parent == "38"){
    						echo '<li><a href="'.$guid.'">'.$category->cat_name.'</a></li>';
    					}
    				}
    
    			}
    
    	endforeach; ?>
    
    </div>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter David Gard

    (@duck_boy)

    Thanks, but I had already seen that and I cannot get it to work. It just deosn’t display anything.

    OK, I tried it and it has several problems, apparently caused when the code was pasted into the post. I modified it as follows:

    <?php function get_first_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches[1][0];
    //if(empty($first_img)){ //Defines a default image
    //   $first_img = '/images/default.jpg';
    //}
    return $first_img;
    }?>

    I wanted it to return an empty value, not a default image, so those lines are commented out.

    I called the function using this code:

    <?php $image = get_first_image();
     if (!empty ($image)) echo "<img src='$image' title='Image on post " . get_the_ID() . "' width='140' height='100' />"; ?>

    You will need to modify that to fit your own styling, maybe use a class.

    Thread Starter David Gard

    (@duck_boy)

    Thanks for that. Had a quick play and couldn’t get it to work (was always didplaying the default image no matter what), but it no longer matters. Due to the unique way in which my employers run their company there is no longer a need to do this, so sorry for wasting your time.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display image attached to post’ is closed to new replies.