Friday 12 June 2015

How to Set default thumbnails size & creating multiple thumbnail sizes?

Registers a new image size. This means WordPress will create a copy of the image with the specified dimensions when a new image is uploaded.
To set the default Featured Image (formerly Post Thumbnail) dimensions use: set_post_thumbnail_size()
<?php add_image_size$name$width$height$crop ); ?>
if ( function_exists( 'add_theme_support' ) ) {
   add_theme_support( 'post-thumbnails');
   set_post_thumbnail_size( 200, 200, true );
}

What does above code really mean?

Does it mean all my thumbnails will be 800x600 or smaller? Because even after setting set_post_thumbnail_size get_post_thumbnail_id($post->ID) points to full-sized thumbnails (like 2500x1200). I don't really need that big images...

When do WordPress create (custom) thumbnails?

Set the image size by cropping the image (not showing part of it):
Set the image size by resizing the image proportionally (without distorting it):

add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) ); // Hard crop left top

When setting a crop position, the first value in the array is the x axis crop position, the second is the y axis crop position.
  • x_crop_position accepts 'left' 'center', or 'right'.
  • y_crop_position accepts 'top', 'center', or 'bottom'.
By default, these values default to 'center' when using hard crop mode.

if ( function_exists( 'add_theme_support' ) ) {
  add_theme_support( 'post-thumbnails');
  set_post_thumbnail_size( 900, 600, true );
  add_image_size( 'foo', 400, 300, true );
  add_image_size( 'bar', 200, 250, true ); 
}


When are these custom thumbnails created (like image-900x600.jpg / image-400x300.jpg etc.) ? Only during upload process? What if I'm just changing themes to theme based on add_image_size functions with different thumbnail sizes, do I have to reupload all my images to get right sizes? If yes, then using timthumb over add_image_size wasn't that bad idea..

How to set different thumbnail sizes per custom post type?

Registers a new image size. This means WordPress will create a copy of the image with the specified dimensions when a new image is uploaded.
To set the default Featured Image (formerly Post Thumbnail) dimensions use: set_post_thumbnail_size().

Usage

<?php add_image_size$name$width$height$crop ); ?>

if you have multiple post types, maybe this will help
(not tested)(in the loop)

<?php
$post_type = get_post_type(); // get's the post type of a post in the loop
$args=array(
  'public'   => true,
  '_builtin' => false
);
$output = 'names'; // names or objects
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types($args,$output,$operator); // gets all post types you registered
$post_types = array_values($post_types); // makes it a numerically indexed array

if(in_array( $post_type, $post_types)){ // post is a custom post type -> show post type size thumbnail
  $custom_post_thumbnail = get_the_post_thumbnail($post->ID, $post_type.'-thumbnail');
  if($custom_post_thumbnail != ''){ // there is a custom post thumbnail size
    echo $custom_post_thumbnail;
  } else { // no custom post thumbnail size -> show normal thumbnail
    echo get_the_post_thumbnail($post->ID);
  }
} else { // show normal thumbnail for posts and pages
  echo get_the_post_thumbnail($post->ID);
}
?>


You still have to add the image sizes manually in your theme's functions.php

If you have custom post types review and movies:

add_image_size( 'review-thumbnail', 150, 200, true );
add_image_size( 'movies-thumbnail', 400, 9999 );


How to Display Featured Post with Thumbnails Without Plugin in Homepage

On home page, we generally show a fixed number of recent posts with summary. Although, WordPress provides Sticky Posts functionality but not so useful when number of featured posts are more. So I decided to implement this without any plugin.

1. Display featured post with thumbnail.
2. Title will be placed below the thumbnail.
3. Featured posts will be randomly selected and must show default thumbnail if no thumbnail is defined for the post.
4. All selected posts must appear together, no slider to navigate.
5. It must not use any script(Not good for SEO) or plugin.

1. To implement this, First, You need to create a category (say featuredcategory) and add featured posts in the category.
2. Write following code in function.php of your WordPress theme.

<?php
function getFeaturePosts(){
 ?>             
                <ul class="related-posts">
                <?php
                $default_thumbnail = 'http://img.techbrij.com/techbrij%20logo.gif';
                $the_query = new WP_Query('showposts=5&orderby=rand&category_name=featuredcategory');
                while ($the_query->have_posts()) : $the_query->the_post(); ?>
                        <li>
                             <div class="related_thumbnail">
            <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">                           
                            <?php
                            if (has_post_thumbnail()):
                             the_post_thumbnail();
                            else:
                            ?>
                            <img src="<?php echo $default_thumbnail; ?>" alt="<?php the_title(); ?>" />
                            <?php endif; ?>
                            </a>
        </div>
        <div style="clear:both;"></div>
            <div class="related_permalink">
            <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>      
        </div>
                             
                        </li>            
 
                <?php endwhile; ?>
                <?php wp_reset_query(); ?>
                </ul>
<?php
}
 ?>

