Can you dynamically set a page title to selectively overide a pages normal title

Permalink 2 users found helpful
Hello

This question has been asked twice before but unfortunately has gone unanswered.I'm guessing thats because it may not be possible but it'd be nice to know either way.

http://www.concrete5.org/community/forums/customizing_c5/dynamic-se...

http://www.concrete5.org/community/forums/customizing_c5/dynamic-ti...

I would like to overide a pages title with a dynamic title. I'm trying to get my search pages to output in the format "SITE_NAME :: SEARCH_QUERY" as they are being indexed by Google but all currently have the same name.

I tried setting $pageTitle in my head right before the call to header_required but it looks the variable is being overwritten somewhere.

Is there any way this can be achieved without changing header_required. I'm reluctant to overide core files as I didn't want to update the files with every core update.

-Thanks

digirunt
View Replies: View Best Answer
pvernaglia replied on at Permalink Reply
pvernaglia
Just thinking out loud here, maybe in a page controller you could set $pageTitle or use a $this->addHeaderItem($string), It would be nice to find a way to do this.
mkly replied on at Permalink Reply
mkly
It actually overrides $pageTitle with the 'meta_title' page attribute it it exists
This might be what's slipping you up.
/concrete/elements/header_required.php line 20 something ish
// all this stuff is after $pageTitle gets created with
// $pageTitle = (!$pageTitle) ? $c->getCollectionName() : $pageTitle;
<?php 
$akt = $c->getCollectionAttributeValue('meta_title'); 
$akd = $c->getCollectionAttributeValue('meta_description');
$akk = $c->getCollectionAttributeValue('meta_keywords');
if ($akt) { 
  $pageTitle = $akt; 
  ?><title><?php  echo htmlspecialchars($akt, ENT_COMPAT, APP_CHARSET)?></title>
<?php  } else { 
  $pageTitle = htmlspecialchars($pageTitle, ENT_COMPAT, APP_CHARSET);
  ?><title><?php  echo sprintf(PAGE_TITLE_FORMAT, SITE, $pageTitle)?></title>
<?php  } 
?>


That may be where you are running into trouble. You can get aggressive in your template header.php and pull something like this
$global $c;
//not sure if you need this
Loader::model('attribute/categories/collection');
$collection_attribute_key = CollectionAttributeKey::getByHandle('meta_title');
$c->setAttribute($collection_attribute_key, 'My New Title');

before header_required but that would permanently set that value everytime which feels pretty crude.

Other than that you would either have to
1. Make sure no one adds a meta_title(You can even delete it if you want to go all out)
2. Create your own /elements/header_required.php called like google_title_header_required.php
3. Create a new meta_title called like special_meta_title, name it like the old one and then set $pageTitle before header_required.php and account for the google check first.

Number 1 would probably be what I would do.
mkly replied on at Permalink Best Answer Reply
mkly
Oh I forgot. Are you passing $pageTitle with header_required?

You might need to do something like this
<?php Loader::element('header_required', array('pageTitle' => 'My Fancy Page Title')); ?>


You might already know that though. Loader::element runs extract() on the array passed in the second argument.

EDIT: Oops the array. Fixed.
digirunt replied on at Permalink Reply
digirunt
Wow thats great, a really simple fix. I wasn't aware loader didn't work like a normal include.

I just added...

<?php 
$pageTitle = (isset($_REQUEST['query'])) ? $_REQUEST['query']." Search Results" : '' ;
Loader::element('header_required', array('pageTitle' => $pageTitle));
?>


and my search pages are now titled "SITE_NAME :: SEARCH_QUERY Search Results"

This should help my Google search listing look a lot more relavent.

-Thanks