Thursday, 21 February 2013

Wordpress pagination

<?php
$catname = wp_title('', false);
$wp_query = new WP_Query();
$wp_query->query('category_name='.$catname.'&showposts=5'.'&paged='.$paged);
?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php the_title(); ?>
<?php } ?>

<?php endwhile; ?>

<?php next_posts_link('&laquo; Older Entries') ?>
<?php previous_posts_link('Newer Entries &raquo;') ?>

Wordpress thumbnails

http://codex.wordpress.org/Post_Thumbnails

Post Thumbnails

Post Thumbnail is a theme feature introduced with Version 2.9. It was quickly changed to Featured Image with Version 3.0. Post Thumbnail, now Featured Image, is an image that is chosen as the representative image for Posts, Pages or Custom Post Types. The display of this image is up to the theme. This is especially useful for "magazine-style" themes where each post has an image.

Contents

[hide]

Enabling Support for Post Thumbnails

Themes have to declare their support for post images before the interface for assigning these images will appear on the Edit Post and Edit Page screens. They do this by putting the following in their functions.php file:
add_theme_support( 'post-thumbnails' ); 
Note: To enable Post Thumbnails only for specific post types see add_theme_support()

Setting a Post Thumbnail

If your theme was successful in adding support for Post Thumbnails the "Featured Image" metabox will be visible on the on the Edit Post and Edit Page screens. If it isn't, make sure "Featured Image" is enabled in the screen options on the top right.
Featured Image Metabox
Featured image metabox
After clicking the "Set featured image" link follow the same steps as inserting images in Posts and Pages. But instead of the "Insert into Post" button use the "Use as featured image" link next to it, to set the Post Thumbnail.
image insert button
Insert image button

Function Reference

Template Tags
Other Functions

Examples

Default Usage

// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
 the_post_thumbnail();
} 
the_content();
Note: To return the Post Thumbnail for use in your PHP code instead of displaying it, use: get_the_post_thumbnail()

Linking to Post or Larger Image

To link the Post Thumbnail to the Post permalink or a larger image see the examples in the_post_thumbnail()

Thumbnail Sizes

The default image sizes of WordPress are "thumbnail", "medium", "large" and "full" (the image you uploaded). These image sizes can be configured in the WordPress Administration Media panel under Settings > Media. This is how you use these default sizes with the_post_thumbnail():
the_post_thumbnail();                  // without parameter -> Thumbnail

the_post_thumbnail('thumbnail');       // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium');          // Medium resolution (default 300px x 300px max)
the_post_thumbnail('large');           // Large resolution (default 640px x 640px max)
the_post_thumbnail('full');            // Original image resolution (unmodified)

the_post_thumbnail( array(100,100) );  // Other resolutions

Set the Post Thumbnail Size

To be used in the current Theme's functions.php file.
Set the default Post Thumbnail size by resizing the image proportionally (that is, without distorting it):
set_post_thumbnail_size( 50, 50 ); // 50 pixels wide by 50 pixels tall, resize mode
Set the default Post Thumbnail size by cropping the image (either from the sides, or from the top and bottom):
set_post_thumbnail_size( 50, 50, true ); // 50 pixels wide by 50 pixels tall, crop mode

Add New Post Thumbnail Sizes

Example of a new Post Thumbnail size named "category-thumb".
To be used in the current Theme's functions.php file.
add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
Here is an example of how to use this new Post Thumbnail size in theme template files.
<?php the_post_thumbnail( 'category-thumb' ); ?>

Example of functions.php

if ( function_exists( 'add_theme_support' ) ) { 
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 150, 150, true ); // default Post Thumbnail dimensions (cropped)

// additional image sizes
// delete the next line if you do not need additional image sizes
add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
}

Styling Post Thumbnails

Post Thumbnails are given a class "wp-post-image". They also get a class depending on the size of the thumbnail being displayed You can style the output with these CSS selectors:
img.wp-post-image
img.attachment-thumbnail
img.attachment-medium
img.attachment-large
img.attachment-full
You can also give Post Thumbnails their own class.
Display the Post Thumbnail with a class "alignleft":
<?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?>

Wednesday, 20 February 2013

Query string in PHP URL's

http://ditio.net/2008/06/12/php-query-string/

