Last update at :2024-04-08,Edit by888u
DUX theme, this theme looks very good, but the setting of the thumbnail is very simple: if the "Featured Image" is set, the "Featured Image" will be used as the thumbnail; if the "Featured Image" is not set, a "Featured Image" will be displayed. a default picture. After research, I finally realized the display of random thumbnails in the DUX theme: if the "featured image" is set, the "featured image" is used as the thumbnail; if the "featured image" is not set, the first image in the article is automatically retrieved The picture is used as a thumbnail; if there is no picture in the article, a random picture is displayed.
The specific implementation method is as follows (taking DUX 1.3 version as an example):
First modify inc/fn.php, open the fn.php file, find line 463, and change the following content:
$html = sprintf('', get_stylesheet_directory_uri() . '/img /thumbnail.png', $class);
Modify to:
$random = mt_rand(1, 20); $html = sprintf('', get_stylesheet_directory_uri() . '/img/random/'.$random.'.jpg', $class);
Next, we create a folder random under the theme's img, copy in 20 pictures, and rename them to 1.jpg, 2.jpg...20.jpg. In this way, the DUX theme will automatically display random Thumbnail function, if the article does not set a "featured image", a random image will be displayed.
Now I will automatically load the first image and add the following code to the theme's functions.php file:
function autoset_featured() { global $post; $already_has_thumb = has_post_thumbnail($post->ID); if (!$already_has_thumb) { $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment_id); } } } } //end function add_action('the_post', 'autoset_featured'); add_action('save_post', 'autoset_featured'); add_action('draft_to_publish', 'autoset_featured'); add_action('new_to_publish', 'autoset_featured'); add_action('pending_to_publish', 'autoset_featured'); add_action('future_to_publish', 'autoset_featured');
In this way, the DUX theme has been modified, save it and you can see the effect.
Recommended site search: vps foreign server, domain name registration website query, host rental, domain name registration information query, free domain name registration platform, Alibaba Cloud free virtual host, Chinese domain name, managed host, Chinese domain name, Chinese domain name registration query, p>
', get_stylesheet_directory_uri() . '/img /thumbnail.png" alt="DUX theme implements random display of thumbnails" width="600px" height="484px"/>
发表评论