I noticed my old blogs on blogspot had these wonderful titles coming up on google, like “Figaw’s computer tricks: Roll a Dice on Skype!”, and then I noticed the titles that my wordpress site is putting on google, “How to Roll a Dice in Skype! | Figaw’s blog”. I don’t think the latter sounds very familiar, so.. TO DA GOOGLE MOBILE!!
The information about the page Title lies in the Header.php of your theme, now as everyone suggests you should never change the files in your theme directly, but rather make a child-theme, because 1: you want your changes to be persistent and 2: not change the original theme but rather add your own tweaks. In computer science the term is known as “Coding by addition, not by modification” which generally means “you do not fuck with what works, but you add your own so the previous system still works.”
So when you’ve made a copy of the Header.php into your child theme, if you don’t already have this, start editing it in any tool (for these little things I usually just use the web browser editing tool under Appearance->Editor, and this plugin, http://wordpress.org/extend/plugins/theme-file-duplicator/. The plugin allows me to make copies of pages directly in the browser, so I don’t have to start bothering with FTP-clients; saving seconds is what matters in the long run! Even when I’m not needing a copy of another page, I just select whatever is the first file, make a copy of this into a file with my desired name, and clear the content afterwards. It is really handy for eliminating the FTP-client, as long you intend to use the online editing tool as well of course.
The title of the page is decided by whatever is between the <title>
and </title>
tags. By default mine looked like <title><?php wp_title( '|', true, 'right' ); ?></title>
It displayed the title of eg. the post, it displayed the | -symbol as a separator, and it displayed the title of my blog “Figaw’s Blog”, so all in all “Post Name | Figaw’s Blog” (Something is very wrong here, and I’ll come back to that later in the post). Now I want to change this into “Figaw’s Category: Post Name”, where Category is the first category of which my post is listed in.
In the header.php I updated the code between my <title>
and </title>
tags to,
<title> <?php if(is_single($post)){ $category = get_the_category(); echo "Figaw's ".$category[0]->cat_name.": ".wp_title('', false); } else { wp_title( '|', true, 'right' ); } ?> </title>
Now this resulted in almost what I wanted, the titles everywhere else on my site was as usual, very nicely crafted from the standard theme, but my post title were instead on the form “Figaw’s Category: Some Post Figaw’s Blog”. What went wrong? it always appended “Figaw’s Blog” to the title, so my previous assumption that the function wp_title( '|', true, 'right' );
was responsible for everything was faulty, something else is fiddling with my title! (I kind of realized this as I was reading the documentation for the wp_title
function, but hey, I had to write the code above anyways so I might as well try and fail one step at a time).
The internet suggested I looked in the functions.php of my theme, now I’ve created a child theme with a pretty empty functions file, so I continued to that of my parent theme, and here I found, searching with ctrl + f
for “title” that a function called twentytwelve_wp_title
was actually being used to format the title with the code add_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );
. I still want the titles to be formatted in some way, since the other pages are doing just fine, but I wanted to do it in my own way on the posts, so I copied the code into my child themes functions.php but commented it out the add_filter
line in the parents file.
NB: You will want to rename the function to something else! because if your parent theme ever gets reset, and the function gets declared twice, PHP will break and when people try to view your blog, they’ll be greeted with an error and nothing else. If this happens you’ll have to download the functions.php file with an FTP client and edit this. You’ll be fine as long as you just rename the function to something else, I called mine twentytwelvechild_wp_title. Now the worst case scenario is the title looking a little messy; both errors are fixed in seconds after noticing, but the latter doesn’t affect my blogs availability.
In the function, the line $title .= get_bloginfo( 'name' );
was used to append “whatever get_bloginfo( 'name' );
returns” to the $title variable; a pretty good guess here would be “the name of my blog”. If you comment out this line then this doesn’t happen no more, but then, on all of the other pages it will be missing as well, so once again we need to use wordpress’ conditional tags to distinguish the pages; I only want my posts to look differently. I changed the lines,
From: // Add the site name. $title .= get_bloginfo( 'name' ); To: // Add the site name. if(!is_single($post)){ $title .= get_bloginfo( 'name' ); }
and now everything works as intended. I said never to change the files in the parent theme, but I did it anyways for this to work. Luckily for me the change was only to comment a single line, but now I’ve also made this huge note on how I fix it if it ever starts acting up, so I think I’ll be good. Comments and questions are welcome below!