Last active
January 18, 2023 20:29
-
-
Save brandonjp/c87a1f57be5578dc2f2300f4bab38d25 to your computer and use it in GitHub Desktop.
WP Post Edit: Add Metabox to List Attachments - WP / Code Snippets Pro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? // <- remove this line if you copy/paste into Code Snippets Pro | |
namespace cpZgLjBJ; | |
function add_metabox_of_post_attachments() | |
{ | |
add_meta_box('att_thumb_display', 'Attachments', '\cpZgLjBJ\generate_list_of_post_attachments', 'post'); // Change your post type here | |
} | |
function generate_list_of_post_attachments($post) | |
{ | |
// get_posts: https://developer.wordpress.org/reference/functions/get_posts/ | |
$args = array( | |
'post_type' => 'attachment', | |
'order' => 'ASC', | |
// 'post_mime_type' => 'image', // ignore this to get all, else use 'image', 'video', 'audio' | |
'numberposts' => -1, | |
'post_parent' => $post->ID | |
); | |
echo '<ul>'; | |
foreach (get_posts($args) as $key=>$image) { | |
$imgSRC = wp_get_attachment_url($image->ID); | |
echo '<li style="display: flex;align-items: center;">'.$key.': '.$image->post_mime_type.'<a href="' . $imgSRC . '" target="_blank" style="margin-left:1rem;display: flex;align-items: center;"><img style="max-width:150px;margin-right:1rem;" src="' . $imgSRC . '">' . $imgSRC . ' ↗</a> <a href="post.php?post='.$image->ID.'&action=edit" target="_blank" style="margin-left:1rem;">EDIT</a></li>'; | |
} | |
echo '</ul>'; | |
} | |
add_action('add_meta_boxes', '\cpZgLjBJ\add_metabox_of_post_attachments'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHP snippet for Code Snippets Pro
Adds a metabox to the post edit screen to show a list of attached media files.