This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

martin_emmert
martin_emmert

I just want to share something I recently used in one of my projects...

function onEnd() {

    // get the post component
    $_post = $this->components['post'];
    $_post = $_post->post; // post is the alias of my component

    // get the title
    $_title = $_post->title;

    // update the page title
    $this->page->title = $_title;

}

Note: the component instance on the particular page i used this script is named post - see comments in the code

Cheers

Martin

Last updated

dwvcfii12090
dwvcfii12090

Martin,

Just wanted to say thanks for this snippet. I'm new to October and still fumbling my way around the internals. I followed one of the video tutorials for setting up the RainLab blog plugin and the only change I had to make was change:

 $_post = $this->components['post']; 

to

 $_post= $this->components['blogPost']; 

Simply because the component alias was different. But that did exactly what I needed -- change the page title to the blog post title.

parti7818900
parti7818900

Which file should be modified?

alxy
alxy

This should go in your pages code section...

parti7818900
parti7818900

Ok I found the php editor had given

noxioustoo34847
noxioustoo34847

If you use Builder then to set page title as post title you should do this:

function onEnd()
{
   $this->page->title = $this->components['builderDetails']->record->attributes["name"];
}

If it's not working then echo dump($this->components['builderDetails']) and see where is your post title.

pierzo34496
pierzo34496

which file must be modified?

MrCoderZ
MrCoderZ

you should write like this in post page :

title = "post" 
url = "/blog/post/:slug"
layout = "default"
is_hidden = 0
[blogPost]
slug = "{{ :slug }}"
==

function onEnd () {
    $post = $this->components['blogPost'];
    $post = $postTitle->post;

    if( $post ) {
        $post = $post->title;
        $this->page->title = $postName.' | yourSite';
    }
}

==
twig section ...

Last updated

alessio
alessio

Thanks @MrCoderZ

I slightly modified the code for both the post and blog page title as follows:

Post page title:

title = "Post"
url = "/post/:slug"
layout = "yourLayout"
is_hidden = 0

[blogPost]
slug = "{{ :slug }}"
categoryPage = "blog"

==
<?php
function onEnd () {
    $post = $this->components['blogPost'];
    $post = $post->post;

    if( $post ) {
        $post = $post->title;
        $this->page->title = $post.' | yourSite';
    }
}
?>
==

Blog page title:

title = "yourSite"
url = "/blog/:category/:page?"
layout = "yourLayout"
is_hidden = 0

[blogPosts]
pageNumber = "{{ :page }}"
categoryFilter = "{{ :category }}"
postsPerPage = 5
noPostsMessage = "No posts found"
sortOrder = "published_at desc"
categoryPage = "blog"
postPage = "post"

==
<?php
function onEnd () {
    $myblog = $this->components['blogPosts'];
    $myblog = $myblog->category;

    if( $myblog ) {
        $myblog = $myblog->name;
        $this->page->title = $myblog.' | yourSite';
    }
}
?>
==
Mennax
Mennax

noxioustoo34847 said:

If you use Builder then to set page title as post title you should do this:

function onEnd() { $this->page->title = $this->components['builderDetails']->record->attributes["name"]; }

If it's not working then echo dump($this->components['builderDetails']) and see where is your post title.

$this->record->name;

it also works.

Last updated

jaans
jaans

alessio Thanks!

janpark
janpark

Amazing, this helped me a lot!

Hessel
Hessel

noxioustoo34847 said:

If you use Builder then to set page title as post title you should do this:

function onEnd() { $this->page->title = $this->components['builderDetails']->record->attributes["name"]; }

If it's not working then echo dump($this->components['builderDetails']) and see where is your post title.

Thank you, exactly what I was looking for.

1-13 of 13

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.