{"id":32011,"date":"2024-11-25T11:42:49","date_gmt":"2024-11-25T03:42:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32011\/"},"modified":"2024-11-25T11:42:49","modified_gmt":"2024-11-25T03:42:49","slug":"wordpress%e5%89%8d%e7%ab%af%e5%9b%be%e7%89%87%e6%80%8e%e4%b9%88%e4%b8%8a%e4%bc%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32011\/","title":{"rendered":"wordpress\u524d\u7aef\u56fe\u7247\u600e\u4e48\u4e0a\u4f20"},"content":{"rendered":"<p>\/&#8221; target=&#8221;_blank&#8221;&gt;<strong>wordpress\u6559\u7a0b<\/strong>\u680f\u76ee\u7ed9\u5927\u5bb6\u4ecb\u7ecdwordpress\u524d\u7aef\u56fe\u7247\u600e\u4e48\u4e0a\u4f20\u3002<\/p>\n<p><strong>wordpress\u524d\u7aef\u56fe\u7247\u4e0a\u4f20<\/strong> <\/p>\n<p>\u6700\u8fd1\u7814\u7a76\u4e2a\u9879\u76ee\u9700\u8981\u5728wordpress\u524d\u7aef\u4e0a\u4f20\u7528\u6237\u5934\u50cf,\u5728\u7f51\u4e0a\u67e5\u4e86\u4e9b\u8d44\u6599\uff01\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\uff01<br \/>1:\u9996\u5148\u5c31\u662f\u5728\u9700\u8981\u7684\u5730\u65b9\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u6846\u4e86<\/p>\n<pre>&lt;form action=\"\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\n    &lt;input type=\"file\" id=\"file\" name=\"files[]\" multiple=\"multiple\" accept=\"image\/*\" \/&gt;\n  &lt;input type=\"submit\" name=\"submit\" value=\"Upload!\" \/&gt;\n&lt;\/form&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2: \u5bf9\u56fe\u7247\u8fdb\u884c\u5904\u7406<\/p>\n<pre>$post=get_post(13);\/\/\u6d4b\u8bd5\u7528\n\nif ( $_FILES ) {\n\n    $files = $_FILES['files'];\n     $count= count($files['name']);\n\n    foreach ($files['name'] as $key =&gt; $value) {\n        if ($files['name'][$key]) {\n            $file = array(\n                'name'     =&gt; $files['name'][$key],\n                'type'     =&gt; $files['type'][$key],\n                'tmp_name' =&gt; $files['tmp_name'][$key],\n                'error'    =&gt; $files['error'][$key],\n                'size'     =&gt; $files['size'][$key]\n            );\n\n            $_FILES = array(\"files\" =&gt; $file);\n\n            foreach ($_FILES as $file =&gt; $array) {\n\n                $newupload = insert_attachment($file,$post-&gt;ID);\/\/\u6b64\u65b9\u6cd5\u5c06\u6587\u7ae0\u9644\u52a0\u5230ID\u4e3a13\u7684\u6587\u7ae0\u4e2d\u3002\u5982\u679c\u4e0d\u60f3\u63d2\u5165\u5230\u6587\u7ae0\u53ef\u4ee5\u4e3a\u7a7a\"\"\n} } } }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>3:\u5728functions.php\u6587\u4ef6\u6dfb\u52a0\u529f\u80fd\u51fd\u6570<\/p>\n<pre>insert_attachment\u8be5\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5982\u679c\u4e3a\u7a7a\u5c06\u4e0d\u9644\u52a0\u5230\u6587\u7ae0\u4e2d\u56fe\u7247\u3002<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>function insert_attachment($file_handler,$post_id,$setthumb='false') {\n global $wpdb;\n  \/\/ check to make sure its a successful upload\n  if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();\n\n  require_once(ABSPATH . \"wp-admin\" . '\/includes\/image.php');\n  require_once(ABSPATH . \"wp-admin\" . '\/includes\/file.php');\n  require_once(ABSPATH . \"wp-admin\" . '\/includes\/media.php');\n\n  $attach_id = media_handle_upload( $file_handler, $post_id );\n\n\n$image_url = wp_get_attachment_image_src(  $attach_id,'full' ); \nif ($setthumb){ \n\n  $wpdb-&gt;insert(\n  $wpdb-&gt;prefix . 'postmeta', array(\n                'post_id' =&gt; $post_id,\n                'meta_key' =&gt; 'wpcf-vi-img',\n                'meta_value' =&gt; $image_url[0] ));\n\n\n\n  }\n  return $attach_id;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>4:\u5f15\u7528\u65b9\u6cd5<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>$image_url = wp_get_attachment_image_src(  $attach_id,'full' );\/\/\u7531\u4e8e\u9875\u9762\u5237\u65b0\u7684\u95ee\u9898\u76f4\u63a5\u5728\u9875\u9762\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u662f\u4e0d\u751f\u6548\u7684\uff01\u9700\u8981\u5728\u51fd\u6570\u4e2d\u6784\u9020\u6b64\u65b9\u6cd5\u7684\u529f\u80fd\u3002\n\n\/\/\u5faa\u73af\u6587\u7ae0\u4e2d\u7684\u7279\u5f81\u56fe\u7247\u7684\u65b9\u6cd5\uff0c\u5982\u679c\u5c06\u56fe\u7247\u9644\u52a0\u5230\u6587\u7ae0\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u6279\u91cf\u8f93\u51fa\uff01\n$imagess=get_post_meta(13,'wpcf-vi-img',false);\nforeach($imagess as $images){\n echo  $images;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u524d\u7aef\u56fe\u7247\u600e\u4e48\u4e0a\u4f20\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/&#8221; target=&#8221;_blank&#8221;&gt;wordpress\u6559\u7a0b\u680f\u76ee\u7ed9\u5927\u5bb6\u4ecb\u7ecdwordpress\u524d\u7aef\u56fe\u7247\u600e\u4e48\u4e0a\u4f20\u3002 wordpress\u524d\u7aef\u56fe\u7247\u4e0a\u4f20 \u6700\u8fd1\u7814\u7a76\u4e2a\u9879\u76ee\u9700\u8981\u5728wordpress\u524d\u7aef\u4e0a\u4f20\u7528\u6237\u5934\u50cf,\u5728\u7f51\u4e0a\u67e5\u4e86\u4e9b\u8d44\u6599\uff01\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\uff011:\u9996\u5148\u5c31\u662f\u5728\u9700\u8981\u7684\u5730\u65b9\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u6846\u4e86 &lt;form action=&#8221;&#8221; method=&#8221;post&#8221; enctype=&#8221;multipart\/form-data&#8221;&gt; &lt;input type=&#8221;file&#8221; id=&#8221;file&#8221; name=&#8221;files[]&#8221; multiple=&#8221;multiple&#8221; accept=&#8221;image\/*&#8221; \/&gt; &lt;input type=&#8221;submit&#8221; name=&#8221;submit&#8221; value=&#8221;Upload!&#8221; \/&gt; &lt;\/form&gt; \u767b\u5f55\u540e\u590d\u5236 2: \u5bf9\u56fe\u7247\u8fdb\u884c\u5904\u7406 $post=get_post(13);\/\/\u6d4b\u8bd5\u7528 if ( $_FILES ) { $files = $_FILES[&#8216;files&#8217;]; $count= count($files[&#8216;name&#8217;]); foreach ($files[&#8216;name&#8217;] as $key =&gt; $value) { if ($files[&#8216;name&#8217;][$key]) { $file = array( &#8216;name&#8217; =&gt; $files[&#8216;name&#8217;][$key], &#8216;type&#8217; =&gt; $files[&#8216;type&#8217;][$key], &#8216;tmp_name&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32011","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32011"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32011\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}