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
- Decrease file size upload in Media
- Replacing all attachment links in post with media file link
- PHP/Manual use of images? – 2 Questions
- How to create shortcodes that pull custom field data from general settings
- Insert code when users come from an specific referer
- the_title() & the_content() are retrieving Post details inside a Page, after calling a shortcode
- How to display related posts from parent category
- Get list of shortcodes from content
- Audio tags around Mp3 URL in content
- How do I get images (with a thumbnail preview) to show in search results?
- Wrap each shortcode in array to div
- How to migrate the posts from an old custom legacy blog to a new WordPress website?
- Easy way to delete 70k posts and attached media?
- Put php variable in shortcode
- is it possible to force wordpress to always save thumbnails as ‘jpg’ not ‘png’
- How do I add text in a shortcode?
- Template part inside shortcode, unexpected reult
- Shortcode Inside Class Not Working
- Shortcode API: How to get name that matched shortcode RegEx?
- Shortcode content filter?
- How can I use wp_query to show all product data using just the products ID?
- How do you use WP slugs for PDF files / media?
- How to display posts by current user in a drop down
- Get value from shortcode to do something
- Loop through categories and display posts title under each dropdown
- 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
- Custom shortcode outputs plain text instead of HTML at top of post
- Automatically attach image to post if they have the same title
- Shortcode returns values in the wrong order
- Embedding PHP in shortcode $content
- Shortcode with PHP issue “Undefined index”
- get_posts works in the page template but not in a shortcode
- Count posts on multisite with blog id
- Making (and edit existing) posts with ID included
- How can I include shortcodes within PHP?
- Parsing Menu Items and Blog Posts
- Syntax error in a shortcode function
- How to show an entire post content and not also the excerpt?
- Help with WordPress function inside a shortcode
- Short code template + ajax
- WordPress shortcode returns the data before
- Shortcode to embed Edit Account form not working
- Increment paged on WP_Query
- Modify main query before it run without pre_get_post
- .html end of URl affected to SEO?
- Update if post with specific title exists
- Shortcode for a Series of Elevating Image Files, Excluding a Range?
- Shortcode won’t execute
- AJAX function not working [closed]
- Can ‘Custom Field’ data be inserted into this Short Code?
- Add class to all img if they have a certain ACF field
- Set document title through shortcode plugin
- Advanced Query posts
- My Custom Post Type AJAX Query is Returning no posts – why?
- How do I reopen the (Popup Maker) after entering the correct password for a password protected page?
- Not able to remove caption shortcode from the content
- Inside Array – “unidentified index” error with “prepare was called incorrectly” despite not calling the prepared statement with wordpress [closed]
- Shortcodes not processing inside post content
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- wpdb->query returns different value to phpMyAdmin
- Don’t update modified post date when user add a product review or comment?
- Limit the number of characters/words in an excerpt for a related posts section
- I want to hide “sold by” on certain pages with id page 43
- How to add a PHP scripts into WordPress
- Closing Comments conditionally in comments.php
- Multiple Arrays for Custom Post Query using Boostrap accordions and ACF repeater fields
- Having trouble with customizing blog posts on the home page
- Replacing entire tag with shortcode – JavaScript [duplicate]
- Related Posts: if there is no category use tags
- How do if all posts has this category ID then do this
- Shortcode just with a php code
- Getting error in sql query
- Insert div after every three posts in home.php [duplicate]
- PHP tag in post content makes wordpress go crazy
- Unable to get Post Category Name and URL
- Custom fields changes the formatting of metabox input
- Dynamically Create Posts Via XML File
- insert thumbnail image from php script
- Contact form field in wordpress menu
- php string inside shortcode does not work
- Limit the number of successful logins
- Shortcodes (with a space) added to php Sample
- Trying to see if page is category or single and displaying title with appropriate heading tag
- How can I put a custom field as the link of a button shortcode?
- Problem using explode inside add_shortcode() callback funciton [closed]
- get value from post to sidebar
- Display Current Time using shortcode
- How to create shortcode of this PHP code
- Gutenberg – render_callback does not pass ToggleControl value on frontend
- HTTP 500 Error after Creating Shortcode
- How call WordPress shortcode inside function file
- How to execute a shortcode within a custom field?