• Resolved dumboxp

    (@dumboxp)


    Dear Greg,

    I am also using the “Force Feature Image” snippet and tried to make sure the first image in the gallery is used as featured-image by using the snipped you provided in this post.

    Unfortunately it only uses the first (or last) UPLOADED image, but not the image manually moved to the 1st place of the gallery! … which is very different!

    Intuitive would be:
    1. automatically use the image from the 1st place in the gallery as featured-image (ignoring the order of the upload)
    2. therefore the first place in the gallery is special and should be highlighted

    Any other solution is not unintuitive to lay users because they do not understand what is happening and how to open the edit-popup to mark one image to become the featured-image (which is nice for maintainers/admins).

    I tried to debug and modify function force_featured_image( $post_id ) {} but it seems that get_children( array( 'post_parent' => $post_id )) does not contain the required information about the order/position of the images in the gallery.

    Do you have any idea, how to get the 1st image from the (possibly manually) re-ordered gallery to make it the featured-image?

    thanks!

    ps: i very much vote for adding this directly into wpadverts ??

    • This topic was modified 5 years, 1 month ago by dumboxp.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    the order of the images is stored in _adverts_attachments_order meta, you can get it from there like this

    
    $keys = get_post_meta( $post_id, '_adverts_attachments_order', true );
    $first_image_id = $keys[0];
    update_post_meta( $post_id, '_thumbnail_id', $first_image_id );
    return 1;
    

    you wouldn’t then need the whole

    
    $children = get_children( array( 'post_parent' => $post_id ) );
    foreach( $children as $child ) {
        update_post_meta( $post_id, '_thumbnail_id', $child->ID );
        return 1;
    }
    

    Some of the snippets will be moved to the WPAdverts as options, the force featured image feature should be one of them, but right now i cannot tell how exactly it will work, but it seems that using the first image on the list after reordering will be the best way to do that.

    Thread Starter dumboxp

    (@dumboxp)

    Thanks Greg for your quick reply!

    The solution using _adverts_attachments_order is great! Only json_decode() was missing, to get the image-id from the serialized string in post-meta.

    Here the working function to set the 1st image in the gallery as featured-image:

    
    /**
     * Sets 1st image in gallery as featured image for $post_id
     * @param int $post_id  ID of a post for which we wish to force featured image
     * @return int          1 if success less or equal to 0 on failure
     */
    function force_featured_image( $post_id ) {
      if( $post_id < 1 ) {
        return -1; // No images uploaded
      } else if( $post_id > 0 && get_post_thumbnail_id( $post_id ) ) {
        return -2; // Has main image selected
      } 
      
      $attachment_order = get_post_meta( $post_id, '_adverts_attachments_order', true );
      $keys = json_decode($attachment_order); // Deserialize array of image ids
      $first_image_id = $keys[0];
      update_post_meta( $post_id, '_thumbnail_id', $first_image_id ); 
      return 1; // Set featured image
    }
    

    By adding the following Custom CSS, the 1st image in the gallery is highlighted by a 2px black border.

    
    /* Highlight 1st image in adverts gallery */
    .adverts-gallery-upload-item:nth-of-type(1) {
      border: 2px solid black;
    }
    

    Looking forward to all the upcoming versions of WPAdverts!
    br

    • This reply was modified 5 years, 1 month ago by dumboxp.
    • This reply was modified 5 years, 1 month ago by dumboxp.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Forcing 1st Image in Gallery as Featured Image’ is closed to new replies.