8/9/2008 ↓

If, Then, Else 33comments

If you like this post, please subscribe to our RSS feed to read our new posts every day.

Ever become brave enough to look inside of a WordPress php file? Specifically, a php file from a theme? If you have, you may have noticed that they are filled with If, Then, and Else statements. If you have no idea what these mean, this post is for you.

Php has always been a pain for me to understand, but if you think about it in a logical manner, it begins to make sense. If, then and else statements are used as a means of making decisions, similar to the way you and I make decisions in the real world. Here is an example of how this logic works in WordPress.

< ?php if (have_posts()) : ?>

The if statement within that php function is asking if there are posts. If the blog has posts, WordPress begins processing what is known as “The Loop“.

<? php while (have_posts()) : the_post(); ?>

This piece of code tells WordPress to grab the posts from the MySQL database and display them on your blog. The loop then closes with this function.

<?php endwhile; ?>

If the answer to the if question turns out to be no, WordPress bypasses the loop and displays a message that no posts exist. This is where the else statement comes into play.


< h2 class=”center”>Not Found< /h2>
< p class=”center”>Sorry, but you are looking for something that isn’t here.< /p>
< ?php include (TEMPLATEPATH . “/searchform.php”); ?>
< ?php endif; ?>

That last line of code is the if statement and concludes the function.

If, then, and else are easy to remember when you think of how you function on a daily basis. A real world example, (If I am hungry, Then I’ll eat some food, or Else I’ll starve). Maybe not the best example in the world, but you should be able to get the picture.

I’m no coder by any means but when I read explanations about statements and functions inside of WordPress via Lisa Sabin Wilson’s WordPress For Dummies, it’s as if light bulbs turn on inside of my head. One of those “Ohhhh, so thats what that does” sort of moments.

Be sure to let me know if this post was of any help to you.

1/23/2007 ↓

DiggProof your Wordpress 1comment

DiggProof your Wordpress A close look at optimizing and tweaking your MySql database for Wordpress and other tips and plugins to make your Wordpress blog faster than it already is. Even though Wordpress is fast in its own right, this article is geared for high traffic blogs and especially those that run Wordpress on smaller Virtual Private Servers.

While we are on the subject of optimizations, the newly released Wordpress 2.1 Ella has introduced many code and query optimizations that should make Wordpress much faster than it already was. For example, one of the changes introduced to handle future posts gives the database the ability to cache more queries at the MySql level and should reduce load on the database server (however minuscule).

12/23/2005 ↓

  • Wordpress Theme Zip for Lazy Wordpress Theme Authors

    Wordpress Theme Zip: easiest (and laziest) way to generate a zip archive from a Wordpress Theme that is being worked on, so you don’t have to manually create, update and upload a new zip file every time you modify a file in it. (0)

12/14/2005 ↓

  • Variables in your CSS via PHP

    Variables in your CSS via PHP: Good writeup/tutorial on using PHP to control the way your page looks. I love the way Chris writes, but this article is even more poignant as it also talks very candidly about OOP in php (which is sorely lacking in larger percentage of all PHP code) (0)

10/19/2005 ↓

  • Free eBooks from Apress

    Free eBooks from Apress Including “A Programmer’s Introduction to PHP 4.0″, “Writing Perl Modules for CPAN” and “COM and .NET Interoperability”. Thanks again Slickdeals (5)

Friends

Translate

Translate to German Translate to Spanish Translate to French Translate to Italian Translate to Portuguese Translate to Japanese Translate to Korean Translate to Russian Translate to Chinese

Latest Videos

S2