{"id":16833,"date":"2024-04-20T22:48:14","date_gmt":"2024-04-20T14:48:14","guid":{"rendered":"http:\/\/www.slof.top\/?p=16833"},"modified":"2024-04-20T22:48:16","modified_gmt":"2024-04-20T14:48:16","slug":"wordpress%e5%85%8d%e6%8f%92%e4%bb%b6%e8%87%aa%e5%8a%a8%e4%b8%ba%e6%96%87%e7%ab%a0%e6%b7%bb%e5%8a%a0%e8%ae%be%e7%bd%ae%e7%89%b9%e8%89%b2%e5%9b%be%e5%83%8f%e7%9a%84%e4%b8%89%e7%a7%8d%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.slof.top\/?p=16833","title":{"rendered":"wordpress\u514d\u63d2\u4ef6\u81ea\u52a8\u4e3a\u6587\u7ae0\u6dfb\u52a0\u8bbe\u7f6e\u7279\u8272\u56fe\u50cf\u7684\u4e09\u79cd\u65b9\u6cd5"},"content":{"rendered":"\n<p>wordpress\u7279\u8272\u56fe\u50cf\u53ef\u4ee5\u4e3a\u6bcf\u7bc7\u6587\u7ae0\u8bbe\u7f6e\u4e00\u4e2a\u7f29\u7565\u56fe\uff0c\u4f46\u5fc5\u987b\u662f\u624b\u52a8\u8bbe\u7f6e\uff0c\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4ee3\u7801\u5b9e\u73b0\u81ea\u52a8\u628a\u6587\u7ae0\u4e2d\u7684\u7b2c\u4e00\u5f20\u56fe\u7247\u6dfb\u52a0\u4e3a\u7f29\u7565\u56fe\uff0c\u5982\u679c\u6587\u7ae0\u4e2d\u6ca1\u6709\u56fe\u7247\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528\u5a92\u4f53\u5e93\u4e2d\u7684\u67d0\u4e2a\u56fe\u7247\u4f5c\u4e3a\u6587\u7ae0\u7684\u7f29\u7565\u56fe\uff0c\u6216\u8005\u6211\u4eec\u8bbe\u5b9a\u4e00\u4e2a\u6587\u4ef6\u5939\u91cc\u9762\u4e0a\u4f20\u6211\u4eec\u9700\u8981\u8bbe\u5b9a\u4e3a\u7f29\u7565\u56fe\u7684\u56fe\u7247\u3002<\/p>\n\n\n\n<p>\u5728\u5f53\u524d\u4e3b\u9898\u7684functions.php\u91cc\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801<br>function wpforce_featured() {<br>global $post;<br>$already_has_thumb = has_post_thumbnail($post-&gt;ID);<br>if (!$already_has_thumb) {<br>$attached_image = get_children( &#8220;post_parent=$post-&gt;ID&amp;post_type=attachment&amp;post_mime_type=image&amp;numberposts=1&#8221; );<br>if ($attached_image) {<br>foreach ($attached_image as $attachment_id =&gt; $attachment) {<br>set_post_thumbnail($post-&gt;ID, $attachment_id);<br>}<br>}<br>}<br>} \/\/end function<br>add_action(&#8216;the_post&#8217;, &#8216;wpforce_featured&#8217;);<br>add_action(&#8216;save_post&#8217;, &#8216;wpforce_featured&#8217;);<br>add_action(&#8216;draft_to_publish&#8217;, &#8216;wpforce_featured&#8217;);<br>add_action(&#8216;new_to_publish&#8217;, &#8216;wpforce_featured&#8217;);<br>add_action(&#8216;pending_to_publish&#8217;, &#8216;wpforce_featured&#8217;);<br>add_action(&#8216;future_to_publish&#8217;, &#8216;wpforce_featured&#8217;);<br>\u8fd9\u91cc\u6709\u4e00\u6bb5\u5f88\u5b9e\u7528\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u81ea\u52a8\u5c06\u6587\u7ae0\u4e2d\u7684\u7b2c\u4e00\u5f20\u56fe\u7247\u8bbe\u7f6e\u4e3a\u7279\u8272\u56fe\u50cf\uff0c\u5982\u679c\u4f60\u624b\u52a8\u8bbe\u7f6e\u4e86\u7279\u8272\u56fe\u50cf\uff0c\u53ef\u4ee5\u8986\u76d6\u8fd9\u6bb5\u4ee3\u7801\uff0c\u8fd9\u6837\u5373\u4f7f\u4f60\u5fd8\u8bb0\u4e86\u8bbe\u7f6e\uff0cwp\u4f1a\u81ea\u52a8\u8c03\u7528\u6587\u7ae0\u4e2d\u7684\u7b2c\u4e00\u5f20\u56fe\u7247\u4f5c\u4e3a\u7f29\u7565\u56fe\u3002<\/p>\n\n\n\n<p>\u81ea\u52a8\u8c03\u7528\u5a92\u4f53\u5e93\u4e2d\u7684\u56fe\u7247\u4f5c\u4e3a\u7f29\u7565\u56fe<br>\u95ee\u9898\u6765\u4e86\uff0c\u5982\u679c\u6211\u4eec\u7684\u6587\u7ae0\u91cc\u6ca1\u6709\u56fe\u7247\uff0c\u53c8\u5fd8\u4e86\u8bbe\u7f6e\u7279\u8272\u56fe\u50cf\u5462\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u8ba9WP\u8c03\u7528\u5a92\u4f53\u5e93\u91cc\u7684\u67d0\u5f20\u6307\u5b9a\u7684\u56fe\u7247\u4f5c\u4e3a\u7f29\u7565\u56fe\u3002<\/p>\n\n\n\n<p>function wpforce_featured() {<br>global $post;<br>$already_has_thumb = has_post_thumbnail($post-&gt;ID);<br>if (!$already_has_thumb) {<br>$attached_image = get_children( &#8220;post_parent=$post-&gt;ID&amp;post_type=attachment&amp;post_mime_type=image&amp;numberposts=1&#8221; );<br>if ($attached_image) {<br>foreach ($attached_image as $attachment_id =&gt; $attachment) {<br>set_post_thumbnail($post-&gt;ID, $attachment_id);<br>}<br>} else {<br>set_post_thumbnail($post-&gt;ID, &#8217;66&#8217;);<br>}<br>}<br>} \/\/end function<br>add_action(&#8216;the_post&#8217;, &#8216;wpforce_featured&#8217;);<br>add_action(&#8216;save_post&#8217;, &#8216;wpforce_featured&#8217;);<br>add_action(&#8216;draft_to_publish&#8217;, &#8216;wpforce_featured&#8217;);<br>add_action(&#8216;new_to_publish&#8217;, &#8216;wpforce_featured&#8217;);<br>add_action(&#8216;pending_to_publish&#8217;, &#8216;wpforce_featured&#8217;);<br>add_action(&#8216;future_to_publish&#8217;, &#8216;wpforce_featured&#8217;);<br>\u5176\u4e2d $post-&gt;ID, &#8217;66&#8217;\u662f\u5a92\u4f53\u5e93\u4e2d\u67d0\u5f20\u56fe\u7247\u7684id<br>\u5982\u4f55\u67e5\u770b\u5a92\u4f53\u5e93\u91cc\u67d0\u5f20\u56fe\u7247\u7684ID\u5462\uff1f<br>WP-\u540e\u53f0-\u591a\u5a92\u4f53-\u5a92\u4f53\u5e93\uff0c\u91c7\u7528\u4e00\u4e2a\u5217\u8868\u6d4f\u89c8\u65b9\u5f0f\uff0c\u628a\u9f20\u6807\u6307\u5411\u56fe\u7247\uff0c\u5728\u6d4f\u89c8\u5668\u7684\u4e0b\u9762\u4f1a\u663e\u793a\u56fe\u7247\u7684ID\uff0c\u6216\u8005\u4f60\u9605\u8bfb\u4e0b\u9762\u8fd9\u7bc7\u6587\u7ae0\u6765\u5b9e\u73b0\uff1a<br>wordpress\u540e\u53f0\u65e0\u63d2\u4ef6\u663e\u793a\u6587\u7ae0\u548c\u5206\u7c7bID<\/p>\n\n\n\n<p>\u81ea\u5b9a\u4e49\u56fe\u7247\u4f5c\u4e3a\u7279\u8272\u56fe\u50cf<\/p>\n\n\n\n<p>\/**<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6dfb\u52a0\u7279\u8272\u7f29\u7565\u56fe\u652f\u6301\u76f8\u5173\u6587\u7ae0\u5e26\u6709\u7f29\u7565\u56fe<br><em>\/ if ( function_exists(&#8216;add_theme_support&#8217;) )add_theme_support(&#8216;post-thumbnails&#8217;); \/\/\u8ba9\u4f60\u7684\u4e3b\u9898\u5f00\u542f\u7279\u8272\u56fe\u50cf\u529f\u80fd function post_thumbnail_src(){ global $post; if( $values = get_post_custom_values(&#8220;thumb&#8221;) ) {\/\/\u8f93\u51fa\u81ea\u5b9a\u4e49\u57df\u56fe\u7247\u5730\u5740 $values = get_post_custom_values(&#8220;thumb&#8221;); $post_thumbnail_src = $values [0]; } elseif( has_post_thumbnail() ){\/\/\u5982\u679c\u6709\u7279\u8272\u7f29\u7565\u56fe\uff0c\u5219\u8f93\u51fa\u7f29\u7565\u56fe\u5730\u5740 $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),&#8217;thumbnail&#8217;); \/\/\u5176\u4e2dfull\u53ef\u4ee5\u4fee\u6539\u7684\uff0c\u53ef\u4ee5\u4e3athumbnail, medium, large or full\uff08\u5206\u522b\u4ee3\u8868\u6700\u5c0f\u7684\u7f29\u7565\u56fe\u3001\u4e2d\u7b49\u3001\u5927\u548c\u539f\u59cb\u5c3a\u5bf8\uff09 $post_thumbnail_src = $thumbnail_src [0]; } else { $post_thumbnail_src = &#8221;; ob_start(); ob_end_clean(); $output = preg_match_all(&#8216;\/\\'&#8221;[\\'&#8221;].<\/em>>\/i&#8217;, $post->post_content, $matches);<br>$post_thumbnail_src = $matches [1] [0]; \/\/\u83b7\u53d6\u8be5\u56fe\u7247 src<br>if(empty($post_thumbnail_src)){ \/\/\u5982\u679c\u65e5\u5fd7\u4e2d\u6ca1\u6709\u56fe\u7247\uff0c\u5219\u663e\u793a\u968f\u673a\u56fe\u7247<br>$random = mt_rand(1, 10);<br>echo get_bloginfo(&#8216;template_url&#8217;);<br>echo &#8216;\/images\/pic\/&#8217;.$random.&#8217;.jpg&#8217;;<br>\/\/\u5982\u679c\u65e5\u5fd7\u4e2d\u6ca1\u6709\u56fe\u7247\uff0c\u5219\u663e\u793a\u9ed8\u8ba4\u56fe\u7247<br>\/\/echo &#8216;\/images\/default_thumb.jpg&#8217;;<br>}<br>};<br>echo $post_thumbnail_src;<br>}<br>\u6211\u4eec\u5728\u4e3b\u9898\u4e0b\u5efa\u7acb\u56fe\u50cf\u6587\u4ef6\u5939\/images\/pic\/\uff0c\u91cc\u9762\u4e0a\u4f20\u4f60\u5148\u663e\u793a\u7684\u56fe\u7247\uff0c\u4f5c\u4e3a\u968f\u673a\u8c03\u7528\uff0c\u800c\/images\/default_thumb.jpg\u6700\u4e3a\u9ed8\u8ba4\u8c03\u7528\uff0c\u5f53\u7136\u8fd9\u4e24\u4e2a\u529f\u80fd\u6211\u4eec\u9009\u62e9\u4e00\u4e2a\u5c31OK\u4e86\uff0c\u6240\u4ee5\u6ce8\u91ca\u6389\u4e86\u4e00\u4e2a\u3002<\/li>\n<\/ul>\n\n\n\n<p>PS:\u8c03\u7528\u7279\u8272\u56fe\u50cf\u548c\u8bbe\u7f6e\u5927\u5c0f<br>\u8981\u8ba9\u4f60\u7684\u4e3b\u9898\u652f\u6301\u7279\u8272\u56fe\u50cf\u5fc5\u987b\u5728functions.php\u4e2d\u52a0\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p>if ( function_exists(&#8216;add_theme_support&#8217;) )add_theme_support(&#8216;post-thumbnails&#8217;);<br>\u5728post\u6a21\u677f\u4e2d\u8c03\u7528:<\/p>\n\n\n\n<p><br><br>\u53ef\u4ee5\u8c03\u7528\u4e0d\u540c\u5c3a\u5bf8\u7684\u56fe\u7247:<\/p>\n\n\n\n<p>the_post_thumbnail(); \/\/ \u65e0\u53c2\u6570\uff0c\u9ed8\u8ba4\u8c03\u7528Thumbnail<br>the_post_thumbnail(&#8216;thumbnail&#8217;);\/\/ Thumbnail(\u9ed8\u8ba4\u5c3a\u5bf8 150px150px max)<br>the_post_thumbnail(&#8216;medium&#8217;); \/\/ Medium resolution(default300px300px max)<br>the_post_thumbnail(&#8216;large&#8217;); \/\/ Large resolution(default640px640px max)<br>the_post_thumbnail(&#8216;full&#8217;); \/\/ Full resolution(original size uploaded)<br>the_post_thumbnail( array(100,100) );\/\/Other resolutions<br>\u603b\u7ed3<br>1\u3001\u4f60\u5982\u679c\u518d\u5236\u4f5c\u81ea\u5df1\u7684\u4e3b\u9898\uff0c\u5728\u8c03\u8bd5\u4e2d\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4e00\u4e2a\u4f60\u559c\u6b22\u7684\u65b9\u6cd5\u3002<br>2\u3001\u4f60\u7684\u7ad9\u70b9\u5df2\u7ecf\u53d1\u8868\u4e86\u5f88\u591a\u6587\u7ae0\uff0c\u5e76\u4e14\u90fd\u542f\u7528\u53d1\u7279\u8272\u56fe\u50cf\uff0c\u8981\u614e\u7528\uff0c\u53ef\u80fd\u4f60\u9009\u62e9\u7684\u67d0\u4e00\u4e2a\u4ee3\u7801\u529f\u80fd\u4f1a\u5f71\u54cd\u4f60\u5df2\u7ecf\u53d1\u5e03\u7684\u6587\u7ae0\u4e0d\u80fd\u6b63\u5e38\u663e\u793a\uff01<br>3\u3001\u7279\u8272\u56fe\u50cf\u4f1a\u5360\u7528\u5927\u91cf\u7684\u670d\u52a1\u5668\u7a7a\u95f4\uff0c\u56e0\u4e3a\u6bcf\u5f20\u56fe\u7247\u90fd\u4f1a\u88c1\u526a\u6210\u591a\u5f20\u5927\u5c0f\u4e0d\u540c\u7684\u7f29\u7565\u56fe\u65b9\u4fbf\u5728\u4e0d\u540c\u7684\u4f4d\u7f6e\u8c03\u7528\uff0c\u6700\u4e3b\u8981\u7684\u662f\u4e0d\u652f\u6301\u5916\u94fe\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>wordpress\u7279\u8272\u56fe\u50cf\u53ef\u4ee5\u4e3a\u6bcf\u7bc7\u6587\u7ae0\u8bbe\u7f6e\u4e00\u4e2a\u7f29\u7565\u56fe\uff0c\u4f46\u5fc5\u987b\u662f\u624b\u52a8\u8bbe\u7f6e\uff0c\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4ee3\u7801\u5b9e\u73b0\u81ea\u52a8\u628a\u6587\u7ae0\u4e2d\u7684\u7b2c\u4e00\u5f20\u56fe\u7247\u6dfb\u52a0\u4e3a\u7f29\u7565\u56fe\uff0c\u5982\u679c\u6587\u7ae0\u4e2d\u6ca1\u6709\u56fe\u7247\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528\u5a92\u4f53\u5e93\u4e2d\u7684\u67d0\u4e2a\u56fe\u7247\u4f5c\u4e3a\u6587\u7ae0\u7684\u7f29\u7565\u56fe\uff0c &hellip;<\/p>\n","protected":false},"author":1,"featured_media":16815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[10],"tags":[],"class_list":["post-16833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-website","entry","has-media"],"_links":{"self":[{"href":"http:\/\/www.slof.top\/index.php?rest_route=\/wp\/v2\/posts\/16833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.slof.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.slof.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.slof.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.slof.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16833"}],"version-history":[{"count":1,"href":"http:\/\/www.slof.top\/index.php?rest_route=\/wp\/v2\/posts\/16833\/revisions"}],"predecessor-version":[{"id":16834,"href":"http:\/\/www.slof.top\/index.php?rest_route=\/wp\/v2\/posts\/16833\/revisions\/16834"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.slof.top\/index.php?rest_route=\/wp\/v2\/media\/16815"}],"wp:attachment":[{"href":"http:\/\/www.slof.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.slof.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16833"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.slof.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}