Related Articles

WordPress runs on PHP and MySQL. PHP is a server-side programming langauge that runs on a Web server before the page is sent to a user's browser. For this reason, when you right-click on the browser and select to view source code, you will only see HTML that the PHP code outputs. WordPress uses PHP to communicate with its MySQL database. It also comes with a large set of pre-coded PHP functions – reusable blocks of code you can call by name – that allow you to build templates with only a basic grasp of PHP programming.

1. Call the header, sidebar and footer templates into the blog and page templates. Together, these pieces will build the pages for your blog: <?php get_header(); ?> <?php get_sidebar(); ?> …content, post loop, etc... <?php get_footer(); ?> These tags go wherever you need to place each part. In most cases, the header call goes first, followed by the divs and loops that make up the main code for the page. The sidebar call can go before or after the the main code. A call to the footer usually goes last.

2. Use the “bloginfo()” function to output information about the blog in any template. The function is most often seen in the header, where it outputs the blog name: <h1><?php bloginfo('name'); ?></h1> The word “name” is placed inside a set of quotes within the parentheses that follow the function name. Quote marks around “name” tell the server to process this as text, rather than a variable that holds information or another data type. In this case, “name” is one of the parameters you can use with “bloginfo().” Other parameters include “description” for the tagline and “wpurl” for the blog URL.

3. Add the “loop” to a template where you want post or page content to display: <?php if(have_posts()) : while(have_posts()) : the_post(); ?> ...template tags go here... <?php endif(); ?> WordPress uses this code to do two things: query the database for content and put it through a “while” loop for as long as there is content to output. When you click on a post title in the blog, for example, WordPress knows to query for that post and output just that. When on the main blog page, WordPress knows to output all posts.

4. Put template tags between the first and last lines of your WordPress loop. WordPress calls many of its built-in PHP functions “template tags,” and these tags output information from the database: <?php if(have_posts()) : while(have_posts()) : the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_content(); ?> <?php endif(); ?> Above is a simplified version of a WordPress loop that loops through posts and outputs the title of each post within level-two heading tags, followed by the post content.

5. Wrap conditional tags inside “if-then” statements. WordPress conditional tags work like template tags, but instead of outputting information, these tags check if a condition is true or not: <?php if(is_front_page()) : ?> Do something if this is the front page... <?php endif; ?> In this example, “is_front_page()” is the conditional tag. Use “is_page()” to check if content is a page instead of a post or use “is_page('about')” to check whether the template is showing content from your “About” page.

6. Write your custom PHP functions inside the “functions.php” file of your theme. Here the format for a very basic PHP function: function_name() { echo '<h2>Hello, world!</h2>'; } When you add “<?php function_name(); ?>” to a template, the output will be “Hello, world!”

7. Escape in and out of PHP within a function using the PHP delimiter tags: function_name() { ?> <h2>Hello, world!</h2> <?php } This code has the same effect as the example that used an “echo” statement.

8. Switch between single and double quotes when outputting HTML in an “echo” statement: echo '<a href=”http://yourlink.com”>Your Link</a>'; Since the “href” attribute in anchor tags needs quotation marks, use single quotes around the entire string of text that “echo” outputs. The “echo” statement will output this code into the HTML of the page, so on the screen it will look like a regular text link. This is also correct: echo "<a href='http://yourlink.com'>Your Link</a>";

Warning

Back up your theme files before editing them, especially when editing their PHP code. Errors in PHP may lock you out of WordPress until you re-upload the original theme files.

About the Author

Sara Williams lives in western New York, where she is a freelance Web designer and content writer. She specializes in Web design, development and computer-hardware topics. Williams holds an Associate of Applied Science in computer information systems.