-
Notifications
You must be signed in to change notification settings - Fork 39
Using rtp_generate_thumbs() function in child theme
This is by-far the easiest way to create the blog thumbnails on the fly, all we need is, to get familiar with this. We have the default rtp_generate_thumbs() function available in our rtPanel core theme. This function directly returns the thumb_URL that can be used anywhere as the image source on the page. This function needs to be called with parameters as:
$thumb_url = rtp_generate_thumbs( get_post_thumbnail_id(), 'rtp-blog-thumb' );
The 'rtp-blog-thumb' blog here specifies the thumb size that has been to be registered prior to using it in in this function. So this can be registered as:
function rtp_register_thumb_size(){
add_image_size( 'rtp-blog-thumb', 319, 190, 1 );
}
add_action( 'init', 'rtp_register_thumb_size' );
Similarly we can register as many image sizes we want to and correspondingly use it in our code. So now that the image size has been registered, we can specify it using our rtp_generate_thumbs() function as shown above in the first snippet, where we stored the return value in some variable $thumb_url. By default the function provides the image fallback, searching for the post's featured image first and if its not found, it checks for the post's first image. But what if this is also not found? Here we add a provision for the default image in our child theme as:
$thumb_url = ( $thumb_url ) ? $thumb_url : get_stylesheet_directory_uri(). '/default-img.jpg';
Also if we want to add the functionality for checking the attachments of a particular post as yet another image fallback condition, we need to over-ride the default rtp_generate_thumbs() function as**_ rtp_generate_child_thumbs()_** in our function.php and add the following code segment in addition to the code available in the default rtp_generate_thumbs() function.
function rtp_generate_child_thumbs() {
if ($attach_id == ''){
$images =& get_children( 'post_parent='.$post->ID.'&post_type=attachment&post_mime_type=image&numberposts=1' );
}
if ( $images ) {
// if the post has attachments
foreach ( $images as $attachment) {
$image_src = wp_get_attachment_image_src( $attachment->ID, $size );
return $image_src[0] ;
}
//rest keep the same as in the default rtp_generate_thumbs() function
}
Now that we have overridden the default function, now instead of calling the rtp_generate_thumbs() function, we make calls to rtp_generate_child_thumbs() instead, as:
$thumb_url = rtp_generate_child_thumbs( get_post_thumbnail_id(), 'rtp-blog-thumb' );
I hope now you know how to use 'rtp_generate_thumbs()' and 'rtp_generate_child_theme()' functions in rtPanel.
For any questions/doubts you can connect with rtPanel developers on our support forum.
rtPanel:
- End-User Documentation
- Developer Documentation
- General
- Customizing Image Sliders in rtPanel Child Theme
- Add Google ad-banner above logo in rtPanel
- Customize rtPanel Footer Information
- Filters in rtPanel
- Integrated SEO plugin in rtPanel
- Create Metabox on rtPanel Child Theme
- Add Google Adsense Link Unit In Header
- Create Custom Taxonomy on rtPanel
- rtPanel:Comments with Gravatar
- rtPanel: Subscribe Widget
- Create Custom Post Type with rtPanel Child Theme
- rtPanel Theme Options ‘General’
- Using rtp_generate_thumbs() function in child theme
- WordPress Debugger 'WP_DEBUG'
- Adding Social Sharing Button to Website: Using Text Widget
- Clickable header in rtPanel child theme
- CSS Checklist for developers
- Implementing rtp-slider in the child theme
- Creating custom page templates in rtPanel child theme
- Child Theme development using rtPanel – Part I
- Child Theme development using rtPanel – Part 2
- rtPanel Developer Program: Standards Guidelines
- rtPanel Developer Program:Test Project
- Removing the default rtPanel Hooks
- Browser Specific Styling