Programmatically editing page-title of single page
I've created a single page that kind of works like a blog. I link to this page by using an ID, like this:
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?
Does anyone have any other ideas of constructing a pagetitle programmatically?
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 :
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
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.
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.
I'll look into it.
Do you also have an idea of how I can edit the page title from the code?
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):
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.
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!
To get rid of the site prefix/suffix, you have to customize header_required.php.
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.