Use following code to do the work :
global $post;
$page_id = $post->ID;
$all_attachments = get_posts( array( 'post_type' => 'attachment','post_mime_type' => 'image', 'post_parent' => $page_id, 'post_status'=>'published', 'numberposts' => -1 ) );
$no_of_photos = count($all_attachments); // Gets the number of photo's attached in $no_of_photos variable
$first_img_URL = get_permalink($all_attachments[0]->ID); // Gets the URL of first attachment in $first_img_URL variable
In the above code just replace the $page_id to the id of page/post to which the photos are attached.
Related Posts:
- remove links from images using functions.php
- How to add classes to images based on their categories?
- Auto delete WordPress images/thumbnails (all sizes) and featured after X days/hours, or similar?
- How to add a rel attribute to images that contains their categories?
- Best way to programatically add “rel” attributes to page and post images
- What is the “icon” parameter in wp_get_attachment_image_src used for?
- Find first image on paginated post for Pinterest
- How do I permanently Disable Attachment Post URL
- Can’t check if a post has thumbnail adding filter to get_post_metadata()
- Image rotation fails to regenerate custom sized thumbnail
- Display images that are not in the content
- Can’t filter wp_get_attachment_link
- Get ID of first image attached to a post
- How to stop wordpress from injecting hard-coded style into image attachments with captions
- How to hide image-url if no attachment?
- attachment page template? only show attachments for current post?
- How to get an attachment id from a filename
- What are the meta fields for an attachment?
- Remove attachment page for audio media type only
- Get Attachment Category Name
- Different image using srcset function
- Auto delete content in specific folder inside media library
- WP_Query for Attachments not working as expected
- How to update an image attachment’s alt text from a custom field when saving a post?
- Remove all video attachment, both files and post/postmeta from database?
- Open image size links in a page instead of direct image link
- Set thumbnail from URL, by grabbing image in functions.php
- Disable Attachment Page Except for Category
- How to check if a user exists by a given id
- Redeclare a function in a child theme
- How can I programmatically create “child” pages on theme activation?
- Add admin bar link to edit author
- How to include one class/instance without using global variables
- Modify search function in WordPress (TwentyTwelve)
- Show/hide Widgets in Dashboard Based on Current Advanced Custom Fields Option
- syntax for remove_filter in parent theme with class
- How to Acheive the custom woocommerce category template
- Can I remove the Rich Text box editor for a specific post?
- How to add CSS style sheet dynamically in wordpress
- How to update BuddyPress xprofile fields programmatically? [closed]
- Replace Archive Widget Link Text
- A snippet after every image
- Programmatically set page template based on page ID
- How can I add an image field to BuddyPress Extended Profile Fields? [closed]
- wp_remote_post with ssl:// protocol
- How to include local menu based on page id?
- How to add custom fields to my custom registration form
- How can I change the do_action function in footer (copyright area)
- How to display post content instead of excerpt
- Override a theme function in a child theme?
- changing behaviour of get_search_form
- Which action does wp_update_user triggers?
- Include default functions and methods
- Dynamic dropdown from database table
- Functions in custom theme
- Please Explain the Importing Process of Multiple Stylesheets for Child Themes
- check if author has published posts in custom post type, then send mail
- paginate_links() don’t properly work in search.php?
- What’s wrong with my use of plugin_url()?
- How can I modify the element on all pages?
- AMP – Change rel=”canonical” from functions.php [closed]
- echo custom fields with AJAX
- Function filter breaking tag archive menus
- How to create a functions.php in child theme? [closed]
- How to echo the value of an array element using a function via a shortcode
- Convert multiple logos into the_custom_logo
- I can’t add CSS with functions.php
- Register_Sidebar overwriting itself and doesn’t exist in global $wp_registered_sidebars;
- widgets not working
- search form leads to 404
- wp_get_archives() output
- How would I make a function to dynamically determine if user is logged to change navigation menu text?
- How to redirect Subscribers on login to specific page, when logging in from a Page
- how can I add filter in specfic field in my website?
- How to change images url in function.php?
- Which filter fires upon setting a featured image
- Probleme shortcode with list author
- Stylesheet does not load despite functions.php
- How to customize archive widget only for cpt?
- How to show only specific category post by user role without plugin and restrict all other cats
- Shortcode to output category description by passing ID
- some profile informations like youtube link doesnt seem
- Unable to declare AOS library in functions
- Removing sanitize_title_with_dashes Function with The Real Title
- Printing medium sized image
- Filter posts on new page
- Sticky – Featured Image
- WordPress Jquery+scripts enqueue issue
- Adding a jQuery rotator function
- How to pass a variable from a template page to the child theme functions.php
- How to programmatically GET (to know) which Sidebar (or) which Menu is being used on Different Pages?
- Settings in functions.php used by a plugin
- Can’t change excerpt length and more tag
- Echo a div to header.php from functions.php
- Making an under maintenance page (without using plugins)
- Image width issue in IE [closed]
- Changed functions.php file and now site is blank [closed]
- Load script only on selected Pages
- Convert Image to Webp on upload without plugin
- How to automatically load Google Fonts on pages only as they’re used?