In the above code, you need to define $default_thumbnail parameter and assign default thumbnail link. If there is no thumbnail then defined $default_thumbnail link will be displayed.
Following line is the most important line.
$the_query = new WP_Query(‘showposts=5&orderby=rand&category_name=featuredcategory’);
showposts=5: define number of posts to be displayed
orderby=rand: posts are randomly selected
category_name=featuredcategory: define category of featured posts.
3. Add following styles in your css file:
?
/* Related OR Feature Posts */
.related-posts {
    list-style:none;
    margin:0;
    padding:0;
         
}
  
.related-posts li {
    display:block;
    text-align:center;
    float:left;
    margin:0 auto;
    padding-left:3px 10px 3px 0px;   
    width:150px;
    height:175px;
}
.related-posts li :hover{
 
}
.related_permalink{  
    text-align:center;
    padding-top:5px;   
    padding-bottom:5px;
margin-bottom:2px
 
}
 .related_permalink a{
color:black;
font-weight:bold;
 text-decoration:none;
 }
 .related_permalink a:hover{
 text-decoration:underline;
 }
.related_thumbnail{
     text-align:center;
}
.related_thumbnail img
{ border-width:0px;
width:100px;
float:none;
margin:0 auto;
}
/* Related Posts End */
you can change thumbnail size by defining height and width in .related_thumbnail img style.
4. Now call method where you want to display featured posts.
?
<?php  getFeaturePosts(); ?>
See following sample display of featured posts:

How to regenerate thumbnails in wordpress

There are times when you may want to change the thumbnail or featured image dimensions in your current WordPress theme or you may have planned to switch to a new theme. In both of these cases, you would feel the need of regenerating thumbnails, compatible to new dimensions. In this post, we have shared a plugin called Regenerate Thumbnails, which allows you to regenerate the new thumbnails after changing the thumbnail sizes in WordPress.


What’s the use of this plugin?

This plugin is very useful if you have changed the thumbnail dimensions (sizes) recently in WordPress (Settings » Media) or if you have switched to a new theme, which is having different thumbnail (featured image) dimensions.

Plugin Configuration

You will be able to configure the plugin's settings for your own particular needs by doing the following:
  1. From the left hand navigation menu, click Tools > Regen. Thumbnails.
  2. On the next screen, click Regenerate All Thumbnails.

The plugin will then regenerate new image sizes as defined by your theme or on the Settings > Media page

Regenerate  Thumbnails by other plugin

find the AJAX Thumbnail Rebuild to be better than the regenerate thumbnails plugin, because it doesn't tie up your server as bad, allows you to skip certain sizes (which saves time), and gives more visual feedback.

Thursday 11 June 2015

How to solve a conflict between a plugin and a theme in wordpress?

The default transition effect between slides is a crossover effect (like this example), but the effect in my slider seems to be image fade to white, then white fades to image. Not sure why this is happening.
Here is the code that calls other scripts:

The original solution (at the bottom) worked in a specific plugin situation (with WP Touch).
I think this is the proper one:
add_action( 'wp_enqueue_scripts', 'wpse_77772_remove_theme_enqueues', 11 );

function wpse_77772_remove_theme_enqueues()
{
    if( is_front_page() || is_home() ) 
    {
        wp_dequeue_script('hatch_pro_fancybox');
        // etc
    }
}

**
 * Proper way to enqueue scripts and styles
 */
