• <?php
    
    $imgsrcurl = '';
    $sql = "SELECT ID, guid, post_type FROM $tableposts WHERE post_parent = " . $post->ID;
    $imgsrc = $wpdb->get_results($sql);
    
    foreach ($imgsrc as $srcurl) {
    	$imgsrcurl = $srcurl->guid;
    	}
    
    if (!$imgsrcurl) {
    	$imgsrcurl = "https://media.speakeasyvintagemusic.com/images/noalbumcover.gif";
    }else{
    	$pieces = explode(".",$imgsrcurl);
    	$imgsrcurl = $pieces[0] . "." . $pieces[1] . "." . $pieces[2] . ".thumbnail." . $pieces[3];
    	}
    
    echo '<img src="' . $imgsrcurl . '" alt="Album Cover Image" class="coverthumb reflect" />';
    ?>

    The above code is in my “archive.php” file, it pulls the first uploaded file for post as it’s outputting it’s excerpt.

    The idea is to get an image that was uploaded while editing any given post, and then to place that image on the page next to the text for that post.

    The question is, do I really have to do this, or is there some WP command I could have used? (I looked but I didn’t see anything).

    Also, it is bad to access the database this way? (excessive bandwidth, et. al.)

  • The topic ‘Hows this code look?’ is closed to new replies.