Умалени изображения в WordPress
Вмъкване на опция в WordPress да генерира on-the-fly умалените изображения в сайта.
Първо вмъкваме разрешаваме функцията WordPress да прави умалени изображения със специфичен размер при самото качване на файла.
Добавяме кода в functions.php на вашия шаблон.
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}
След това сваляме скрипта Timthumb и го слагаме в кореновата директория на шаблона. Създаваме папка „cache“, която трябва да е с chmod 755 или 777 (в зависимост от настройките на сървъра).
След това в шаблона, където искате да се показва умалено изображение вмъквате следния код:
<?php
// Checks to see if there is a post thumbnail, or image specified via custom field
if (has_post_thumbnail() || get_post_meta($post->ID, 'post_image_value', true)) {
// Opens a container called post-tnail and accompanying link to enclose the image
echo '<div class="post-tnail"><a href="'.get_permalink().'" title="'.get_the_title().'">';
// Creates a function for both methods of attaching images to posts for inclusion later on
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
$postimage = get_post_meta($post->ID, 'post_image_value', true);
// If the post thumbnails function exists, there is a post thumbnail assigned
if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) {
echo '<img src="'.get_bloginfo('template_url').'/thumb.php?src='.$thumbnail[0].'&w=150&h=110&zc=1&q=95" alt="'.get_the_title().'" />';
}
// Or if the post thumbnails function does not exist and there is no post thumbnail, but there is a custom field image
else if (get_post_meta($post->ID, 'post_image_value', true)) {
echo '<img src="'.get_bloginfo('template_url').'/thumb.php?src='.$postimage.'&w=150&h=110&zc=1&q=95" alt="'.get_the_title().'" />';
}
// Closes the surrounding link and container
echo '</a></div>';
}
?>
За мен е по-удобно (предполагам и на вас) да направя външен файл, който съдържа кода за умалените изображения, запазвам го с име thumbnail.php в папка /includes и го вмъквам в темата
<?php include ('includes/thumbnails.php'); ?>
Оригиналната статия може да намерите в сайта на Matt Brett

















Leave a Reply
Want to join the discussion?Feel free to contribute!