{"id":2787,"date":"2011-05-07T11:14:16","date_gmt":"2011-05-07T03:14:16","guid":{"rendered":"http:\/\/www.rockfu.net\/blog\/?p=2787"},"modified":"2011-05-19T22:50:11","modified_gmt":"2011-05-19T14:50:11","slug":"wordpress-attachment-images","status":"publish","type":"post","link":"https:\/\/www.rockfu.net\/blog\/2011\/05\/website\/code\/wordpress-attachment-images\/","title":{"rendered":"wordpress attachment images"},"content":{"rendered":"<p>\u6700\u8fd1\u505a\u4e3b\u984c\u6642\u8981\u7528\u5230wordpress \u9644\u4ef6\u5716\u7247\uff0c\u5728\u770b\u6578\u64da\u5eab\u6642\u767c\u73fe\uff0c\u539f\u4f86\u6bcf\u4e00\u500b\u9644\u4ef6\u90fd\u662f\u4f5c\u70ba\u4e00\u500bpost\u63d2\u5165\u6578\u64da\u5eab\uff0c\u53ea\u662fpost-type\u4e0d\u540c\uff0c\u800c\u4e14\u9084\u6709\u662f\u5c6c\u65bc\u54ea\u500bpost\u3002\u5728\u6587\u7ae0\u4e2d\u4e5f\u53ef\u4ee5\u76f4\u63a5\u63d2\u5165gallery\u3002<\/p><div class=\"rockf-content\" id=\"rockf-1004069723\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-5237544886808594\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:block;\" data-ad-client=\"ca-pub-5237544886808594\" \ndata-ad-slot=\"6353985797\" \ndata-ad-format=\"auto\"><\/ins>\n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div>\n<p>\u770b\u5230\u9019\u4e9b\u529f\u80fd\uff0c\u5fc3\u52d5\u4e86\uff0c\u53ea\u662f\u9019\u4e9b\u81ea\u5e36\u7684\u529f\u80fd\u7f3a\u4e4f\u7684\u6771\u897f\u5c31\u662f\u9644\u4ef6\u5716\u7247\u7684\u63cf\u8ff0\u3002<\/p>\n<p>\u6839\u64da\u8aaa\u660e\uff0c\u53d6\u5f97\u9644\u4ef6\u7684function \u662fwp_get_attachment_image($id,size)\uff0c\u9019\u6a23\u52a0\u4e00\u500bforeach\u5c31\u80fd\u53d6\u5f97\u5230post\u88e1\u9762\u7684\u6240\u6709\u9644\u4ef6\u5716\u7247\uff0c\u4e0d\u904e\u5c31\u662f\u7f3a\u5c11\u4ecb\u7d39\u3002<\/p>\n<p>\u7528google\u627e\u5230\u4e86\u4e00\u7bc7\u4e0d\u932f\u7684\u6587\u4ef6\uff0c\u5beb\u4e86\u4e00\u500bhack\uff0c\u53ef\u4ee5\u53d6\u5f97\u9644\u4ef6\u5716\u7247\u7684title, description<\/p>\n<p>[php]<br \/>\n\/**<br \/>\n* Retrieves the attachment data such as Title, Caption, Alt Text, Description<br \/>\n* @param int $post_id the ID of the Post, Page, or Custom Post Type<br \/>\n* @param String $size The desired image size, e.g. thumbnail, medium, large, full, or a custom size<br \/>\n* @return stdClass If there is only one result, this method returns a generic<br \/>\n* stdClass object representing each of the image&#8217;s properties, and an array if otherwise.<br \/>\n*\/<br \/>\nfunction getImageAttachmentData( $post_id, $size = &#8216;thumbnail&#8217;, $count = 1 )<br \/>\n{<br \/>\n$objMeta = array();<br \/>\n$meta;\/\/ (stdClass)<br \/>\n$args = array(<br \/>\n&#8216;numberposts&#8217; =$count,<br \/>\n&#8216;post_parent&#8217; =$post_id,<br \/>\n&#8216;post_type&#8217; = &#8216;attachment&#8217;,<br \/>\n&#8216;nopaging&#8217; =false,<br \/>\n&#8216;post_mime_type&#8217; =&#8217;image&#8217;,<br \/>\n&#8216;order&#8217; = &#8216;ASC&#8217;, \/\/ change this to reverse the order<br \/>\n&#8216;orderby&#8217; = &#8216;menu_order ID&#8217;, \/\/ select which type of sorting<br \/>\n&#8216;post_status&#8217; = &#8216;any&#8217;<br \/>\n);<\/p>\n<p>$attachments = get_children($args);<\/p>\n<p>if( $attachments )<br \/>\n{<br \/>\nforeach( $attachments as $attachment )<br \/>\n{<br \/>\n$meta = new stdClass();<br \/>\n$meta-ID = $attachment-ID;<br \/>\n$meta-title = $attachment-post_title;<br \/>\n$meta-caption = $attachment-post_excerpt;<br \/>\n$meta-description = $attachment-post_content;<br \/>\n$meta-alt = get_post_meta($attachment-ID, &#8216;_wp_attachment_image_alt&#8217;, true);<\/p>\n<p>\/\/ Image properties<br \/>\n$props = wp_get_attachment_image_src( $attachment-ID, $size, false );<\/p>\n<p>$meta-properties[&#8216;url&#8217;] = $props[0];<br \/>\n$meta-properties[&#8216;width&#8217;] = $props[1];<br \/>\n$meta-properties[&#8216;height&#8217;] = $props[2];<\/p>\n<p>$objMeta[] = $meta;<br \/>\n}<\/p>\n<p>return ( count( $attachments ) == 1 ) ? $meta : $objMeta;<br \/>\n}<br \/>\n}<\/p>\n<p>[\/php]<\/p>\n<p>\u5982\u4f55\u4f7f\u7528\uff1a<\/p>\n<p>[php]<br \/>\ngetImageAttachmentData( $_posts-ID, &#8216;full&#8217; );<br \/>\n[\/php]<\/p>\n<p>\u53c3\u8003\u4f86\u6e90\uff1a<a href=\"http:\/\/www.farfromfearless.com\/\">http:\/\/www.farfromfearless.com\/<\/a><\/p>\n<p><a href=\"http:\/\/www.newvibes.com\/blog\/wordpress-get-attachment-title-and-description\/\">http:\/\/www.newvibes.com\/blog\/wordpress-get-attachment-title-and-description\/<\/a><\/p>\n<div class=\"rockf-after\" id=\"rockf-3022708627\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-5237544886808594\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:block;\" data-ad-client=\"ca-pub-5237544886808594\" \ndata-ad-slot=\"6353985797\" \ndata-ad-format=\"auto\"><\/ins>\n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u505a\u4e3b\u984c\u6642\u8981\u7528\u5230wordpress \u9644\u4ef6\u5716\u7247\uff0c\u5728\u770b\u6578\u64da\u5eab\u6642\u767c\u73fe\uff0c\u539f\u4f86\u6bcf\u4e00\u500b\u9644\u4ef6\u90fd\u662f\u4f5c\u70ba\u4e00\u500bpost\u63d2\u5165\u6578\u64da\u5eab &hellip; <a href=\"https:\/\/www.rockfu.net\/blog\/2011\/05\/website\/code\/wordpress-attachment-images\/\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80<span class=\"screen-reader-text\"> &#8220;wordpress attachment images&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[89,139],"tags":[],"class_list":["post-2787","post","type-post","status-publish","format-standard","hentry","category-code","category-wordpress"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/prNEE-IX","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.rockfu.net\/blog\/wp-json\/wp\/v2\/posts\/2787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rockfu.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rockfu.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rockfu.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rockfu.net\/blog\/wp-json\/wp\/v2\/comments?post=2787"}],"version-history":[{"count":7,"href":"https:\/\/www.rockfu.net\/blog\/wp-json\/wp\/v2\/posts\/2787\/revisions"}],"predecessor-version":[{"id":2793,"href":"https:\/\/www.rockfu.net\/blog\/wp-json\/wp\/v2\/posts\/2787\/revisions\/2793"}],"wp:attachment":[{"href":"https:\/\/www.rockfu.net\/blog\/wp-json\/wp\/v2\/media?parent=2787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rockfu.net\/blog\/wp-json\/wp\/v2\/categories?post=2787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rockfu.net\/blog\/wp-json\/wp\/v2\/tags?post=2787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}