{"id":196595,"date":"2015-08-21T00:44:08","date_gmt":"2015-08-20T15:44:08","guid":{"rendered":""},"modified":"2016-09-15T22:08:03","modified_gmt":"2016-09-15T13:08:03","slug":"%e3%83%a1%e3%83%87%e3%82%a3%e3%82%a2%e7%94%bb%e5%83%8f%e3%81%ae%e8%a1%a8%e7%a4%ba%e3%81%ab%e9%96%a2%e3%81%97%e3%81%a6foreach%e2%86%92for%e3%83%ab%e3%83%bc%e3%83%97%e3%81%b8%e5%a4%89%e6%9b%b4%e3%81%97","status":"closed","type":"topic","link":"https:\/\/ja.wordpress.org\/support\/topic\/%e3%83%a1%e3%83%87%e3%82%a3%e3%82%a2%e7%94%bb%e5%83%8f%e3%81%ae%e8%a1%a8%e7%a4%ba%e3%81%ab%e9%96%a2%e3%81%97%e3%81%a6foreach%e2%86%92for%e3%83%ab%e3%83%bc%e3%83%97%e3%81%b8%e5%a4%89%e6%9b%b4%e3%81%97\/","title":{"rendered":"\u30e1\u30c7\u30a3\u30a2\u753b\u50cf\u306e\u8868\u793a\u3092for\u30eb\u30fc\u30d7\u3067\u8868\u73fe\u3057\u305f\u3044"},"content":{"rendered":"<p>\u5236\u4f5c\u76ee\u6a19\u306f\u3001<br \/>\n\u30bf\u30af\u30bd\u30ce\u30df\u30fc\u3067\u30ab\u30c6\u30b4\u30ea\u5206\u3051\u3055\u308c\u305f\u30e1\u30c7\u30a3\u30a2\u306e\u6700\u65b0\u753b\u50cf\u3092<strong>\uff11\u679a<\/strong>\u8868\u793a\u3057\u3001<br \/>\n\u305d\u308c\u3089\u3092\u30ab\u30c6\u30b4\u30ea\u306e\u66f4\u65b0\u9806\u306b\u4e26\u3079\u66ff\u3048\u308b\u3053\u3068\u3067\u3059\u3002<br \/>\n\uff08\u53c2\u7167\uff1a<a href=\"http:\/\/www.h-fj.com\/blog\/archives\/2013\/06\/05-095031.php\">WP\u3067\u30ab\u30c6\u30b4\u30ea\u3092\u6295\u7a3f\u306e\u65b0\u3057\u3044\u9806\u306b\u4e26\u3079\u66ff\u3048\u308b<\/a>\uff09<\/p>\n<p>\u4e00\u822c\u7684\u306b\u30e1\u30c7\u30a3\u30a2\u753b\u50cf\u4e00\u89a7\u8868\u793a\u306a\u3069\u306b\u306fforeach\u30eb\u30fc\u30d7\u3092\u4f7f\u3046\u3088\u3046\u3067\u3059\u304c\u3001<br \/>\n\u4eca\u56de\u300c\u30ab\u30c6\u30b4\u30ea\u5225\u300d\u300c\u66f4\u65b0\u9806\u300d\u3068\u3044\u3046\u6761\u4ef6\u304c\u3042\u308b\u305f\u3081for\u30eb\u30fc\u30d7\u3067\u8868\u73fe\u3057\u305f\u3044\u3067\u3059\u3002<br \/>\n\u3057\u304b\u3057\u4e0b\u8a18\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3046\u307e\u304f\u3044\u304b\u305a\u3068\u3066\u3082\u56f0\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u554f\u984c\u70b9\u3092\u63a2\u308b\u305f\u3081\u3001\u4e00\u5ea6for\u30eb\u30fc\u30d7\u3092\u5916\u3057\u3066print_r\u3067\u3072\u3068\u3064\u305a\u3064\u8981\u7d20\u3092\u8abf\u3079\u305f\u6240<br \/>\n10\u884c\u76ee\u306eget_posts\u95a2\u6570\u3067\u5024\u3092\u307e\u3063\u305f\u304f\u53d6\u5f97\u3067\u304d\u3066\u3044\u306a\u3044\u3088\u3046\u306a\u306e\u3067\u3059\u3002<br \/>\n\u539f\u56e0\u304c\u3064\u304b\u3081\u305a\u3001\u4ed6\u306e\u95a2\u6570\u3092\u5f53\u3066\u306f\u3081\u3088\u3046\u306b\u3082\u308f\u304b\u3089\u306a\u3044\u72b6\u6cc1\u3067\u3059\u3002<br \/>\n\u3069\u3046\u304b\u89e3\u6c7a\u7b56\u3084\u30d2\u30f3\u30c8\u3060\u3051\u3067\u3082\u3054\u52a9\u529b\u3044\u305f\u3060\u3051\u307e\u305b\u3093\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<pre><code>&lt;!--function.php--&gt;\n&lt;?php  \/\/\u66f4\u65b0\u9806\u30fb\u30ab\u30c6\u30b4\u30ea\u30fc\u5225\u30a2\u30bf\u30c3\u30c1\u30e1\u30f3\u30c8\u8868\u793a\u65b9\u6cd5\nfunction get_taxs_and_sort() {\n    $taxs = get_terms( &#039;attachment_category&#039;, &#039;get=all&#039; );\t\/\/ \u7279\u5b9a\u306e\u30bf\u30af\u30bd\u30ce\u30df\u30fc\u306b\u3042\u308b\u30ab\u30c6\u30b4\u30ea\u3092\u3059\u3079\u3066\u8aad\u307f\u8fbc\u3080\n    $count = count($taxs);\t\t\t\t\t\/\/ \u30ab\u30c6\u30b4\u30ea\u306e\u6570\u3092\u5f97\u308b\n    for ($i = 0; $i &lt; $count; $i++) {\t\t\t\t\/\/ \u30ab\u30c6\u30b4\u30ea\u306e\u6570\u3060\u3051\u7e70\u308a\u8fd4\u3059\n\n        \/\/ \u5404\u30ab\u30c6\u30b4\u30ea\u306e\u6700\u3082\u65b0\u3057\u3044\u6295\u7a3f\u3092\u8aad\u307f\u8fbc\u3080\n        $where = array(&#039;attachment_category&#039; =&gt; $taxs[$i]-&gt;term_id, &#039;orderby&#039; =&gt; &#039;post_date&#039;,\n\t\t\t&#039;post_type&#039; =&gt; &#039;attachment&#039;, &#039;order&#039; =&gt; &#039;desc&#039;, &#039;numberposts&#039; =&gt; 1);\n        $newest_atms = get_posts($where); \/\/\u25a0\u3053\u3053\u304c\u6a5f\u80fd\u3057\u306a\u3044\u25a0\n\n        \/\/ \u30ab\u30c6\u30b4\u30ea\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u300cnewest_atm\u300d\u3068\u3044\u3046\u30e1\u30f3\u30d0\u3092\u8ffd\u52a0\u3057\u3066\u3001\u6700\u65b0\u306e\u6295\u7a3f\u3092\u4ee3\u5165\u3059\u308b\n        $taxs[$i]-&gt;newest_atm = $newest_atms[0];\n    }\n    \t\/\/ \u30ab\u30c6\u30b4\u30ea\u3092\u3001\u6700\u65b0\u306e\u6295\u7a3f\u306e\u65e5\u4ed8\u304c\u65b0\u3057\u3044\u9806\u306b\u4e26\u3079\u66ff\u3048\u308b\n    \tusort($taxs, &#039;sort_by_postdate&#039;);\n    \t\/\/ \u7d50\u679c\u3092\u8fd4\u3059\n    \treturn $taxs;\n}\n\nfunction sort_by_postdate($a, $b) {\n    \/\/ 2\u3064\u306e\u30ab\u30c6\u30b4\u30ea\u306e\u6700\u65b0\u306e\u6295\u7a3f(newest_atm\u30e1\u30f3\u30d0)\u306e\u65e5\u4ed8\u306e\u5927\u5c0f\u95a2\u4fc2\u3092\u8fd4\u3059\n    return ($a-&gt;newest_atm-&gt;post_date == $b-&gt;newest_atm-&gt;post_date) ? 0 :\n           ($a-&gt;newest_atm-&gt;post_date &lt; $b-&gt;newest_atm-&gt;post_date) ? 1 : -1;\n}\n?&gt;<\/code><\/pre>\n<p>\u5ff5\u306e\u305f\u3081\u3001\u30ab\u30b9\u30bf\u30e0\u56fa\u5b9a\u30da\u30fc\u30b8\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u30b3\u30fc\u30c9\u3082\u8f09\u305b\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre><code>&lt;?php\n$taxs = get_taxs_and_sort();\t\/\/ \u30ab\u30c6\u30b4\u30ea\u30fc\u3068\u5404\u30ab\u30c6\u30b4\u30ea\u30fc\u306e\u6700\u65b0\u6295\u7a3f\u3092\u53d6\u5f97\n$count = count($taxs);\t\t\/\/ \u30ab\u30c6\u30b4\u30ea\u306e\u6570\u3092\u5f97\u308b\nfor($i=0; $i&lt;$count; $i++) :\t\/\/ \u30ab\u30c6\u30b4\u30ea\u306e\u6570\u3060\u3051\u7e70\u308a\u8fd4\u3059\n?&gt;\n\n&lt;li class=&quot;photo-list&quot;&gt;\n\t&lt;?php \t\/\/\u6700\u65b0\u306e\u6295\u7a3f\u306b\u95a2\u3059\u308b\u60c5\u5831\u3092\u6e96\u5099\u3057\u3066\u304a\u304f\n\t \t$post = $taxs[$i]-&gt;newest_atm; setup_postdata($post);\n\t\t\/\/\u6700\u65b0\u306e\u6295\u7a3f\u753b\u50cf\u3092\u547c\u3073\u51fa\u305b\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\n\t\t$img = wp_get_attachment_image_src( $newest_atms[$i]-&gt;ID, &#039;&#039; );\n\t ?&gt;\n\n\t&lt;!--\u30e1\u30a4\u30f3\u30b3\u30f3\u30c6\u30f3\u30c4--&gt;\n\t&lt;?php echo $taxs[$i]-&gt;name ?&gt;(&lt;?php echo get_the_date(); ?&gt;)&lt;br \/&gt;\n\t&lt;?php the_title(); ?&gt;(&lt;?php echo get_the_date(); ?&gt;)&lt;br \/&gt;\n\t&lt;?php \techo &#039;&lt;a href=&quot;&#039; . $img[0] . &#039;&quot;&gt;&#039;;\n\t\techo wp_get_attachment_image( $newest_posts[0]-&gt;ID, &#039;thumbnail&#039; );;\n\t\techo &#039;&lt;\/a&gt;&#039;;\n\t ?&gt;\n&lt;\/li&gt;\n\n&lt;?php endfor; ?&gt;<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-196595","topic","type-topic","status-closed","hentry"],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ja.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/196595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ja.wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/ja.wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/ja.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/196595\/revisions"}],"wp:attachment":[{"href":"https:\/\/ja.wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=196595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}