So if you haven’t been able to tell already… I love WordPress. This past week WordPress launched its newest release, code named “Green,” with version 3.4. My friend and colleague, Justin Handler, has been working with WordPress for quite some time now. He even works as support desk from one of the top ranking theme authors on ThemeForest, Chris “The Molitor” Molitor. Justin has been asking for sometime now that I do a post on WordPress tags, and how to use them. So below you will find (in my opinion) the top three heavy hitters in WordPress tags. They are “the_title(),” “the_content(),” and “get_post_meta().”
1. The Title
Probably the most obivous of all the tags, the_title() is just that… the title you gave the post. Example below:
<?php the_title(); ?>
The title can typically be found at the top of each loop, and is most likely printed out inside either a anchor tag: (
<a>) or a heading tag (
<h1>,<h2>,<h3>, etc) or a combination of both (
<h1><a></a></h1>. See some examples below:
1 2 3 4 5 6 7 8 9 10 11
<header> <h1><?php the_title(); ?></h1> </header> <header> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> </header> <header> <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> </header>
In each case, the title is used as the title of the piece of content you are calling. The title can also be stored as a variable and used in other ways as well. The example below converts the title to lowercase, replaces the spaces to underscores, and prints it as the class of an div.
1 2 3 4 5 6
<?php $title = get_the_title(); $title = strtolower( str_replace( ' ', '_', $title ) ); ?> <div class="<?php echo $title; ?>">Some unique content that can be styled via the title.</div>
So the title acts as the main identifier for that piece of content and can be used as such.
2. The Content
If you think of each piece on content as a body part, the_content() would be the torso. This is where the meat of the matter resides. The content tag holds all the information you add to the WYSIWYG in the backend. Whether its text, images, videos, etc, this is where the majority of your content sits. In addition to being typically the most hefty part of your content, it also is a key part in the exicution of plugins use. When you call the content to the page, see below, you also call hooks that plugin authors can use to add more code to your page. Whether its social integrations, custom galleries, etc, the_content tag is typically where these hooks reside.
<?php the_content(); ?>
3. Get Post Meta
More for advanced users, get_post_meta() is probably the most powerful and most versitile tag in the WordPress arsinal. Post meta is where you can store extra information, images, varibables, etc, that you can use in your theme to customize the look and feel of each post.
Say for example you created a custom post type for the employees at your company. You can use post meta to print out additional information about that person, and format it in a standard way, and not have to worry about messing up the html if someone enters in the wrong informaiton. The example below will print out the person’s name: the_title, their bio: the_content, and their phone number, email address, and job role, all with the template tags we just discuss. This exmaple asumes you can create a custom post type, and custom meta boxes to generate the require inputs for these fields.
1 2 3 4 5 6 7 8 9 10 11 12 13
<header> <hgroup> <h1><?php the_title(); ?></h1> <h4><i><?php echo get_the_title( $post->ID, 'job_title', true ); ?></i></h4> </hgroup> </header> <h4>Bio:</h4> <?php the_content(); ?> <h4>Contact Info:</h4> <p>Phone: <?php echo get_the_title( $post->ID, 'phone', true ); ?><br /> Email: <a href="mailto:<?php echo get_the_title( $post->ID, 'email', true ); ?>"><?php echo get_the_title( $post->ID, 'email', true ); ?></a></p>
So those are the top three WordPress heavy weights. Using these three tags alone can get you pretty far with creating some feature rich, and easily maintainable websites.