Wordpress provides a host of PHP functions that output information store in your database. Here are some of the most common functions and some examples. Much of the information is taken from the Wordpress Codex’s article Stepping Into Template Tags, which you should take a look at.
Functions that Can Go Anywhere
Some of Wordpress’s functions can be used anywhere on the page: in the header or footer, inside a Wordpress loop or outside of it. The function bloginfo()
is probably the most common:
bloginfo()
This is a function that takes a keyword as an argument and outputs info about your site, most of which are controlled by the settings under Settings > General. This is best shown through examples:
<?php
bloginfo('name'); // echoes "James's Blog"
bloginfo('description'); // echoes "A Website about Design"
bloginfo('url'); // echoes "http://watkinswebdev.com/jmuspratt/demo/"
bloginfo('template_directory'); // echoes "http://watkinswebdev.com/jmuspratt/demo/wp-content/themes/starterkit/"
?>
It’s important to note that in the last two examples, the function outputs a raw URL. If you want to link to the page at that URL, it’s up to you to wrap that URL in a link tag and open and close PHP correctly. So if we want to construct a link that goes to the home page, think about it as three logical steps, working backwards from the output you want to create with PHP:
- If you were manually writing a link to the home page, it would look like
<a href="http://watkinswebdev.com/jmuspratt/demo/">Visit the home page.</a>
- Since
bloginfo('url');
only outputs the URL of the blog, you’re going to have to turn PHP on and off inside the quotation marks of the href
attribute. In other words, you’ll do something like this: <a href="PHP_GOES_HERE">Visit the home page.</a>
- Since you’re starting in HTML, you’ll need to open PHP, output the URL, and then close PHP inside the quotation marks. So the final code should look like this:
<a href="<?php bloginfo('url'); ?>">Visit the home page.</a>
For the complete list of options, visit Wordpress Codex: bloginfo().
Inside the Loop
bloginfo()
works outside the loop because its information is consistent no matter what post or page the user is viewing. Since the functions that output the contents of a post obviously do differ from post to post, they must be used inside the Loop. In Starter Kit, you can open single.php
and the file snippet-post.php
for a good example of what follows below.
the_title()
To echo the title of each post, use the_title()
. Note that Wordpress provides a few optional parameters illustrated by the examples below. The first two parameters represent opening and closing HTML tags that Wordpress can add to the title, while the third is a boolean value called “display” which controls whether to echo or return the title. If you override the default TRUE
value by specifying FALSE
, you can use the function to store the returned value instead of echoing it.
<?php
the_title(); // echoes "My Blog Post"
the_title('<h3>', '</h3>'); // echoes "<h3>My Blog Post</h3>"
the_title('', '', FALSE); // doesn't echo anything, but RETURNS "My Blog Post"
?>
See also: Wordpress Codex: the_title().
the_permalink()
The permalink is the unique URL given to every post so that it can be bookmarked, emailed, and linked to even if that post falls off the home page’s display of recent posts. Note that the particular style/formatting of the permalink can be globally altered under Settings > Permalinks.
<?php
the_permalink(); // echoes "http://example.com/news/my-post"
?>
Like bloginfo()
, the_permalink()
outputs a URL, not a complete hyperlink. It’s therefore common to combine it with the_title()
so that you get a meaningful link text linked to each URL:
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
which would output:
<a href="http://example.com/news/my-blog-post">My Blog Post</a>
See also: Wordpress Codex: the_permalink().
the_content()
The main editing window in a Wordpress post editing screen isn’t labeled, but it is referred to as the “content” field throughout Wordpress. Remember that Wordpress automatically wraps paragraphs in <p>
tags, inserts <br />
tags for soft returns, and may add classes to images that appears in the content field.
<?php
the_content(); // echoes "<p>Today I had a chicken sandwich.</p>"
?>
If you’re interested in automatically splitting your posts into shorter and longer versions, read up on the “Read More” option in the codex: Wordpress Codex: the_content().
the_date()
and the_time()
These two functions are a lot like the native PHP date() and time() functions, but they refer to the date/time that each post was published. You have extensive control over the syntax and formatting by using different letters for the function parameters.
<?php
the_date('Y-m-d', '<h2>', '</h2>'); // echoes "<h2>2011-10-12</h2>"
the_date('F j, Y'); // echoes "October 12, 2011"
the_date('F j, Y', '<h2>', '</h2>'); // echoes "<h2>October 12, 2011</h2>"
the_date('F j, Y', '<h2>', '</h2>', FALSE); // returns "<h2>October 12, 2011</h2>"
the_time('d/m/Y \a\t g:i A'); // echoes "10/12/2011 at 10:43 AM"
?>
Note: In Starter Kit, I used the_time()
instead of the_date()
to output the date of each post. The reason: the_date()
is designed to be used in long lists of posts (as on an archive page) where multiple posts might have the same date. The default behavior in this situation is to output the date just once for each group of posts that share that date (this way, dates can be used more logically as headings). This can be useful for that particular kind of archive page, but if you really want to output the date for each and every post, you’re forced to use the_time()
, which doesn’t have that bug/feature.
See also: Wordpress Codex: the_date(), Wordpress Codex: the_time() and Wordpress Code: Formatting Date and Time.
Bringing It All Together
If you look at the snippet-post.php file, you’ll see something similar to the code below (which is simplified a bit). It illustrates how these core functions can be used inside HTML to output (with PHP) and structure (with HTML tags) a blog post.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h3><?php the_time('F j, Y');?></h3>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
For each post, then, this will output something like this:
<h3>October 12, 2011</h3>
<h1><a href="http://example.com/news/my-blog-post">My Blog Post</a></h1>
<p>Today I had a chicken sandwich.</p>