subcategory template in word press

function new_subcategory_hierarchy() {  
    $category = get_queried_object();

$parent_id = $category->category_parent;

$templates = array();

if ( $parent_id == 0 ) {
    // Use default values from get_category_template()
    $templates[] = "category-{$category->slug}.php";
    $templates[] = "category-{$category->term_id}.php";
    $templates[] = 'category.php';      
} else {
    // Create replacement $templates array
    $parent = get_category( $parent_id );

    // Current first
    $templates[] = "subcategory-{$category->slug}.php";
    $templates[] = "subcategory-{$category->term_id}.php";
    $templates[] = "subcategory.php";

    // Parent second
    $templates[] = "category-{$parent->slug}.php";
    $templates[] = "category-{$parent->term_id}.php";
    $templates[] = 'category.php';  
}
    return locate_template( $templates );
}



add_filter( 'category_template', 'new_subcategory_hierarchy' );

Now you can user subcategory-slug.php or subcategory-id.php for specific subcategories

or subcategory.php in general for all subcategories

A SIMPLE SHORTCODE – WORDPRESS

In version 2.5 WordPress introduced shortcodes, and all of us have probably used them at one time or another. They usually come bundled with plugins, or even themes, and what they do is watch for when you insert something inside square brackets then replace that with some other content; it could be a simple sentence or it could be a massive PHP function, it all depends on what you instructed WordPress to do.

Bundled shortcodes are great, and speed up things considerably, but wouldn’t it be great to know how to create shortcodes of your own?

In this article I’ll take you through creating some simple WordPress shortcodes to help you create any functionality you like.

A SIMPLE SHORTCODE

The shortcode API works very simply: first you need to create a callback function that will run anytime the shortcode is used; then you need to tie that function to a specific shortcode making it ready for use. The code is frequently placed in the functions.php file, but if you plan on having a lot of shortcodes, it makes sense to create a separate file and include that file in your functions.php file.

In our first example we want to create a shortcode that will create some lorem ipsum every time we type [lorem] into the editor. First we need to create the callback function that will return the lorem ipsum (in shortcodes we don’t echo anything, everything is returned):

function lorem_function() {
  return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec nulla vitae lacus mattis volutpat eu at sapien. Nunc interdum congue libero, quis laoreet elit sagittis ut. Pellentesque lacus erat, dictum condimentum pharetra vel, malesuada volutpat risus. Nunc sit amet risus dolor. Etiam posuere tellus nisl. Integer lorem ligula, tempor eu laoreet ac, eleifend quis diam. Proin cursus, nibh eu vehicula varius, lacus elit eleifend elit, eget commodo ante felis at neque. Integer sit amet justo sed elit porta convallis a at metus. Suspendisse molestie turpis pulvinar nisl tincidunt quis fringilla enim lobortis. Curabitur placerat quam ac sem venenatis blandit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nullam sed ligula nisl. Nam ullamcorper elit id magna hendrerit sit amet dignissim elit sodales. Aenean accumsan consectetur rutrum.';
}

Next we need to add this shortcode to WordPress using the add_shortcode function in either our functions.php file or a file that’s being included in it, this function adds the shortcode and also ties it to the function we just created. add_shortcode only takes two arguments, the first one being the name we want this shortcode to have (what we will type between the square brackets) and the second one being the function we want to attach to that shortcode:

add_shortcode('lorem', 'lorem_function');

That is all it takes to create a simple shortcode in WordPress.

ADDING PARAMETERS

Continuing with this dummy content idea, we often need images in our content when we preparing our mockups and these images need to be different sizes, so now we’ll create a shortcode to insert an image like this:

[picture width="500" height="500"]

When WordPress encounters this we want a function that will insert an image. It needs to read the width and height attributes, but just in case we’ll also provide default values so that it can be used without the attributes. Because we may not have an image available, we’re going to use the lorempixel.com service to provide us with a random image.

First we need to create the function:

