Quantcast
Channel: Web Design 101 » WordPress
Viewing all articles
Browse latest Browse all 6

Get the Title of a post or page in WordPress

$
0
0

To get the title of your WordPress post or page you have a lot of options to choose from.  Here are just a couple of them to use when you next need some web design skills…

the_title()

The basic WordPress function to be used WHEN INSIDE THE LOOP which will get the current post or page title that you are on.  You can also reduce your code somewhat further by passing parameters into this funtion, which include a string to to returned before the title, a string after (such as <h2> and </h2>) and an echo parameter.  Only include the echo parameter if you intend to use the function to define a variable for later use.

Example use could be:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php while ( have_posts() ) {

the_post();

$before = '<h1 class="pagetitle">';

$after = '</h1>';

the_title( $before, $after );

}

?>

This would simply echo out the title as a header 1 with class of “pagetitle”.

get_the_title()

The function get_the_title() can be used OUTSIDE THE LOOP to get the title (!) of the post or page you want using a parameter passed to it of the ID.  You can also leave the parameter blank and use this function WITHIN THE LOOP as the same effect as the_title() except you would have to echo it out.

1
2
3
4
5
<?php

echo get_the_title($ID);

?>

To follow on with this post, we will soon show you how to get the title for your blog page, which isn’t always as simple as it looks, due to the dreaded LOOOOOOP… Why do I use capitals to spell out WITHIN THE LOOP…? I HAVE NO IDEA?!

 


Viewing all articles
Browse latest Browse all 6

Trending Articles