<?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>featured image Archives - CNERIS</title>
	<atom:link href="https://cneris.com/en/tag/featured-image/feed/" rel="self" type="application/rss+xml" />
	<link>https://cneris.com/en/tag/featured-image/</link>
	<description></description>
	<lastBuildDate>Tue, 29 Oct 2024 08:59:48 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>How you can set a default featured image for each post category</title>
		<link>https://cneris.com/en/how-you-can-set-a-default-featured-image-for-each-post-category/</link>
					<comments>https://cneris.com/en/how-you-can-set-a-default-featured-image-for-each-post-category/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 29 Oct 2024 08:59:48 +0000</pubDate>
				<category><![CDATA[Centos]]></category>
		<category><![CDATA[Dedicated servers]]></category>
		<category><![CDATA[Plesk]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Web Applications]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[featured image]]></category>
		<category><![CDATA[post category]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">https://cneris.com/?p=2671</guid>

					<description><![CDATA[<p>In WordPress, you can set a default featured image for each post category by using a snippet of code in your theme's functions.php file. This code checks if a post has a featured image assigned; if not, it sets a default image based on the post's category. Code to Assign Default Featured Image by Category [...]</p>
<p>The post <a href="https://cneris.com/en/how-you-can-set-a-default-featured-image-for-each-post-category/">How you can set a default featured image for each post category</a> appeared first on <a href="https://cneris.com/en">CNERIS</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In WordPress, you can set a default featured image for each post category by using a snippet of code in your theme&#8217;s <code>functions.php</code> file. This code checks if a post has a featured image assigned; if not, it sets a default image based on the post&#8217;s category.</p>
<h4>Code to Assign Default Featured Image by Category</h4>
<ol>
<li>Open your theme&#8217;s <code>functions.php</code> file (preferably a child theme to avoid losing changes with updates).</li>
<li>Add the following code, replacing the URLs with your default images for each category:</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"><br />
</code></div>
</div>
<p>function set_default_thumbnail($post_id) {<br />
if (has_post_thumbnail($post_id)) {<br />
return;<br />
}</p>
<p>// Define default images for each category<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>// Get post categories<br />
$categories = get_the_category($post_id);</p>
<p>// Check if categories are assigned and find a matching default image<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>// Download image and set as featured image<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>// Function to download and attach the image to the post<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>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>file_put_contents($file, $image_data);</p>
<p>$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>$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>return $attach_id;<br />
}</p>
<h4>Code Explanation</h4>
<ul>
<li><strong><code>$default_images</code></strong>: Defines default images for each category using the category slug as the key and the image URL as the value.</li>
<li><strong><code>set_default_thumbnail</code></strong>: This function runs when a post is saved. If the post has no featured image, it finds the first category with a default image in <code>$default_images</code> and sets it as the featured image.</li>
<li><strong><code>attach_image_from_url</code></strong>: This function downloads the image from the URL and attaches it to the post as a media file, making it the featured image.</li>
</ul>
<p>This code will automatically assign a default featured image for new or edited posts based on their category.</p>
<p><strong>Note</strong>: Make sure to test this code in a development environment before implementing it on your live site, and replace the example URLs with your actual default image URLs.</p>
<p>The post <a href="https://cneris.com/en/how-you-can-set-a-default-featured-image-for-each-post-category/">How you can set a default featured image for each post category</a> appeared first on <a href="https://cneris.com/en">CNERIS</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cneris.com/en/how-you-can-set-a-default-featured-image-for-each-post-category/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