function random_picture($atts) {
   extract(shortcode_atts(array(
      'width' => 400,
      'height' => 200,
   ), $atts));
return '<img src="http://lorempixel.com/'. $width . '/'. $height . '" />';
}

We named this function random_picture and since this shortcode will be able to take arguments we gave it the $atts parameter. In order to use the attributes we need two functions: the shortcode_atts which is a WordPress function that combines our attributes with known attributes and fills in defaults when needed; and the extractPHP function which, as the name suggests, extracts those attributes we set for our shortcode. Finally the function returns the value we want, in this case the HTML code for our image combined with the width and height variables.

The only thing left to do is register this shortcode:

add_shortcode('picture', 'random_picture');

Our shortcode is complete, when we type [picture] it will give us a random image 400 by 200, and if we use the attributes we can create an image of any size we please.

CONCLUSION

Creating little shortcodes for things we use frequently definitely helps us when writing blog posts because you can do anything you please with shortcodes, it can be as simple as returning a sentence, or as complex as adding a form or the latest posts sorted by month.

What can i do with php?

For this question most of us will shout loud that we can create websites with php and some geeks  would say we can do server side scripting.

But is that the only thing php can do?

PHP can be used for the following 3 purposes.

  1. Server Side Scripting for your websites
  2. Command line scripting, you can run php on your terminal (linux) or command prompt (in windows)
  3. Writing desktop applications- you can use PHP-GTK to create desktop applications.

The second and third options may come as a surprise to most of the people. Keep in mind PHP is not just a Server side scripting language

Top 13 Alternatives to WhatsApp Messenger

1. Viber

Viber Allows users to connect the world using Text messages, make free calls and share photos,videos for free across the world. More than 200 million Viber users text, call, and send photo and video messages worldwide over Wifi or 3G – for free. On Viber, your phone number is your ID. The app syncs with your mobile contact list, automatically detecting which of your contacts have Viber.

Download for Android   Download for iOS

2. WeChat

With WeChat, users can send voice messages and instant messages and also make crystal clear video calls. The message alerts are displayed with push notifications and users can import contacts instantly. Its allow Instant messaging with group chats and animated smileys. you can Chat with your friends or with people nearby with stunning features like Sending photos and videos has never been simpler. its main feature is Real time walkie talkie mode with up to 40 friends.

Download for Android   Download for iOS

3. Nimbuzz

Nimbuzz Messenger combines the power of internet and smart phone messenger into one, and lets you make free video calls, voice calls, send chat messages, share files, on any mobile device across popular messengers. Nimbuzz Messenger is available on iOS, Android, Blackberry, Symbian, Java, Windows and Mac.

Download for Android   Download for iOS

4. Line

LINE is a new communication app which allows you to make FREE voice calls and send FREE Emoji messages whenever and wherever you are, 24 hours a day! LINE has more than 210 million users worldwide and is used in over 231 countries! All Line users can enjoy free high quality voice calls and send messages with colorful icons, photos and location details as well.

Download for Android  Download for iOS

5. KakaoTalk Messenger

KakaoTalk Messenger Allows users to make FREE calls and text. Make group calls in high-quality sound and schedule appointments in your chat room. Download now and enjoy animated emoticons, stickers, and themes – completely free!

Download for Android   Download for iOS

6. Kik Messenger

Kik Messenger is one of the simplest free, cross-platform messaging apps around. 50 million users love Kik! It’s the fast, simple, and personal smartphone messenger that connects you to everyone you love to talk to.

Download for Android   Download for iOS

7. Tango

Tango gives you the highest quality video & phone calls, texts, photo & video sharing, games and more for FREE! It’s the all-in-one messaging app that finally combines every way you want to interact with friends & family.  130M people around the world using  Tango today.

Download for Android   Download for iOS

8. Imsy

Imsy allows you to message your friends for free. Have lightning fast, real-time chat and group messaging with friends; add emoticons and relevant images to your messages based on suggestions from Imsy.

Download for Android   Download for iOS

9. LiveProfile

