{"id":5184,"date":"2017-08-06T14:02:41","date_gmt":"2017-08-06T18:02:41","guid":{"rendered":"https:\/\/themespress.ca\/?p=5184"},"modified":"2022-10-01T15:05:34","modified_gmt":"2022-10-01T19:05:34","slug":"display-number-images-linked-to-post-id","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/display-number-images-linked-to-post-id\/","title":{"rendered":"Display the Number of Images Attached to Post IDs"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>In this tutorial, we will create a few lines of code that will allow you to display the number of images attached to a Post ID with the help of a WordPress query.<\/p>\n<p>First, in our query, we will need the <strong>$attachments<\/strong> variable which contains <a title=\"get_children\" href=\"https:\/\/codex.wordpress.org\/Function_Reference\/get_children\" target=\"_blank\" rel=\"noopener\">get_children<\/a> of all images linked to Post or Post Type. And then we create a new <strong>$count<\/strong> variable that will count the images.<\/p>\n<p>With a foreach, we will display Post ID&#8217;s number of images as long as the query will find Post Ids.<\/p>\n<p>Here&#8217;s the code to use with your Post or Post Type query.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-4979188\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-4979188\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">global $post;\n$attachments = get_children ( \n   array( \n   &#039;post_parent&#039;      =&gt; $post-&gt;post_parent, \n   &#039;post_status&#039;      =&gt; &#039;inherit&#039;, \n   &#039;post_type&#039;        =&gt; &#039;attachment&#039;, \n   &#039;post_mime_type&#039;   =&gt; &#039;image&#039;, \n   &#039;order&#039;            =&gt; &#039;ASC&#039;, \n   &#039;orderby&#039;          =&gt; &#039;menu_order ID&#039; ) \n);\n \n$count = count( $attachments );\n$specific = array();\n$i = 1;\n \nforeach ( $attachments as $attachment ) {\n    $specific[$attachment-&gt;ID] = $i;\n    ++$i;\n}<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>We display the results of the loop the number of images attached to the selected Post ID:<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-2160069\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-2160069\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">echo $count;<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"In this tutorial, we will create a few lines of code that will allow you to display the number of images attached to a Post ID with the help of<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/display-number-images-linked-to-post-id\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":5187,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[318],"class_list":["post-5184","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-functions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=5184"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5184\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/5187"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=5184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=5184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=5184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}