Categories featured images
-
Hi.
Is it possible to have certain featured images for certain categories? I want if one category selected,the default image of this category will be featured image and if someone upload more images,those images will shown inside the article.
Thank you.
-
Glad to help.
It’s not a current feature of the plugin, I do like the idea though. The only way to do it currently is programmatically, either by modifying your theme template or using one of the filter hooks in the plugin function,
usp_auto_display_images()which is located on line five in/library/core-functions.php. I’m not sure if it’s 100% do-able for this particular task, but thought I would mention in case it is useful for you.I hope it helps, let me know if I can provide any further information, thank you.
-
This reply was modified 1 year, 3 months ago by
Jeff Starr.
Thank you for your answer. I asked Gemini AI and it gave me this code.I don’t know if its ok but I will try it and i will inform you
<?php
// User Submitted Posts - Core Functions
if (!defined('ABSPATH')) die();
function usp_auto_display_images($content) {
global $usp_options;
$enable = isset($usp_options['auto_display_images']) ? $usp_options['auto_display_images'] : 'disable';
if (usp_is_public_submission() && ($enable === 'before' || $enable === 'after')) {
$markup = isset($usp_options['auto_image_markup']) ? $usp_options['auto_image_markup'] : '';
$author = get_post_meta(get_the_ID(), 'user_submit_name', true);
$post_id = get_the_ID();
$categories = get_the_category($post_id);
$featured_image_set = false; // Flag to check if featured image was set
if ($categories) {
foreach ($categories as $category) {
// Check if category has a default featured image
$default_featured_image_url = get_category_meta($category->term_id, 'default_featured_image', true);
if ($default_featured_image_url && !$featured_image_set) {
// Set default featured image
$featured_image_id = usp_get_attachment_id_from_url($default_featured_image_url);
if ($featured_image_id) {
set_post_thumbnail($post_id, $featured_image_id);
$featured_image_set = true;
}
}
}
}
$args = array(
'post_type' => 'attachment',
'post_parent' => $post_id,
'numberposts' => -1,
);
$args = apply_filters('usp_image_args', $args);
$attachments = get_posts($args);
if ($attachments) {
// Display other images inside the article
$images = '<p>';
foreach ($attachments as $attachment) {
$title = apply_filters('usp_image_title', $attachment->post_title);
$thumb = apply_filters('usp_image_thumb', wp_get_attachment_image_src($attachment->ID, 'thumbnail', false));
$medium = apply_filters('usp_image_medium', wp_get_attachment_image_src($attachment->ID, 'medium', false));
$large = apply_filters('usp_image_large', wp_get_attachment_image_src($attachment->ID, 'large', false));
$full = apply_filters('usp_image_full', wp_get_attachment_image_src($attachment->ID, 'full', false));
$custom_size = apply_filters('usp_image_custom_size', 'custom');
$custom = apply_filters('usp_image_custom', wp_get_attachment_image_src($attachment->ID, $custom_size, false));
$parent_id = wp_get_post_parent_id($attachment->ID);
$parent_title = get_the_title($parent_id);
$url = apply_filters('usp_url_custom_field', get_post_meta(get_the_ID(), 'user_submit_url', true));
$images .= usp_replace_image_vars($markup, $title, $thumb, $medium, $large, $full, $custom, $parent_title, $author, $url);
}
$images .= '</p>';
if ($enable === 'before') $content = $images . $content;
elseif ($enable === 'after') $content = $content . $images;
}
}
return $content;
}
add_filter('the_content', 'usp_auto_display_images');
// Helper function to get attachment ID from URL
function usp_get_attachment_id_from_url($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url));
if ($attachment) {
return $attachment[0];
}
return false;
}Just remember that any changes need done via filter and/or action hooks, and not by simply modifying the core plugin files — otherwise changes will be overwritten on subsequent plugin upgrades. Just FYI
-
This reply was modified 1 year, 3 months ago by
The topic ‘Categories featured images’ is closed to new replies.