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

Get the Title of your BLOG PAGE in WordPress

$
0
0

Following on from our previous post about getting the title of a post or page in WordPress within web design, this post is here to help you return the title you have given to the page which lists your BLOG entries.

WordPress Reading settings

This is when you have set up your WordPress installation to act as a static site with a home page and a news page – which you do by going to Settings->Reading and changing the option for ‘Front page displays’ from ‘Your latest posts’ to a known static ‘home’ page and a known static ‘blog/news’ page. What this does is change how WordPress displays where your latest posts are viewed, mainly in your template files.  We won’t cross that bridge yet – just hint to you that you might fall off it to your death…

In your theme files, open up index.php (which will be used to view your latest posts). If you just put the_title() chances are this function will echo out the first blog post title, and not the title of your blog page (e.g. ‘Blog Section’).

get_the_title(‘NUMBER’)

You could use the function get_the_title() along with the known page ID of your blog, e.g.

1
2
3
4
5
<?php

echo get_the_title(6);

?>

Where ‘6‘ was the known page ID of your ‘blog page’.  Trouble with this method is that it is not good for themes that may travel onto other WordPress installations, or if you delete your blog page and set it up on a new page with a different page ID.

single_post_title()

The WordPress function single_post_title() can be used which displays post/page titles OUTSIDE THE LOOP.  The function can have additional parameters set including a prefix string and an echo parameter to either spit the title out or retain if for use within the PHP. It is better than using get_the_title(ID) above as it does not need an ID number.

1
<h1><?php single_post_title('Give me my blog page title: '); ?></h1>

If this fails, try below…

get_option(‘page_for_posts’) and get_the_title()

When you set your page for your latest blog posts above – you set an option value in the WordPress wp_options database table for option name ‘page_for_posts‘. That option value is the page ID of the page used to display your posts. With this in mind we can use WordPress functions get_option() and get_the_title() to always output the correct page title.

1
2
3
4
5
6
7
<?php

$pageTitle = get_the_title( get_option('page_for_posts') );

echo $pageTitle;

?>

This will echo out the page title of the page used and set as the page to display your blog posts – not the title of your first blog post.

Simple!


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images