function theme_name_scripts() {
 wp_enqueue_style( 'style-name', get_stylesheet_uri() );
 wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

Sunday 7 June 2015

Wordpress Developer Documentation

WordPress is fast, lightweight, and easy to use. To ensure it stays that way, the Core Team thinks carefully about adding functionality to the core WordPress code. Still, users often find the need to graft additional functionality onto WordPress to meet their needs. This section of the Codex offers guidelines and references for anyone wishing to modify, extend, or contribute to WordPress.

Tuesday 2 June 2015

wordpress function cheat sheet



1.Theme Structure

header.php ...................... Header Section
index.php ......................... Main Section
sidebar.php .................... Sidebar Section
single.php ....................... Post Template
page.php ......................... Page Template
comments.php .................. Comment Template
search.php ...................... Search Content
searchform.php ............ Search Form Template
archive.php ................... Archive Template
functions.php ................ Special Functions
404.php .................... Error Page template
style.css .......................... Style Sheet

BlogInfo Tags

<?php bloginfo('name'); ?> ...........................Title of the blog
<?php bloginfo('charset'); ?> ........................The character set
<?php bloginfo('description'); ?> ..........The description of the blog
<?php bloginfo('url'); ?> ......................The address of the blog
<?php bloginfo('rss2_url'); ?> .............................The RSS URL
<?php bloginfo('template_url'); ?> .............The URL of the template
<?php bloginfo('pingback_url'); ?> ....................The pingback URL
<?php bloginfo('stylesheet_url'); ?> The URL for the template's CSS file
<?php bloginfo('wpurl'); ?> .............URL for WordPress installation
<?php bloginfo('version'); ?> ....Version of the WordPress installation
<?php bloginfo('html_type'); ?> ...............HTML version of the site

2.BlogInfo Tags

is_home() .......................When the user is on the blog home page
is_front_page() ......................When the user is on the home page
is_single() ..............................When the single post displayed
is_sticky() ..................................Check if a post is sticky
is_page() .....................................When a page is displayed
is_category() ..............................When a category is displayed


3.Theme Definition

/*
Theme Name: Wordpress Theme
Theme URI: http://wordpress.org/
Description: Test Blog
Version: 1.6
Author: Ekin Ertaç
Author URI: http://themesmart.org
Tags: powerful, cheat, sheet
*/

4.WordPress Template Tags

<?php the_title() ?> .....................Displays the posts/pages title
<?php the_content() ?> ............Displays the content of the post/page
<?php the_excerpt() ?> ....Displays the excerpt of the current post/page
<?php the_time() ?> ..........Displays the time of the current post/page
<?php the_date() ?> .....Displays the date of a post or set of post/page
<?php the_permalink() ?> .............Displays the URL for the permalink
<?php the_category() ?> .................Displays the category of a post
<?php the_author(); ?> ..................Displays the author of the post
<?php the_ID(); ?> ..........Displays the numeric ID of the current post
<?php wp_list_pages(); ?> ........................Displays all the pages
<?php wp_tag_cloud(); ?> ...........................Displays a tag cloud
<?php wp_list_cats(); ?> ........................Displays the categories
<?php get_calendar(); ?> ..........................Displays the calendar
<?php wp_get_archives() ?> ..........Displays a date-based archives list
<?php posts_nav_link(); ?> ...Displays Previous page and Next Page links
<?php next_post_link() ?> .....................Displays Newer Posts link
<?php previous_post_link() ?> ....................Displays previous link
<?php edit_post_link(__('Edit Post')); ?> ........Displays the edit link
<?php the_search_query();?> ................Value for search form
<?php wp_register();?> ................Displays the register link
<?php wp_loginout();?> ..............Displays the log in/out link
<?php wp_meta();?> .......................Meta for administrators
<?php timer_stop(1);?> .....................Time to load the page
<?php echo c2c_custom('test');?> ...... Displays the custom field1
<?php get_links_list(); ?> ...........Display links from Blogroll
<?php get_calendar(); ?> ..........Displays the built-in calendar
<?php comments_popup_link(); ?> .......Link of the posts comments

5.The Loop

<?php if(have_posts());?>
<?php while(have_posts()); the_post();?>
// The Stuff... Custom HTML & PHP Code
<?php else;?>
<?php endif;?>

6.The Category Based Loop

<?php query_posts('category_name=
Category&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
// The Stuff... Custom HTML & PHP Code
<?php endwhile;?>

7.Template Include Tags

< ?php get_header(); ?>
< ?php get_sidebar(); ?>
< ?php get_footer(); ?>
< ?php comments_template(); ?>

8.Navigation Menu

Category Based Navigation
<ul id="menu">
<li <?php if(is_home()) { ?> class="current-cat"< ?php } ?>>
<a href="<?php bloginfo('home'); ?>">Home</a></li>
< ?php wp_list_categories('title_li=&orderby=id'); ?>
</ul>
Pages based Navigation
<ul id="menu">
<li <?php if(is_home()) { ?> class="current_page_item"< ?php } ?>>
<a href="<?php bloginfo('home'); ?>">home</a></li>
< ?php wp_list_pages('sort_column=menu_order&depth=1&title_li='); ?>
</ul>

Popular Articles