This is single.php

5 Commonly Used WordPress Functions

This is content-single.php

5 Commonly Used WordPress Functions

WordPress Functions

48% of Technorati’s Top 100 blogs are managed with it.

74.6 million sites depend on it.

Or 18.9% of all sites on the web.

What is it?

WordPress.

It used to be a bit of a black box back in the day when I didn’t understand what PHP was.

It was still a mystery until I learned about the built in functions inside of it recently.

There are a lot.




You’d probably only use a handful of them most of the time.

Like…

Snatching The Post Title

With all of the WordPress functions these are best run in a loop so you can splash the html onto a page.

What you want to use here is the_title.

The code might look something like:

<?php echo get_the_title( $ID ); ?>

Where $ID is the ID of a specific post.

If you leave out the optional $ID you get your most recent post.

And if you throw it inside of a loop?

You can print out any number of posts on the page… however you want it.

What else?

Grabbing The Post Link

If you’re tossing out the post title by itself that’s great… you’ve printed out a list of titles.

What if you want each of those titles to link to the actual post?

Well you’d build the html and php so that the title is wrapped in a link tag.

It might look something like this…

<a href="<?php echo get_permalink; ?>"><?php echo get_the_title; ?></a>

The link’s inside of the href attribute.

The actual post title is the link’s text.

Hallelujah!

You’ve turned your list of post titles into a list of post titles and links to their actual blog post entries.

Just imagine…

Showcasing The Post Image

Every blog post you or your clients create should have an image to suit the subject.

You the developer want to display some sort of thumbnail image to catch the eye of your audience or your client’s audience when you custom design their WordPress site.

Lovely.

Use…

<?php echo get_the_post_thumbnail( '51', 'full' ); ?>

Or something along those lines.

In this case we passed some optional options to grab the thumbnail for a post with ID 51 and make it the full width (i.e. full).

Of course you could just use your CSS stylesheet to set the image size too.

Stick this in the same look and you’ve got yourself the start of a visual feast.

Spilling The Post Content

What if you want to show the entire post itself?

… in one long scrolling of blog posts?

You use:

<?php echo get_the_content; ?>

Again inside of a loop.

Nothing fancy.

Yet, on the other hand…

Slice It Down to An Excerpt

Why spill the entire story of each post on the blog listing page, eh?

You want to entice your audience to read only the posts that interest them.

So display only the excerpt text of the post or page if any is provided.



Your audience’s attention span is short and you want to hand them a page that is easily scannable.

No frustration necessary.

And this piece of code?

<?php the_excerpt(); ?>

For a simple WordPress site, grabbing and fanning out content into the HTML with these kind of code commands should be a breeze.

Cheers,
Sunny Lam

Writing just north of the Beaches
Toronto, Ontario

References

14 Surprising Statistics About WordPress Usage

Comments insertion starts here.

Leave a Reply