• I need the latest five image attachments from the specific pages to be shown on the frontpage and linked them to the page where they are attached to.

    I have this theme code where it should be easily changed, but after hours and hours of searching I still can’t figure how.

    <?php
    $categories = array(
      'page-slug-one',
      'page-slug-two',
      'page-slug-three',
      'etc'
    );
    
    $args = array(
      'post_type' => 'attachment',
      'numberposts' => -1,
      'post_parent' => null,
      'order_by' => 'post_date',
    );
    
    $images = get_posts($args);
    $limit = 0;
    
    if (!empty($images)) {
    	foreach ($images AS $image) {
    		if ($limit == 5) {
    			break;
    		}
    
    		$post = get_post($image->post_parent);
    			if (in_array($post->post_name, $categories)) {
    				$limit++;
    				$date = new DateTime($post->post_date);
    				$formatted_date = $date->format('M j, Y');
    				print("<li onclick='showPage(\"" . xx_language_code() . "/" . $post->post_name . "\")'>" . $post->post_name . " (" . $formatted_date . ")</li>");
    			}
    	}
    }
    else {
    	print('<li>' . __('There are no recent images.') . '</li>');
    }
    ?>

    Obviously I want to change $post->post_name with an image but how?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I adjusted your code above to get only attachments from the specified posts – you have to use post ID instead of slugs.

    <?php
    // IDs of posts
    $pages = array(
      189,
      2,
      12
    );
    
    // Get attachments that have parent from posts array, limit to 5.
    $args = array(
      'post_type' => 'attachment',
      'numberposts' => 5,
      'post_parent' => $pages,
      'order_by' => 'post_date'
    );
    
    $images = get_posts($args); 
    
    if ( !empty($images) ) {
        foreach ($images AS $image) { ?>
            <img src="<?php echo $image->guid; ?>" width="300"/><?php echo $image->post_date; ?><br/>
            <?php
        }
    }
    else {
        print('<li>' . __('There are no recent images.') . '</li>');
    }
    ?>
    Thread Starter KelminLumo

    (@kelminlumo)

    Thanks for the answer! This is beautiful! I love to see such understandable code instead of this I have to work here. However, unfortunately it didn’t make any change. For some reason, the first image comes from a post, which is in a category that is assigned to a page by a template, but the page or the post are not in the list. Actually the list doesn’t do anything. I could even remove that part from the code. Also one image is broken for some reason (will look into that later).

    The site has pages where new images are sometimes uploaded, which is why we want to show the recent on the frontpage. Then there are blog pages using own templates that display posts in those categories.

    I now realized that it would be easier to update the site if we just ignore only a few pages instead, meaning excluding their page ID’s I believe, not to list every single page and add a new into the list everytime such a page is created.

    If this can’t be done, maybe we stick with the page names then ($post->post_name).

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show the latest image attachments from specific pages’ is closed to new replies.