Depending of what you are exactly trying to achieve accessing the first element of the $posts
array may work for you. No need for the foreach
loop.
echo '<img src="'.$posts[0]['img']['url'].'">';
Related Posts:
- Get the id of all images in a post
- Inject post (from specific category) between posts in Loop
- Permanently remove first image from posts
- Looping through WP_Post Object
- Using a javascript file to access a get posts array
- Styling images coming from another blog
- Must Use Plugin Causing Query Error
- How can I add multiple ‘tax_query’ arrays via a loop?
- Cannot display or echo alt text on featured image
- Recent posts with featured image or fallback image with permalink
- Best practice for migration friendly images in posts/pages?
- Reprinting tags with all attributes
- How can I add extra word in permalink when someone click download button?
- remove post that has no content
- Checking array against author id in loop
- Redirect to another page using contact form 7? [closed]
- How to loop through all the attached images in a post, and get their url one by one
- How to avoid duplicates when creating recent network posts
- What file have I to create in my custom WordPress theme to show all the post belonging to a specific category?
- How to get the last category name of a child category?
- WordPress query undefined offset in loop
- WordPress loop by meta key that is an array? and how loop multiple arrays
- Get all posts as an array ID => Name
- I’m unable to call img path using single quotes in an array?
- Do not duplicate posts with multiple categories in multiple loops
- how to make an array of post id’s in is_single
- array_rand not working correctly?
- How to pick “full/thumbnail” images in the loop?
- Update value of a associative array with update_post_meta
- Wp Query : Order by distance lat,lon
- Display articles with a different template in the home page | Solved |
- in_array not working on dev server but works on localhost
- Is there anyway I can call the year once?
- ACF – Get ID of relationship field in flexible content
- Show the subcategory name based on current product
- Get list of posts from attachment
- How do if all posts has this category ID then do this
- Make sticky post with FacetWP
- Get current_post in reverse order with pagination
- what’s the syntax for if the image exists get the image?
- How do I get the featured post to share on Social Media and show on Individual Post pages?
- Add div after every 4 posts then every 2 posts for a responsive loop
- Insert div after every three posts in home.php [duplicate]
- Retrieving specific images from Media Library
- WP_Post is not from correct array
- Create Customization Controls from Array
- Automatically create a loop for post ID
- Excerpt all post content Content Same Size without word cutting off
- How to create a loop that will display one post and stop?
- WooCommerce: write featured image dimensions to custom fields in product’
- How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
- Count the number of matching post names in foreach loop
- How to convert objects into arrays
- How to get 2 or multiple custom post types in wordpress functions.php
- “before delete post” action fire when the post is updated?
- IMG src weird behaviour inside a single post loop
- Convert all images to PNG on file upload
- multiple if statements [closed]
- Escape post image attachments added to template
- How to select WooCommerce products by post_meta and order them
- Parsing php string in jquery [closed]
- Displaying a button on each post
- Auto crop images in WordPress
- Trying to make php run in a post
- Hide post if matches current month and year
- Display post image with fancybox
- How can I add pagination and how can I change thumbnail size?
- Admin Options page. Save as Array
- Creating bulk posts with Youtube videos
- How to Display a Single Post Excerpt
- How to get media objects
- have_posts() execution failure
- PHP code to call image Caption, Alternative Text, and Decription?
- Echo URL of large version of Featured Image
- How does the ternary operator work in the wordpress loop post?
- Remove posts after a given amount of time
- PHP get the first post separately from array returned by wpdb->get_results()
- $wpdb->flush(); breaks the loop
- Working with query_posts ( arrays and query strings)
- How do I let users upload files to a chosen location?
- Automatically add custom CSS to new posts using a category template
- how can i show only the parents in owl-carousel?
- Change the URL of an image from wp_get_attachment_image_src except in the frontpage
- Get different images for mobile and desktop with php, advanced custom fields without using js and ajax
- Use object in template part
- Create shortcodes within foreach loop (using array)
- How Display Posts on category
- Hide wordpress field if data is empty in post!
- How to get specific image in media library with php
- How can I use wp_query to show all product data using just the products ID?
- hover image appears below placeholder instead of overlayed
- Query all posts of a custom taxonomy term
- Retrieving Author ID in wp-admin area
- Get first URL from post content
- Using a variable in is_page(array())
- Remove duplicates – array_unique()
- ACF page while loop breaks footer while loop
- Getting a specific post values to another div or modal
- Trying to add attribute to my posts’ featured image
- How to properly loop through these external URLs to get them into the sitemap using this hook