LiveProfile helps you stay connected with friends in real-time. It’s the crazy fast, completely free, cross-platform messenger for all smartphones. Send messages, photos, videos, post status updates, customize your profile, set your profile picture, and much more. It has no calling features but it does have standard messaging features with the abilty to start a group chat and send pictures or videos.

Download for Android   Download for iOS

10. GroupMe

GroupMe is the best way to chat with every friends. It’s free, whether you’re talking to a group of friends, or texting with one person. Best of all, it works on nearly every phone, with a smartphone app or via SMS. With GroupMe, it’s easy to reach anyone, anytime, anywhere.

Download for Android   Download for iOS

11. Facebook Messenger

Facebook Messenger allows users to Text your friends for free. Get Facebook Messenger to instantly reach friends on their smart phones, feature phones or desktop computers.

Download for Android   Download for iOS

12. ChatON

Create a global social community with over 1 billion friends who have ChatON through a Samsung account. Users can choose to sign in to the app with the Samsung account or just by entering their name. Share your life with friends using Samsung’s global communication service, ChatON!

Download for Android   Download for iOS

13. Skype

Skype is one of the best ways to make calls for free. Say “hello” to friends and family with an instant message, voice or video call on Skype for free. There’s so much you can do, right from the palm of your hand.

Download for Android   Download for iOS

Generators 

Support for generators has been added via the yield keyword. Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface.

A simple example that reimplements the range() function as a generator (at least for positive stepvalues):

<?php
function xrange($start, $limit, $step = 1) {
for ($i = $start; $i <= $limit; $i += $step) {
yield $i;
}
}

echo ‘Single digit odd numbers: ‘;

/*
* Note that an array is never created or returned,
* which saves memory.
*/
foreach (xrange(1, 9, 2) as $number) {
echo “$number “;
}

echo “\n”;
?>

The above example will output:

Single digit odd numbers: 1 3 5 7 9 

finally keyword added 

try-catch blocks now support a finally block for code that should be run regardless of whether an exception has been thrown or not.

New password hashing API

new password hashing API that makes it easier to securely hash and manage passwords using the same underlying library as crypt() in PHP has been added. See the documentation for password_hash() for more detail.

foreach now supports list()

The foreach control structure now supports unpacking nested arrays into separate variables via the list()construct. For example:

<?php
$array = [
[1, 2],
[3, 4],
];

foreach ($array as list($a, $b)) {
echo “A: $a; B: $b\n”;
}
?>

The above example will output:

A: 1; B: 2
A: 3; B: 4

Further documentation is available on the foreach manual page.

empty() supports arbitrary expressions 

Passing an arbitrary expression instead of a variable to empty() is now supported. For example:

<?php
function always_false() {
return false;
}

if (empty(always_false())) {
echo “This will be printed.\n”;
}

if (empty(true)) {
echo “This will not be printed.\n”;
}
?>

The above example will output:

This will be printed.

array and string literal dereferencing ¶

Array and string literals can now be dereferenced directly to access individual elements and characters:

<?php
echo 'Array dereferencing: ';
echo [1, 2, 3][0];
echo "\n";

echo ‘String dereferencing: ‘;
echo ‘PHP'[0];
echo “\n”;
?>

The above example will output:

Array dereferencing: 1
String dereferencing: P

Class name resolution via ::class ¶

It is possible to use ClassName::class to get a fully qualified name of class ClassName. For example:

<?php
namespace Name\Space;
class ClassName {}

echo ClassName::class;

echo “\n”;
?>

The above example will output:

Name\Space\ClassName

OPcache extension added ¶

The Zend Optimiser+ opcode cache has been added to PHP as the new OPcache extension. OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request. See the installation instructions for more detail on enabling and using OPcache.

foreach now supports non-scalar keys ¶

foreach now supports keys of any type. While non-scalar keys cannot occur in native PHP arrays, it is possible for Iterator::key() to return a value of any type, and this will now be handled correctly.

Apache 2.4 handler supported on Windows ¶

The Apache 2.4 handler SAPI is now supported on Windows.

