Programmatically editing page-title of single page

Permalink
Hello everyone.

I've created a single page that kind of works like a blog. I link to this page by using an ID, like this:
www.www.example.com/news/detail?id=15...
And this single page then retrieves the correct content from the database and displays it.

What I would like to do, is change the pagetitle depending on the id in the query string. Each of those pages has a specific title already (it's the title of an article), and I would like to use this title for SEO-purposes.

I've looked around but didn't find an immediate way to accomplish this.

Can anyone help?

 
BailHope replied on at Permalink Reply
Hey everyone, sorry to bump this thread, but I was really wondering if anyone had any idea?
12345j replied on at Permalink Reply
12345j
i'd say the easiest way to do something like this is to use javascript to receive all the get parameters and then set the title that way.
http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-j...
http://www.itamer.com/changing-the-page-title-using-javascript/118/...
BailHope replied on at Permalink Reply
Thanks for your reply. Unfortunately, this isn't quite an SEO-friendly solution, as I'd like the search engines to pick up the new title as well, if you catch my drift ...

Does anyone have any other ideas of constructing a pagetitle programmatically?
12345j replied on at Permalink Reply
12345j
its not going to be seo friendly if you're making the title change based on the get parameters.
BailHope replied on at Permalink Reply
Could you clarify what you mean by this?

A different url is a different page to search engines, so I don't see what couldn't be SEO-friendly about this.
Sure, completely different URLs are better, but harder to achieve.

Just to clarify, I mean :
www.www.example.com/news/detail?id=15...
will receive a pagetitle like "Example news - [news item title]"

I know it would be more SEO friendly to include the news item title in my URL as well
like this:
www.www.example.com/news/my-news-item-title?id=15...
or whatever ... but this is not what I'm trying to achieve.

I don't understand why Concrete5 does not offer the opportunity to change your page title depending on the view of your data. It makes sense.
jordanlev replied on at Permalink Reply
jordanlev
C5 behaviour for parsing single_page paths is as follows:
If user requestshttp://example.com/news/article-one,... C5 looks for a single_page controller at that exact path. If it doesn't find one, then it looks to the next-higher path (in this case,http://example.com/news) and sees if the controller for that single_page has an action for the later part of the path (in this case, it would see if the news controller had a "article-one" function).

In your case, though, you can't just create a controller action for every new blog post (you'd have to rewrite the code of that controller on-the-fly, which is silly and maybe not even possible).

So first I'd try using the php magic "__call()" method (seehttp://www.php.net/manual/en/language.oop5.overloading.php#language... ). This will respond to all functions that aren't explicitly declared in the file. In that magic function you'd then have to map the path to the proper blog post. *BUT* this might not work because I'm not sure the magic method will properly tell the C5 system that it will respond to whatever action it happens to be asking for (it might, but it might not -- depends on how the dispatching is implemented in C5 core).

If that doesn't work, then I think the next thing to try is a rewrite rule in your htaccess file that checks for a certain path after the "/news/" path and sends that to the top-level news controller with the path as an argument. This is kind of like duplicating how C5 handles url rewriting already, but you'd be short-circuiting that to do it your own way.

Good luck!
BailHope replied on at Permalink Reply
Thanks jordanlev for your very informative answer.
I'll look into it.

Do you also have an idea of how I can edit the page title from the code?
jordanlev replied on at Permalink Reply
jordanlev
I've never done this myself, so I'm not entirely sure... but it looks like you can pass a $pageTitle variable into the "header_required" element.

This means that the "view.php" file in your theme (which is what C5 uses to skin all single_pages) needs to have this:
<?php Loader::element('header_requires'); ?>

...changed to this:
<?php Loader::element('header_requires', array('pageTitle' => $customPageTitle)); ?>

...and you need to somehow set that $customPageTitle variable from your controller. This I'm really not sure how to do -- you may be able to simply do $this->set('customPageTitle', 'whatever') -- but maybe not because I don't know if those get bubbled up to the view.php template itself. If not, you'll need to look through the core code and figure out how to tell the view to set its title.
I did find one other forum post with a possible solution, but it doesn't really make sense to me (he says he had to uncomment things in the core code, but I don't think that's true):
http://www.concrete5.org/community/forums/customizing_c5/page-title...

Good luck -- and please post back if/when you solve this as I'd love to know how to do it in case I need to in the future.
BailHope replied on at Permalink Best Answer Reply
Jordanlev, your reply was right on the money!

To change the pagetitle on a single page to a title that was programmatically generated, you do the following steps:

1) in your single page controller :

$pageTitle = "My own custom PageTitle"; // construct your pagetitle
$this->set('pageTitle', $pageTitle); //make your variable available to your view


2) in your view.php, or in your own theme, wherever you've put the following line:

Loader::element('header_required', array('pageTitle' => $pageTitle)); 
//pass the pagetitle to the header_required file, which will handle the variable


Things of interest:
- If you happen to pass an empty "pagetitle", it will auto-generate one, which is the default set through the Dashboard.
- If you have a fixed suffix on your website, like for instance " - MyWebsite.com", this will automatically be added to the pagetitle you passed. If you happen to pass it as well, it will appear twice!
chemmett replied on at Permalink Reply
chemmett
To add to this, you can also generate a dynamic meta description the same way by setting the pageDescription variable:
Loader::element('header_required', array('pageTitle'=>$pageTitle,'pageDescription'=>$pageDescription));

To get rid of the site prefix/suffix, you have to customize header_required.php.
PatrickHenry replied on at Permalink Reply
PatrickHenry
Sorry to necro this one, but it's something I'm highly interested in and I think recent updates to c5 since this post have changed how to do this somewhat.
I'm looking to use this to specifically insert a c5 user's First Name and Last Name into the page title.
Instead of the default "Profile :: Site Name", it would be "John Doe :: Site Name"
I'm using c5 users to power my website directory listing, and this would greatly help SEO efforts I think.
Any advice you could give me on how to go about that I would be most appreciative of.