Query string plays important role in building web applications, especially if you want to make nice urls without question mark and many key, value pairs, actually it is not so easy to design application with nice urls however it is always worth doing so, because it not only looks very proffessional, such URLs are search engine friendly, which means that they will get indexed faster but also it happens that search engines have problems with indexing pages with more then 3 key=value pairs in query string.
However, one place where using nice URLs is not necessary are all kinds of admin panels, there are usually only few admins and more over admin panel never gets indexed so it doesn’t make any sense to make those URLs search engine friendly.
Query string can be accessed thru global array $_SERVER, more specific $_SERVER['QUERY_STRING'] is the actual variable where query string is written, this variable contains all data that is inputed after question mark in the URL. For example if we have URL which looks like this:
http://someurl.com/page.php?a=1&b=2&c=3
Then echo $_SERVER['QUERY_STRING'] will display: a=1&b=2&c=3, such data is no use to us, we need to parse it, or get it thru global array $_GET, in our case we could write:
echo $_GET['a'];
echo $_GET['b'];
echo $_GET['c'];
which would output:
1
2
3
Two things i have to mention here:
- i do not recommend displaying variables passed by user withut checking if they contain potentially dangerous code.
- make sure you have register_globals set to off in your ini file. If you do not have access to ini file, you can change this setting in your .htaccess file (if you have htaccess files working on your server). To do this just add following line php_flag register_globals off.
Using $_GET array to access variables from query string is pretty simple, however we would want to transform our URL to make it look like this:
http://someurl.com/1/2/3
This is very search engine friendly approach, however like always there are no roses without thorns. First problem is that $_SERVER['QUERY_STRING'] variable is empty now, so $_GET array is empty as well.
But first thing first, we can’t just input this URL in web browser address bar, hit enter and wait for a page to load. Probably it would end with 404 Error. To avoid this we need to create .htaccess file in the same directory as page.php file. Then open .htaccess file and write in it:
RewriteEngine on
RewriteRule .* page.php
Now all requests to directory which contains file page.php will be redirected to page.php file. Pretty simple and we can now use URL without question mark or any other unwanted signs i provided earlier (e.g. http://someurl.com/1/2/3).
So as i said earlier now our $_GET array is empty, but fortunately we can still check what the URL looks like by writing this code:
echo $_SERVER['REQUEST_URI'];
// output /1/2/3
Well, maybe URL looks pretty search engine friendly however id does not provide a lot of info as it did earlier. Unfortunately solution to this problem is beyond the scope of this article and i will give you only few guidlines: first you need to parse URL wit explode() to get array, then it will be good to assign some keys to each of the variable, but you always need to remember who is who, do not change order of parameters in the URL.
I covered only two types passing arguments to application with query string, there are other solutions as well, but i think that this two are the most common used and are actually the best when it comes to PHP.

Monday, 18 February 2013

leaf year programme for php and jquery

<html>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script>
$(document).ready(function(){
    //alert('readyfunciton');
    //Month value changed
    $("#MM").change(function(){
        var selectVal = $('#MM :selected').val();
        //alert(selectVal);
        if(selectVal == '2'){
            $("#DD option[value='29']").attr('hidden','hidden');   
            $("#DD option[value='30']").attr('hidden','hidden');
            $("#DD option[value='31']").attr('hidden','hidden');
        }else if(selectVal == '4' || selectVal == '6' ||selectVal == '9'|| selectVal == '11'){
            $("#DD option[value='29']").removeAttr("hidden");   
            $("#DD option[value='30']").removeAttr("hidden");
            $("#DD option[value='31']").attr('hidden','hidden');           
        }else{
            $("#DD option[value='29']").removeAttr("hidden");   
            $("#DD option[value='30']").removeAttr("hidden");
            $("#DD option[value='31']").removeAttr("hidden");
        }               
    });
    $("#YYYY").change(function(){
        var yearVal = $('#YYYY :selected').val();       
        var monthVal = $('#MM :selected').val();
        if( ((yearVal%4 == 0 && yearVal%100  !== 0) && monthVal == 2)|| yearVal%400 == 0)){
            $("#DD option[value='29']").removeAttr("hidden");                   
            $("#DD option[value='30']").attr('hidden','hidden');
            $("#DD option[value='31']").attr('hidden','hidden');
                       
        }   
    });
  
});
</script>
<body>
<select name="MM" id="MM">
<option>MM</option>
<?php
$MM = array('1' => 'JAN',
            '2' => 'FEB',
            '3' => 'MAR',
            '4' => 'APR',
            '5' => 'MAY',
            '6' => 'JUN',
            '7' => 'JUL',
            '8' => 'AUG',
            '9' => 'SEP',
            '10' => 'OCT',
            '11' => 'NOV',
            '12' => 'DEC');
foreach ($MM as $key => $value){
    echo '<option value ="'.$key.'">'.$value.'</option>';
}
?>
</select>
<select name="DD" id="DD">
<option>DD</option>
<?php
for ($i = 1; $i <= 31; $i++) {
    echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select>
<select name="YYYY" id="YYYY">
<option>YYYY</option>
<?php
for ($i = 1960; $i < 2020; $i++) {
    echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select>
</body>
</html>