The problem with your code, as you mentioned, is that you are outputting the images via your print_r
function, and you need to instead build a string to return, which is displayed in place of the shortcode.
Give this a try:
function get_media () {
$attachments = get_posts([
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => get_the_ID()
]);
if ($attachments) {
foreach ($attachments as $attachment) {
$output .= wp_get_attachment_image($attachment->ID);
}
}
return $output;
}
add_shortcode( 'media', 'get_media' ) ;
Related Posts:
- Get posts associated with media attachment
- Disable Attachment Pages Completely
- Upload images and attachments from frontend form
- $wpdb returns duplicate posts
- Time Stamp In A WordPress Post Title That Does Not Keep Refreshing
- Attach and retrive multiple pdf files to post or page
- Get list of posts from attachment
- How to get media objects
- Remove posts after a given amount of time
- Shortcode content filter?
- Automatically add custom CSS to new posts using a category template
- Create shortcodes within foreach loop (using array)
- How Display Posts on category
- Hide wordpress field if data is empty in post!
- How can I use wp_query to show all product data using just the products ID?
- Query all posts of a custom taxonomy term
- Retrieving Author ID in wp-admin area
- Custom Theme, Custom shortcode not working
- Trigger popup in a php if/else statement
- Getting a specific post values to another div or modal
- Append HTML Using Shortcode
- Trying to add attribute to my posts’ featured image
- How do you use WP slugs for PDF files / media?
- Why is file_get_contents returning page source?
- Plugin CSS not enqueing
- Displaying the last post on static homepage
- Can’t get wp_insert_post to work
- Why not yield/iterable in posts interface?
- WordPress Search Results for Multiple Post Types
- Different loop output every x posts in custom post type
- Best practice for migration friendly images in posts/pages?
- How to output a PHP file values by shortcode?
- Showing content from one page on another
- How to show part of the_content?
- Showing a different gallery in a seperate post
- How to display posts by current user in a drop down
- Create own WordPress shortcode gallery
- Unreadable pagination
- 200 Rewrite blog post links
- How to set a min number of words for a blog post
- Get value from shortcode to do something
- Loop through categories and display posts title under each dropdown
- Get post id in a function when edit/add a post
- How to exclude category ID from Looper in WordPress
- Add a specific part of current category page url to shortcode
- How to display data from custom table in wordpress phpmyadmin
- Calculating a large number of MYSQL queries as customshort codes, slow
- how do I get a specific post from a post with a subcategory in WP
- Automatically create child pages and grandchild pages when saving a (parent) page
- wp_trim_words() does not work with my code Am I doing any mistake in my code?
- Second transition_post_status hook fired instead of the first
- WordPress Shortcodes.. printf is outputting a random number… Can’t figure out WHY?
- Colon is Missing In My Website Url in WordPress
- How to store post ID’s in cookie or session to display the same posts later
- Image rotation and editing stopped working in WordPress after upgrading Ubuntu from 16.04 to 18.04
- Which hook/action will help me solve my problem?
- How can I add extra word in permalink when someone click download button?
- Send multiple attachments with wp_mail in PHP
- Custom shortcode outputs plain text instead of HTML at top of post
- How to use wp_add_inline_style for custom CSS added via shortcode?
- Post + form + action + results on the same page
- Pass php dynamic variable to shortcode
- How can I dynamically update the class in my shortcode?
- Open post-content in archive page in a Modal box with bootstrap
- Creating own shortcode – echoeing php variable based on JS variable
- What is the alternative to “ when it comes to calling Media (image) files in the ‘attachment.php’ file?
- remove post that has no content
- How create a Shortcode with hover and complex options
- Remove from shortcode
- Displaying recent posts on static page with template-part via shortcode
- wordpress 4.4 self hosted video (html5) doesn’t show
- Attach previously uploaded image to post – current code has unexpected results
- Loading index.php contents which located outside blog folder for post single page
- do_shortcode with custom field
- get post content of particular post by url
- Automatically attach image to post if they have the same title
- Post repeating with infinite scroll
- How do I get around “Sorry, this file type is not permitted for security reasons”?
- How to Reference/Echo Variable from Another PHP Function
- Remove echo from shortcode
- wp_mail file attachment not being placed in upload folder?
- Replace shortcode in substring
- Custom posts in different columns style
- Get children post mime type using parent post_ID wp_post
- How to show only subcategories in parent category not parent category?
- 404 error when I acess the second page of post-type
- Shortcode to show the code
- Shortcode returns values in the wrong order
- Redirect to another page using contact form 7? [closed]
- WordPress automatically adds links to uploaded images
- Get URL from shortcode tag
- Embedding PHP in shortcode $content
- Shortcode with PHP issue “Undefined index”
- Which PHP page is the Default Posts Page
- Excecuting php function in shortcode
- Only first shortcode gets executed
- Multiple do_shortcode in page template
- retrieve post data and insert it in seperate static html page without WordPress
- WP Insert Post and then go to post
- Why am I getting a different filename? And how does WordPress load singular.php for both Page & Post? (Fresh WordPress installation)