0

Add Conditional Styles in WordPress theme

A Standard Conditional IE comment looks like this
<!--[if lt IE 9]>
<link href="style.css" rel="stylesheet" type="text/css" />
<![endif]-->

You can easily code this in your header.php file and it will work fine but it’s not a good practice to do so. A better option is to use the functions and hooks provided by WordPress. You can add the following code to your functions.php file

add_action( 'wp_enqueue_scripts', 'enqueue_conditional_style' );
function enqueue_conditional_style() {

global $wp_styles;
$wp_styles->add(‘example_ie7-‘, ‘/themes/example/ie7-.css’);
$wp_styles->add_data(‘example_ie7-‘, ‘conditional’, ‘lte IE 7’);
$wp_styles->enqueue(array(‘example_ie7-‘));

}

0

Pagination – Same list of posts in every page issue !

As per Wikipedia, Pagination is the sequence of numbers assigned to pages in a book or periodical.

In WordPress, we generally use WP_Query or query_posts to fetch posts and display the list with pagination. A simple query would look like this:

<?php query_posts('posts_per_page=3'); ?>

This will work just fine in most of the cases but sometimes we hear issues that the pagination is not working properly or pagination numbers are correct but every page shows the same list. It’s mostly because we haven’t included the ‘paged‘ parameter to the query. The following is taken from WordPress Codex.

you add the parameter like this:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts('posts_per_page=3&paged=' . $paged); 
?>

The next example is exactly the same as above but with the parameters in an array:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
  'posts_per_page' => 3,
  'paged' => $paged
);

query_posts($args); 
?>

Detailed information about how to properly configure and work with WordPress Pagination can be found in the WordPress Codex.

0

Shortcode, Beware of them

shortcodeSince the the release of WordPress 2.5 Shortcodes have been really popular and developers and user love it. Shortcodes have been extensively used to create a visually good looking content in WordPress.

Basically Shortcodes converts multiple lines of codes that performs specific task to a small WordPress specific code. Shortcodes looks like this [shortcode] or if wrapped with contents may look like [shortcode] Some Content Here [/shortcode].

Shortcodes have been used for multiple purposes like

  • displaying recent posts
  • Creating multiple columns
  • Create a  blockquote
  • Create tabs and accordions
  • Simple Tabs
  • Display Twitter Feed, Etc and many more.

So why this article? Is Shortcode Bad?

No, Shortcodes are really useful and it has brought many easy solutions for developers and users. But, if shortcodes are setup directly in theme, then it might create problem in future. This is because when we implement shortcodes in a theme directly, it becomes available to that particular theme only. If we change to another theme then all of the shortcodes will be unavailable and all we will be left is texts and visually it will look really bad. All the styles that came with shortcodes will be gone, if you had setup tabs and accordions will be gone. if you were displaying recent posts, you will only get something like this [recent-posts].

When creating shortcodes we should keep in mind all the factors that might affect the site if we use the shortcode. The best solution of this would be creating a separate plugin for the shortcodes with all the necessary styles and functions. This will make the shortcode independent of the theme and easily be run and implemented without hampering the site’s look even if the theme is changed.

Some good available plugins for Shortcodes are:

But in the end it’s up to you to decide how to use the shorcodes in the best way possible. Happy Coding 🙂

0

Custom PHP file in WordPress Theme

WordPress has proved to be one of the best platform for Developers and it provides a complete package for building a full Website. But sometimes we developers may need to add one or more custom PHP pages for various reasons.

Suppose you have a template file for your contact page and you built a custom form which when submitted will direct to send-contact-mail.php file and then that file will be used for form data manipulation and send mail. Here, you can easily use the php mail() function easily and achieve your goal but what if you want to use the WordPress actions in the file?

By default, your send-contact-mail.php is not recognized by WordPress and you cannot use any of the WordPress functions like get_bloginfo(‘url’); or in our case wp_mail(); function.

So What Do We Do?

The solution is quite easy. All we need is to include the wp-load.php file in our custom php page. The wp-load.php is location in the root of the site and it will make your custom php file compatible with WordPress functions.

require_once('../../../wp-load.php');

However, there are many critics on including wp-load.php file. When you don’t know where the wp-load.php file is, it creates problem when including the file.

For more detailed information you can visit