Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+7 votes
3.7k views
in Q2A Core by
edited by

Hi Everyone, I have been searching all over the forum, but couldnt find any code to fixe this issue.

How to change HOMEPAGE TITLE only? Without adding the site title! (I speak about title not H1)

I set up 'TAGS' => '', so my homepage is the list of tags in qa.config.php - Then in qa.lang-main.php I change the translation of the page. That is the good h1 title, but it's add the website title after, so google cut my title in search results...

I would like : <title>My homepage title</title>

Right now : <title> My homepage title - My site title</title>

So in search result : <title> My homepage titl...</title>

by
It is currently working that way in the core. I mean, regardless of the page you have as the homepage, make it All Activity or Q&A, if the request is for the homepage then the site name is not added to the title. For the rest of the pages it gets added. Unless you're using a custom theme, of course. Try this with the classic theme and double check that it works as you expect.
by
edited by
hi Puppi, its not because of the theme, its because I set homepage as "Tags"... so its had the title of the website after, even in homepage... So I need something like if($this->template == 'tags') => 'no homepage title"

1 Answer

+3 votes
by
edited by
 
Best answer

Now I see the issue. I misunderstood the 'tags' part. I could reproduce this situation you're facing. So, try this:

1. Edit your qa-theme/<your-theme>/qa-theme.php file

2. Locate function head_title

3.1 If you can't find it then add this function to the file:

function head_title() {
    if ($this->template === 'tags') {
        $headtitle=strip_tags(@$this->content['title']);
    } else {
        $pagetitle=strlen($this->request) ? strip_tags(@$this->content['title']) : '';
        $headtitle=(strlen($pagetitle) ? ($pagetitle.' - ') : '').$this->content['site_title'];
    }
   $this->output('<title>'.$headtitle.'</title>');
}
 
3.2 If you found it then you're using a custom theme which requires a custom solution. Anyway, you can still try replacing that function with the one in 3.1 and see if it works
by
I finally just find out the solution by adding this  if($this->template == 'tags') {
                $pagetitle=strip_tags(@$this->content['title']);
                $this->output('<TITLE>'.$pagetitle.'</TITLE>');
            }
            else
by
Great! It is the same solution as I proposed, the one I mentioned just requires:
&& $this->template !== 'tags'
to be added :)
by
:) just wondering, its had an other empty <title></title> after the good one, do you think your solution fixe this?
by
Oh, you have an output after the ELSE? something like
if (blah) {
    outout...
} else
    oneline..
output...

If that is the case then it makes sense you're getting 2 titles. And yes, mine should do the trick :)
by
OH! You want, ONLY for the tags page, the content of the H1 inside the <title> tag without the site name. So that if you're in the tags (home) page you see: "<your tags page title>" and if you are in any other page you see "<your any other page title> - <your site name>"... is this correct?
by
edited by
exactly! you got it ;) - Actually its working with that code below, except this double title. And I may use this for other specific pages like "all activity"
by
Ok, check now. If you want to try it for the all activity too then just do something like:
($this->template === 'tags' || $this->template === 'activity')
Gotta run now! Hope this helps.
by
that's help a lot! thanks Puppy!!!
by
Just got an error with TAG alone, everything is fine but I try to add ' | '  but its create an error <b>Notice</b>:  Undefined variable: pagetitle.

if ($this->template === 'tag' ) {
        $headtitle=strip_tags(@$this->content['title']) .$pagetitle.' | ' .$this->content['site_title'];
         $this->output('<title>'.$headtitle.'</title>');
    }
by
Bear in mind that is not the code I've added to the answer. You're outputting the title inside the IF statement and also using the $pagetitle variable in the previous line which is not defined. As I mentioned in my previous comment, you just need to modify one line to apply the same logic to other templates apart from the 'tags' one. Just change this line:

if ($this->template === 'tags') {

With this line (including any other templates you need):

if ($this->template === 'tags' || $this->template === 'activity' || $this->template === 'tag') {

Btw, if you end up having too many templates the previous line is equivalent to this less wordy alternative:

if (in_array($this->template, array('tags', 'activity', 'tag'))) {
by
Ok. I'm not 100% sure what title you want to display in the 'tag' page but I guess it is similar to the ELSE statement there. I think the result you should be getting is your expected result but you're just getting the notice. Just remove the variable and one dot around it (only for the $headtitle in the 'tag' section and NOT for the $headtitle in the ELSE statment in your code above):

$headtitle=strip_tags(@$this->content['title']) .' | ' .$this->content['site_title'];
by
edited by
Thanks a million man! everything works fine now!
...