lil_bugga
lil_bugga

Reputation: 91

Wordpress Title Inside Header

I'm tyring to change how the title works within the header.php file on Wordpress. At the moment I have it so it will display out the name of each page the user is on, which works well, apart from on pages that load blog posts.

I'd like it set so the "title" element within the header.php file can cope with custom names too i.e. instead of loading the name of the latest blog as the title when you visit the blog page I want it to just say Blog.

I decided to create my own variable and then using header check to see if that has been set, but I can't get it to work, placing it either before or after the call to the header file.

index.php

$header_title = "Blog";
get_header(); ?>

Then inside the header.php file I have the following code.

<title>
April Kelley | 
    <?php if (isset($header_title)) { 
        echo $header_title;
     } else {
        the_title(); 
    }?>

Now to my mind this should work, so where am I going wrong?

PS. I only intend to use this $header_title variable on certain pages like index.php and seach.php where the pages pull in the blog posts themselves, so will isset still return false is the variable cannot be found?

Upvotes: 0

Views: 601

Answers (2)

soju
soju

Reputation: 25312

A proper way of doing this is to use the_title filter, you don't need to modify template, e.g. :

function my_title($title, $id) {
    if (is_home())
        $title.= ' | Blog';

    return $title;
}
add_filter('the_title', 'my_title');

If you really want to use your own var, you should read this : How to use own php variables in wordpress template?

Upvotes: 2

anstrangel0ver
anstrangel0ver

Reputation: 1073

replace

the_title(); 

as

echo get_the_title()

hope this work for you

Upvotes: 0

Related Questions