<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>添加代码片段 Archives - CNERIS</title>
	<atom:link href="https://cneris.com/zh/tag/%e6%b7%bb%e5%8a%a0%e4%bb%a3%e7%a0%81%e7%89%87%e6%ae%b5/feed/" rel="self" type="application/rss+xml" />
	<link>https://cneris.com/zh/tag/添加代码片段/</link>
	<description></description>
	<lastBuildDate>Tue, 29 Oct 2024 09:03:16 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>在WordPress 如何让每个文章分类设置默认特色图片</title>
		<link>https://cneris.com/zh/%e5%9c%a8wordpress-%e5%a6%82%e4%bd%95%e8%ae%a9%e6%af%8f%e4%b8%aa%e6%96%87%e7%ab%a0%e5%88%86%e7%b1%bb%e8%ae%be%e7%bd%ae%e9%bb%98%e8%ae%a4%e7%89%b9%e8%89%b2%e5%9b%be%e7%89%87/</link>
					<comments>https://cneris.com/zh/%e5%9c%a8wordpress-%e5%a6%82%e4%bd%95%e8%ae%a9%e6%af%8f%e4%b8%aa%e6%96%87%e7%ab%a0%e5%88%86%e7%b1%bb%e8%ae%be%e7%bd%ae%e9%bb%98%e8%ae%a4%e7%89%b9%e8%89%b2%e5%9b%be%e7%89%87/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 29 Oct 2024 09:01:17 +0000</pubDate>
				<category><![CDATA[Centos]]></category>
		<category><![CDATA[Plesk]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[专用服务器]]></category>
		<category><![CDATA[系统管理]]></category>
		<category><![CDATA[网络应用]]></category>
		<category><![CDATA[脚本]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[文章分类]]></category>
		<category><![CDATA[添加代码片段]]></category>
		<category><![CDATA[默认特色图片]]></category>
		<guid isPermaLink="false">https://cneris.com/?p=2673</guid>

					<description><![CDATA[<p>在 WordPress 中，您可以通过在主题的 functions.php 文件中添加代码片段，为每个文章分类设置默认特色图片。此代码会检查文章是否已分配了特色图片；如果没有，则根据文章的分类设置一个默认图片。 根据分类分配默认特色图片的代码 打开主题的 functions.php 文件（最好在子主题中操作，以避免更新时丢失更改）。 添加以下代码，并用每个分类的默认图片 URL 替换示例 URL： function set_default_thumbnail($post_id) { if (has_post_thumbnail($post_id)) { return; }// 定义每个分类的默认图片 $default_images = [ ‘category-1’ =&gt; ‘https://your-site.com/wp-content/uploads/category-image1.jpg’, ‘category-2’ =&gt; ‘https://your-site.com/wp-content/uploads/category-image2.jpg’, ‘category-3’ =&gt; ‘https://your-site.com/wp-content/uploads/category-image3.jpg’, ]; // 获取文章分类 $categories = get_the_category($post_id); // 检查是否分配了分类，并查找匹配的默认图片 foreach ($categories as $category) { if (array_key_exists($category-&gt;slug, $default_images)) { $image_url = $default_images[$category-&gt;slug]; // [...]</p>
<p>The post <a href="https://cneris.com/zh/%e5%9c%a8wordpress-%e5%a6%82%e4%bd%95%e8%ae%a9%e6%af%8f%e4%b8%aa%e6%96%87%e7%ab%a0%e5%88%86%e7%b1%bb%e8%ae%be%e7%bd%ae%e9%bb%98%e8%ae%a4%e7%89%b9%e8%89%b2%e5%9b%be%e7%89%87/">在WordPress 如何让每个文章分类设置默认特色图片</a> appeared first on <a href="https://cneris.com/zh">CNERIS</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>在 WordPress 中，您可以通过在主题的 <code>functions.php</code> 文件中添加代码片段，为每个文章分类设置默认特色图片。此代码会检查文章是否已分配了特色图片；如果没有，则根据文章的分类设置一个默认图片。<span id="more-2673"></span></p>
<h4>根据分类分配默认特色图片的代码</h4>
<ol>
<li>打开主题的 <code>functions.php</code> 文件（最好在子主题中操作，以避免更新时丢失更改）。</li>
<li>添加以下代码，并用每个分类的默认图片 URL 替换示例 URL：</li>
</ol>
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950">
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"></code></code>function set_default_thumbnail($post_id) {<br />
if (has_post_thumbnail($post_id)) {<br />
return;<br />
}<code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"></code></code>// 定义每个分类的默认图片<br />
$default_images = [<br />
&#8216;category-1&#8217; =&gt; &#8216;https://your-site.com/wp-content/uploads/category-image1.jpg&#8217;,<br />
&#8216;category-2&#8217; =&gt; &#8216;https://your-site.com/wp-content/uploads/category-image2.jpg&#8217;,<br />
&#8216;category-3&#8217; =&gt; &#8216;https://your-site.com/wp-content/uploads/category-image3.jpg&#8217;,<br />
];</p>
<p><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"></code></code>// 获取文章分类<br />
$categories = get_the_category($post_id);</p>
<p><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"></code></code>// 检查是否分配了分类，并查找匹配的默认图片<br />
foreach ($categories as $category) {<br />
if (array_key_exists($category-&gt;slug, $default_images)) {<br />
$image_url = $default_images[$category-&gt;slug];</p>
<p>// 下载图片并设置为特色图片<br />
$image_id = attach_image_from_url($image_url, $post_id);<br />
if ($image_id) {<br />
set_post_thumbnail($post_id, $image_id);<br />
}<br />
break;<br />
}<br />
}<br />
}<br />
add_action(&#8216;save_post&#8217;, &#8216;set_default_thumbnail&#8217;);</p>
<p><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"></code></code>// 函数：从 URL 下载图片并附加到文章<br />
function attach_image_from_url($image_url, $post_id) {<br />
$upload_dir = wp_upload_dir();<br />
$image_data = file_get_contents($image_url);<br />
$filename = basename($image_url);</p>
<p><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"></code></code>if (wp_mkdir_p($upload_dir[&#8216;path&#8217;])) {<br />
$file = $upload_dir[&#8216;path&#8217;] . &#8216;/&#8217; . $filename;<br />
} else {<br />
$file = $upload_dir[&#8216;basedir&#8217;] . &#8216;/&#8217; . $filename;<br />
}</p>
<p><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"></code></code>file_put_contents($file, $image_data);</p>
<p><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"></code></code>$wp_filetype = wp_check_filetype($filename, null);<br />
$attachment = [<br />
&#8216;post_mime_type&#8217; =&gt; $wp_filetype[&#8216;type&#8217;],<br />
&#8216;post_title&#8217; =&gt; sanitize_file_name($filename),<br />
&#8216;post_content&#8217; =&gt; &#8221;,<br />
&#8216;post_status&#8217; =&gt; &#8216;inherit&#8217;<br />
];</p>
<p><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"></code></code>$attach_id = wp_insert_attachment($attachment, $file, $post_id);<br />
require_once(ABSPATH . &#8216;wp-admin/includes/image.php&#8217;);<br />
$attach_data = wp_generate_attachment_metadata($attach_id, $file);<br />
wp_update_attachment_metadata($attach_id, $attach_data);</p>
<p><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"></code></code>return $attach_id;<br />
}</p>
<p><code class="!whitespace-pre hljs language-php"><br />
</code></p>
</div>
</div>
<h4>代码说明</h4>
<ul>
<li><strong><code>$default_images</code></strong>：使用分类别名作为键、图片 URL 作为值，定义每个分类的默认图片。</li>
<li><strong><code>set_default_thumbnail</code></strong>：当文章被保存时运行此函数。如果文章没有特色图片，它会找到 <code>$default_images</code> 中第一个匹配的分类并设置为特色图片。</li>
<li><strong><code>attach_image_from_url</code></strong>：此函数从 URL 下载图片并将其附加到文章，生成特色图片。</li>
</ul>
<p>此代码将根据文章的分类自动分配默认特色图片，适用于新文章或编辑的文章。</p>
<p><strong>注意</strong>：确保在开发环境中测试此代码，然后再在正式网站上实施，并用实际的默认图片 URL 替换示例 URL。</p>
<p>The post <a href="https://cneris.com/zh/%e5%9c%a8wordpress-%e5%a6%82%e4%bd%95%e8%ae%a9%e6%af%8f%e4%b8%aa%e6%96%87%e7%ab%a0%e5%88%86%e7%b1%bb%e8%ae%be%e7%bd%ae%e9%bb%98%e8%ae%a4%e7%89%b9%e8%89%b2%e5%9b%be%e7%89%87/">在WordPress 如何让每个文章分类设置默认特色图片</a> appeared first on <a href="https://cneris.com/zh">CNERIS</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cneris.com/zh/%e5%9c%a8wordpress-%e5%a6%82%e4%bd%95%e8%ae%a9%e6%af%8f%e4%b8%aa%e6%96%87%e7%ab%a0%e5%88%86%e7%b1%bb%e8%ae%be%e7%bd%ae%e9%bb%98%e8%ae%a4%e7%89%b9%e8%89%b2%e5%9b%be%e7%89%87/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
