{"id":31426,"date":"2024-11-25T10:51:01","date_gmt":"2024-11-25T02:51:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31426\/"},"modified":"2024-11-25T10:51:01","modified_gmt":"2024-11-25T02:51:01","slug":"wordpress%e4%b9%8b%e7%94%a8%e5%ad%97%e6%af%8d%e6%9b%bf%e4%bb%a3%e5%9b%be%e7%89%87%e8%84%9a%e6%9c%ac%ef%bc%9aletteravatar","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31426\/","title":{"rendered":"WordPress\u4e4b\u7528\u5b57\u6bcd\u66ff\u4ee3\u56fe\u7247\u811a\u672c\uff1aLetterAvatar"},"content":{"rendered":"<p style=\"text-align: center\"><strong><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/020\/5e9e8a16c925e696.jpg\" class=\"aligncenter\" title=\"WordPress\u4e4b\u7528\u5b57\u6bcd\u66ff\u4ee3\u56fe\u7247\u811a\u672c\uff1aLetterAvatar\u63d2\u56fe\" alt=\"WordPress\u4e4b\u7528\u5b57\u6bcd\u66ff\u4ee3\u56fe\u7247\u811a\u672c\uff1aLetterAvatar\u63d2\u56fe\" \/><\/strong><\/p>\n<p>\u4e0b\u9762\u7531\/&#8221; target=&#8221;_blank&#8221;&gt;wordpress\u6280\u5de7\u680f\u76ee\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u5173\u4e8e\u7528\u5b57\u6bcd\u66ff\u4ee3\u56fe\u7247\u811a\u672c\uff1aletteravatar\uff0c\u5e0c\u671b\u5bf9\u9700\u8981\u7684\u670b\u53cb\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u57fa\u4e8ecanvas\uff0c\u901a\u8fc7toDataURL\u52a8\u6001\u751f\u6210base64\u56fe\u7247\u3002\u76ee\u524d\u6211\u4e3b\u9898\u7684Gravatar\u5934\u50cf\uff0c\u5c31\u662f\u5229\u7528\u8fd9\u4e2aLetterAvatar\u811a\u672c\u5b9e\u73b0\u672a\u8bbe\u7f6eGravatar\u5934\u50cf\u5219\u8bfb\u53d6ALT\u6807\u7b7e\uff0c\u81ea\u52a8\u751f\u6210\u9996\u5b57\u56fe\u7247\u66ff\u4ee3\u9ed8\u8ba4\u7684\u5934\u50cf\u56fe\u7247\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/image\/875\/123\/800\/1587448327893213.png\" class=\"aligncenter\" title=\"WordPress\u4e4b\u7528\u5b57\u6bcd\u66ff\u4ee3\u56fe\u7247\u811a\u672c\uff1aLetterAvatar\u63d2\u56fe1\" alt=\"WordPress\u4e4b\u7528\u5b57\u6bcd\u66ff\u4ee3\u56fe\u7247\u811a\u672c\uff1aLetterAvatar\u63d2\u56fe1\" \/><\/p>\n<p>\u4e4b\u524d\u5df2\u6709WP\u7231\u597d\u8005\u5236\u4f5c\u4e86\u4e00\u6b3e\uff1amk-letter-avatar \u5b57\u6bcd\u5934\u50cf\u63d2\u4ef6\uff0c\u8bd5\u4e86\u4e00\u4e0b\u5f88\u597d\u7528\uff0c\u4e0d\u8fc7\u6253\u5f00\u6d4f\u89c8\u5668\u5f00\u8005\u5de5\u5177\u53d1\u73b0\u4ea7\u751f\u5927\u91cf404\u9519\u8bef\uff0c\u770b\u4e86\u4e00\u4e0b\u6e90\u4ee3\u7801\uff0c\u8be5\u63d2\u4ef6\u662f\u901a\u8fc7\u65e0\u5934\u50cf\u8fd4\u56de404\u9519\u8bef\uff0c\u89e6\u53d1onerror\u4e8b\u4ef6\u7528\u81ea\u52a8\u751f\u6210\u7684\u5b57\u6bcd\u56fe\u7247\u66ff\u6362src\u56fe\u7247\u5730\u5740\uff0c\u5224\u65ad\u65b9\u5f0f\u4e0d\u662f\u5f88\u5408\u7406\uff0c\u5982\u679c\u4e0d\u662f\u56e0\u4e3a\u4e2a\u7f3a\u70b9\u6211\u90fd\u60f3\u76f4\u63a5\u62ff\u6765\u7528\u4e86\uff0c\u5982\u679c\u4f5c\u8005\u518d\u4f18\u5316\u4e00\u4e0b\uff0c\u7edd\u5bf9\u662f\u6b3e\u4f18\u79c0\u5b9e\u7528\u7684\u63d2\u4ef6\u3002<\/p>\n<p>\u6211\u7684\u5b9e\u73b0\u539f\u7406\u548c\u63d2\u4ef6\u4e0d\u540c\uff0c\u914d\u5408\u5934\u50cf\u672c\u5730\u7f13\u5b58\u529f\u80fd\uff0c\u5224\u65ad\u65e0\u5934\u50cf\u540e\uff0c\u76f4\u63a5\u4e3a\u65e0\u5934\u50cf\u7684\u56fe\u7247\u6dfb\u52a0\u7279\u5b9a\u7684class\u7c7b\uff0c\u7136\u540e\u901a\u8fc7LetterAvatar\u811a\u672c\u66ff\u6362\u56fe\u7247\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\u4e0a\u9762\u63d0\u5230\u7684\u63d2\u4ef6\uff0cGravatar\u5934\u50cf\u56fe\u7247\u5fc5\u987b\u6709alt\u6807\u7b7e\u5c5e\u6027\uff0c\u5426\u5219\u4e0d\u4f1a\u751f\u6210\u6b63\u5e38\u7684\u56fe\u7247\uff0c\u53ef\u60dc\u5927\u90e8\u5206\u4e3b\u9898\u9ed8\u8ba4Gravatar\u5934\u50cfalt\u6807\u7b7e\u5c5e\u6027\u662f\u7a7a\u7684&#8230;..<\/p>\n<p>\u5982\u679c\u60f3\u81ea\u52a8\u4e3aGravatar\u5934\u50cf\u6dfb\u52a0alt\u6807\u7b7e\u5c5e\u6027\uff0c\u53ef\u4ee5\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u6dfb\u52a0\u5230\u5f53\u524d\u4e3b\u9898\u51fd\u6570\u6a21\u677ffunctions.php\u4e2d\uff1a<\/p>\n<pre>function&nbsp;zm_gravatar_alt($altgravatar)&nbsp;{\nif&nbsp;(have_comments())&nbsp;{\n$alt&nbsp;=&nbsp;get_comment_author();\n}\nelse&nbsp;{\n$alt&nbsp;=&nbsp;get_the_author_meta('display_name');\n}\n$altgravatar=&nbsp;str_replace('alt=''',&nbsp;'alt=''&nbsp;.&nbsp;$alt&nbsp;.&nbsp;''&nbsp;title='Gravatar&nbsp;for&nbsp;'&nbsp;.&nbsp;$alt&nbsp;.&nbsp;''',&nbsp;$altgravatar);\nreturn&nbsp;$altgravatar;\n}\nadd_filter('get_avatar',&nbsp;'zm_gravatar_alt');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e4b\u540e\uff0c\u81ea\u52a8\u5c06\u8bc4\u8bba\u8005\u6635\u79f0\u505a\u4e3aalt\u5c5e\u6027\u3002<\/p>\n<p>\u672c\u6587\u53ea\u662f\u81ea\u5df1\u505a\u4e2a\u8bb0\u5f55\uff0c\u5e76\u4e0d\u662f\u6559\u5927\u5bb6\u600e\u4e48\u5f04\u8fd9\u4e2a\u5934\u50cf\uff0c\u5982\u679c\u8ba4\u4e3a\u8fd9\u5b57\u6bcd\u5934\u50cf\u8fd8\u4e0d\u9519\uff0c\u8bf7\u76f4\u63a5\u4f7f\u7528\u4e0a\u9762\u4ecb\u7ecd\u7684\u63d2\u4ef6\u3002<\/p>\n<p>\u53e6\u9644LetterAvatar\u811a\u672c\u6f14\u793a\u4ee3\u7801\uff1a<\/p>\n<pre>nbsp;html&amp;gt;\n\n&lt;h1&gt;Letter&nbsp;Avatar&lt;\/h1&gt;\n&nbsp;\n&lt;small&gt;&lt;strong&gt;\u7528\u6cd5:&lt;\/strong&gt;&lt;\/small&gt;\n&lt;pre class=\"brush:php;toolbar:false\"&gt;\n&lt;code&gt;@@##@@&lt;\/code&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 &nbsp;<br \/>\n<img><br \/>\n<img><br \/>\n<img> &nbsp; &lt;script&gt;&lt;\/script&gt;&lt;script&gt; \/* * LetterAvatar * * Artur Heinze * Create Letter avatar based on Initials * based on https:\/\/gist.github.com\/leecrossley\/6027780 *\/ (function(w, d){ function LetterAvatar (name, size, color) { name = name || &#039;&#039;; size = size || 60; var colours = [ &#8220;#1abc9c&#8221;, &#8220;#2ecc71&#8221;, &#8220;#3498db&#8221;, &#8220;#9b59b6&#8221;, &#8220;#34495e&#8221;, &#8220;#16a085&#8221;, &#8220;#27ae60&#8221;, &#8220;#2980b9&#8221;, &#8220;#8e44ad&#8221;, &#8220;#2c3e50&#8221;, &#8220;#f1c40f&#8221;, &#8220;#e67e22&#8221;, &#8220;#e74c3c&#8221;, &#8220;#ecf0f1&#8221;, &#8220;#95a5a6&#8221;, &#8220;#f39c12&#8221;, &#8220;#d35400&#8221;, &#8220;#c0392b&#8221;, &#8220;#bdc3c7&#8221;, &#8220;#7f8c8d&#8221; ], nameSplit = String(name).split(&#039; &#039;), initials, charIndex, colourIndex, canvas, context, dataURI; if (nameSplit.length == 1) { initials = nameSplit[0] ? nameSplit[0].charAt(0):&#039;?&#039;; } else { initials = nameSplit[0].charAt(0) + nameSplit[1].charAt(0); } if (w.devicePixelRatio) { size = (size * w.devicePixelRatio); } charIndex = (initials == &#039;?&#039; ? 72 : initials.charCodeAt(0)) &#8211; 64; colourIndex = charIndex % 20; canvas = d.createElement(&#039;canvas&#039;); canvas.width = size; canvas.height = size; context = canvas.getContext(&#8220;2d&#8221;); context.fillStyle = color ? color : colours[colourIndex &#8211; 1]; context.fillRect (0, 0, canvas.width, canvas.height); context.font = Math.round(canvas.width\/2)+&#8221;px Arial&#8221;; context.textAlign = &#8220;center&#8221;; context.fillStyle = &#8220;#FFF&#8221;; context.fillText(initials, size \/ 2, size \/ 1.5); dataURI = canvas.toDataURL(); canvas = null; return dataURI; } LetterAvatar.transform = function() { Array.prototype.forEach.call(d.querySelectorAll(&#039;img[alt]&#039;), function(img, name, color) { name = img.getAttribute(&#039;alt&#039;); color = img.getAttribute(&#039;color&#039;); img.src = LetterAvatar(name, img.getAttribute(&#039;width&#039;), color); img.removeAttribute(&#039;avatar&#039;); img.setAttribute(&#039;alt&#039;, name); }); }; \/\/ AMD support if (typeof define === &#039;function&#039; &amp;&amp; define.amd) { define(function () { return LetterAvatar; }); \/\/ CommonJS and Node.js module support. } else if (typeof exports !== &#039;undefined&#039;) { \/\/ Support Node.js specific `module.exports` (which can be a function) if (typeof module != &#039;undefined&#039; &amp;&amp; module.exports) { exports = module.exports = LetterAvatar; } \/\/ But always support CommonJS module 1.1.1 spec (`exports` cannot be a function) exports.LetterAvatar = LetterAvatar; } else { window.LetterAvatar = LetterAvatar; d.addEventListener(&#039;DOMContentLoaded&#039;, function(event) { LetterAvatar.transform(); }); } })(window, document); &lt;\/script&gt; <\/p>\n<p>\u539f\u9879\u76ee\u5730\u5740\uff1ahttps:\/\/github.com\/daolavi\/LetterAvatar<\/p>\n<p>\u63a8\u8350\u6559\u7a0b\uff1a\u300a\u300b<\/p>\n<p><img> <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fWordPress\u4e4b\u7528\u5b57\u6bcd\u66ff\u4ee3\u56fe\u7247\u811a\u672c\uff1aLetterAvatar\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>\u4e0b\u9762\u7531\/&#8221; target=&#8221;_blank&#8221;&gt;wordpress\u6280\u5de7\u680f\u76ee\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u5173\u4e8e\u7528\u5b57\u6bcd\u66ff\u4ee3\u56fe\u7247\u811a\u672c\uff1aletteravatar\uff0c\u5e0c\u671b\u5bf9\u9700\u8981\u7684\u670b\u53cb\u6709\u6240\u5e2e\u52a9\uff01 \u57fa\u4e8ecanvas\uff0c\u901a\u8fc7toDataURL\u52a8\u6001\u751f\u6210base64\u56fe\u7247\u3002\u76ee\u524d\u6211\u4e3b\u9898\u7684Gravatar\u5934\u50cf\uff0c\u5c31\u662f\u5229\u7528\u8fd9\u4e2aLetterAvatar\u811a\u672c\u5b9e\u73b0\u672a\u8bbe\u7f6eGravatar\u5934\u50cf\u5219\u8bfb\u53d6ALT\u6807\u7b7e\uff0c\u81ea\u52a8\u751f\u6210\u9996\u5b57\u56fe\u7247\u66ff\u4ee3\u9ed8\u8ba4\u7684\u5934\u50cf\u56fe\u7247\u3002 \u4e4b\u524d\u5df2\u6709WP\u7231\u597d\u8005\u5236\u4f5c\u4e86\u4e00\u6b3e\uff1amk-letter-avatar \u5b57\u6bcd\u5934\u50cf\u63d2\u4ef6\uff0c\u8bd5\u4e86\u4e00\u4e0b\u5f88\u597d\u7528\uff0c\u4e0d\u8fc7\u6253\u5f00\u6d4f\u89c8\u5668\u5f00\u8005\u5de5\u5177\u53d1\u73b0\u4ea7\u751f\u5927\u91cf404\u9519\u8bef\uff0c\u770b\u4e86\u4e00\u4e0b\u6e90\u4ee3\u7801\uff0c\u8be5\u63d2\u4ef6\u662f\u901a\u8fc7\u65e0\u5934\u50cf\u8fd4\u56de404\u9519\u8bef\uff0c\u89e6\u53d1onerror\u4e8b\u4ef6\u7528\u81ea\u52a8\u751f\u6210\u7684\u5b57\u6bcd\u56fe\u7247\u66ff\u6362src\u56fe\u7247\u5730\u5740\uff0c\u5224\u65ad\u65b9\u5f0f\u4e0d\u662f\u5f88\u5408\u7406\uff0c\u5982\u679c\u4e0d\u662f\u56e0\u4e3a\u4e2a\u7f3a\u70b9\u6211\u90fd\u60f3\u76f4\u63a5\u62ff\u6765\u7528\u4e86\uff0c\u5982\u679c\u4f5c\u8005\u518d\u4f18\u5316\u4e00\u4e0b\uff0c\u7edd\u5bf9\u662f\u6b3e\u4f18\u79c0\u5b9e\u7528\u7684\u63d2\u4ef6\u3002 \u6211\u7684\u5b9e\u73b0\u539f\u7406\u548c\u63d2\u4ef6\u4e0d\u540c\uff0c\u914d\u5408\u5934\u50cf\u672c\u5730\u7f13\u5b58\u529f\u80fd\uff0c\u5224\u65ad\u65e0\u5934\u50cf\u540e\uff0c\u76f4\u63a5\u4e3a\u65e0\u5934\u50cf\u7684\u56fe\u7247\u6dfb\u52a0\u7279\u5b9a\u7684class\u7c7b\uff0c\u7136\u540e\u901a\u8fc7LetterAvatar\u811a\u672c\u66ff\u6362\u56fe\u7247\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\u4e0a\u9762\u63d0\u5230\u7684\u63d2\u4ef6\uff0cGravatar\u5934\u50cf\u56fe\u7247\u5fc5\u987b\u6709alt\u6807\u7b7e\u5c5e\u6027\uff0c\u5426\u5219\u4e0d\u4f1a\u751f\u6210\u6b63\u5e38\u7684\u56fe\u7247\uff0c\u53ef\u60dc\u5927\u90e8\u5206\u4e3b\u9898\u9ed8\u8ba4Gravatar\u5934\u50cfalt\u6807\u7b7e\u5c5e\u6027\u662f\u7a7a\u7684&#8230;.. \u5982\u679c\u60f3\u81ea\u52a8\u4e3aGravatar\u5934\u50cf\u6dfb\u52a0alt\u6807\u7b7e\u5c5e\u6027\uff0c\u53ef\u4ee5\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u6dfb\u52a0\u5230\u5f53\u524d\u4e3b\u9898\u51fd\u6570\u6a21\u677ffunctions.php\u4e2d\uff1a function&nbsp;zm_gravatar_alt($altgravatar)&nbsp;{ if&nbsp;(have_comments())&nbsp;{ $alt&nbsp;=&nbsp;get_comment_author(); } else&nbsp;{ $alt&nbsp;=&nbsp;get_the_author_meta(&#8216;display_name&#8217;); } $altgravatar=&nbsp;str_replace(&#8216;alt=&#8221;&#8217;,&nbsp;&#8216;alt=&#8221;&nbsp;.&nbsp;$alt&nbsp;.&nbsp;&#8221;&nbsp;title=&#8217;Gravatar&nbsp;for&nbsp;&#8216;&nbsp;.&nbsp;$alt&nbsp;.&nbsp;&#8221;&#8217;,&nbsp;$altgravatar); return&nbsp;$altgravatar; } add_filter(&#8216;get_avatar&#8217;,&nbsp;&#8216;zm_gravatar_alt&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u4e4b\u540e\uff0c\u81ea\u52a8\u5c06\u8bc4\u8bba\u8005\u6635\u79f0\u505a\u4e3aalt\u5c5e\u6027\u3002 \u672c\u6587\u53ea\u662f\u81ea\u5df1\u505a\u4e2a\u8bb0\u5f55\uff0c\u5e76\u4e0d\u662f\u6559\u5927\u5bb6\u600e\u4e48\u5f04\u8fd9\u4e2a\u5934\u50cf\uff0c\u5982\u679c\u8ba4\u4e3a\u8fd9\u5b57\u6bcd\u5934\u50cf\u8fd8\u4e0d\u9519\uff0c\u8bf7\u76f4\u63a5\u4f7f\u7528\u4e0a\u9762\u4ecb\u7ecd\u7684\u63d2\u4ef6\u3002 \u53e6\u9644LetterAvatar\u811a\u672c\u6f14\u793a\u4ee3\u7801\uff1a nbsp;html&amp;gt; &lt;h1&gt;Letter&nbsp;Avatar&lt;\/h1&gt; &nbsp; &lt;small&gt;&lt;strong&gt;\u7528\u6cd5:&lt;\/strong&gt;&lt;\/small&gt; &lt;pre class=&#8221;brush:php;toolbar:false&#8221;&gt; &lt;code&gt;@@##@@&lt;\/code&gt; \u767b\u5f55\u540e\u590d\u5236 &nbsp; &nbsp; &lt;script&gt;&lt;\/script&gt;&lt;script&gt; \/* * LetterAvatar * * Artur Heinze * Create Letter avatar based on Initials * based on https:\/\/gist.github.com\/leecrossley\/6027780 *\/ (function(w, d){ [&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-31426","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31426","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=31426"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31426\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}