Improvements to GD ¶

Various improvements have been made to the GD extension, these include:

A SIMPLE SHORTCODE – WORDPRESS

In version 2.5 WordPress introduced shortcodes, and all of us have probably used them at one time or another. They usually come bundled with plugins, or even themes, and what they do is watch for when you insert something inside square brackets then replace that with some other content; it could be a simple sentence or it could be a massive PHP function, it all depends on what you instructed WordPress to do.

Bundled shortcodes are great, and speed up things considerably, but wouldn’t it be great to know how to create shortcodes of your own?

In this article I’ll take you through creating some simple WordPress shortcodes to help you create any functionality you like.

A SIMPLE SHORTCODE

The shortcode API works very simply: first you need to create a callback function that will run anytime the shortcode is used; then you need to tie that function to a specific shortcode making it ready for use. The code is frequently placed in the functions.php file, but if you plan on having a lot of shortcodes, it makes sense to create a separate file and include that file in your functions.php file.

In our first example we want to create a shortcode that will create some lorem ipsum every time we type [lorem] into the editor. First we need to create the callback function that will return the lorem ipsum (in shortcodes we don’t echo anything, everything is returned):

function lorem_function() {
  return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec nulla vitae lacus mattis volutpat eu at sapien. Nunc interdum congue libero, quis laoreet elit sagittis ut. Pellentesque lacus erat, dictum condimentum pharetra vel, malesuada volutpat risus. Nunc sit amet risus dolor. Etiam posuere tellus nisl. Integer lorem ligula, tempor eu laoreet ac, eleifend quis diam. Proin cursus, nibh eu vehicula varius, lacus elit eleifend elit, eget commodo ante felis at neque. Integer sit amet justo sed elit porta convallis a at metus. Suspendisse molestie turpis pulvinar nisl tincidunt quis fringilla enim lobortis. Curabitur placerat quam ac sem venenatis blandit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nullam sed ligula nisl. Nam ullamcorper elit id magna hendrerit sit amet dignissim elit sodales. Aenean accumsan consectetur rutrum.';
}

Next we need to add this shortcode to WordPress using the add_shortcode function in either our functions.php file or a file that’s being included in it, this function adds the shortcode and also ties it to the function we just created. add_shortcode only takes two arguments, the first one being the name we want this shortcode to have (what we will type between the square brackets) and the second one being the function we want to attach to that shortcode:

add_shortcode('lorem', 'lorem_function');

That is all it takes to create a simple shortcode in WordPress.

ADDING PARAMETERS

Continuing with this dummy content idea, we often need images in our content when we preparing our mockups and these images need to be different sizes, so now we’ll create a shortcode to insert an image like this:

[picture width="500" height="500"]

When WordPress encounters this we want a function that will insert an image. It needs to read the width and height attributes, but just in case we’ll also provide default values so that it can be used without the attributes. Because we may not have an image available, we’re going to use the lorempixel.com service to provide us with a random image.

First we need to create the function:

function random_picture($atts) {
   extract(shortcode_atts(array(
      'width' => 400,
      'height' => 200,
   ), $atts));
return '<img src="http://lorempixel.com/'. $width . '/'. $height . '" />';
}

We named this function random_picture and since this shortcode will be able to take arguments we gave it the $atts parameter. In order to use the attributes we need two functions: the shortcode_atts which is a WordPress function that combines our attributes with known attributes and fills in defaults when needed; and the extractPHP function which, as the name suggests, extracts those attributes we set for our shortcode. Finally the function returns the value we want, in this case the HTML code for our image combined with the width and height variables.

The only thing left to do is register this shortcode:

add_shortcode('picture', 'random_picture');

Our shortcode is complete, when we type [picture] it will give us a random image 400 by 200, and if we use the attributes we can create an image of any size we please.

CONCLUSION

Creating little shortcodes for things we use frequently definitely helps us when writing blog posts because you can do anything you please with shortcodes, it can be as simple as returning a sentence, or as complex as adding a form or the latest posts sorted by month.