Author |
Message |
bengurion Board Member
Joined: 16 Sep 2018
Posts: 25
|
Posted: Sun Sep 16, 2018 1:56 pm Post subject: Recommend how to get topic_id in header? Canonical |
|
|
I have bunch of links in head that are written there by {NAV_LINKS} tag.
So a pair of this links somehow easily shows a topic link in head with topic_id.
The question is - How to add new string in <head> with a topic url? I can only use topic_id in the body of the topic but not in <head>.
Something like this
<link rel="canonical" href="http://site.com/topic123456.html"/>
The question is - how to exactly get topic_id in header to write this string?
Thank you! And thanks god this forum was created.
|
|
Back to top |
|
|
Salvatos Board Member
Joined: 19 Feb 2009
Posts: 449 Location: Québec
|
Posted: Sun Sep 16, 2018 9:27 pm Post subject: Re: Recommend how to get topic_id in header? |
|
|
{TOPIC_ID} gets added to the template in viewtopic.php (which is run after the header is generated, as far as I know):
Code: | $topic_id = $post_id = 0;
if ( isset($HTTP_GET_VARS[POST_TOPIC_URL]) )
{
$topic_id = intval($HTTP_GET_VARS[POST_TOPIC_URL]);
}
else if ( isset($HTTP_GET_VARS['topic']) )
{
$topic_id = intval($HTTP_GET_VARS['topic']);
} |
This is the initial check but there is something more being done with it further down the file, I'm not sure whether it would be useful in your case. Assuming this is enough, you would then need to copy-paste it somewhere into includes/page_header.php, before template vars are assigned around line 343 (and probably add an else case for when the header is loaded from somewhere other than viewtopic). Then you would simply add a line to the array like so (don't forget the comma):
Code: | 'NAV_LINKS' => $nav_links_html,
'TOPIC_ID' => $topic_id) |
And that template var would be available to you in any of your templates' overall_header.tpl.
|
|
Back to top |
|
|
bengurion Board Member
Joined: 16 Sep 2018
Posts: 25
|
Posted: Mon Sep 17, 2018 7:24 pm Post subject: Re: Recommend how to get topic_id in header? |
|
|
Salvatos wrote: | {TOPIC_ID} gets added to the template in viewtopic.php (which is run after the header is generated, as far as I know):
Code: | $topic_id = $post_id = 0;
if ( isset($HTTP_GET_VARS[POST_TOPIC_URL]) )
{
$topic_id = intval($HTTP_GET_VARS[POST_TOPIC_URL]);
}
else if ( isset($HTTP_GET_VARS['topic']) )
{
$topic_id = intval($HTTP_GET_VARS['topic']);
} |
This is the initial check but there is something more being done with it further down the file, I'm not sure whether it would be useful in your case. Assuming this is enough, you would then need to copy-paste it somewhere into includes/page_header.php, before template vars are assigned around line 343 (and probably add an else case for when the header is loaded from somewhere other than viewtopic). Then you would simply add a line to the array like so (don't forget the comma):
Code: | 'NAV_LINKS' => $nav_links_html,
'TOPIC_ID' => $topic_id) |
And that template var would be available to you in any of your templates' overall_header.tpl. |
It works!!! I still can not believe this forum exist. It is the best thing that can be for the old school community builders. Image link
There is one more issue. If the topic is opened through "post" link, than topic_id returns 0(zero). How to get topic_id than? Image link
|
|
Back to top |
|
|
Salvatos Board Member
Joined: 19 Feb 2009
Posts: 449 Location: Québec
|
Posted: Mon Sep 17, 2018 8:43 pm Post subject: Re: Recommend how to get topic_id in header? |
|
|
bengurion wrote: | There is one more issue. If the topic is opened through "post" link, than topic_id returns 0(zero). How to get topic_id than? Image link |
Something like the Last Post link from the topic list? That one would require a database lookup... Looking deeper into the code, I see there's a better way to proceed than what I suggested above.
page_header.php is actually called from viewtopic.php around line 575. That makes things a lot easier and less redundant for us because it can use variables that have already been set there. Basically I shouldn't have made you copy that initial part into page_header.php because it overwrote the version of $topic_id that is already generated, including the database check for cases such as direct linking to a post. So you can forget everything I said yesterday and just add TOPIC_ID to page_header.php and it should be fully functional:
Code: | 'NAV_LINKS' => $nav_links_html,
'TOPIC_ID' => $topic_id) |
Sorry for not realizing this from the start; I didn't expect it to be this convenient
|
|
Back to top |
|
|
bengurion Board Member
Joined: 16 Sep 2018
Posts: 25
|
Posted: Tue Sep 18, 2018 8:18 am Post subject: Re: Recommend how to get topic_id in header? |
|
|
Salvatos wrote: | bengurion wrote: | There is one more issue. If the topic is opened through "post" link, than topic_id returns 0(zero). How to get topic_id than? Image link |
Something like the Last Post link from the topic list? That one would require a database lookup... Looking deeper into the code, I see there's a better way to proceed than what I suggested above.
page_header.php is actually called from viewtopic.php around line 575. That makes things a lot easier and less redundant for us because it can use variables that have already been set there. Basically I shouldn't have made you copy that initial part into page_header.php because it overwrote the version of $topic_id that is already generated, including the database check for cases such as direct linking to a post. So you can forget everything I said yesterday and just add TOPIC_ID to page_header.php and it should be fully functional:
Code: | 'NAV_LINKS' => $nav_links_html,
'TOPIC_ID' => $topic_id) |
Sorry for not realizing this from the start; I didn't expect it to be this convenient |
Yes you are right. There also a lot of other issues appeared at once
Fixed it. And here is code maybe somebody will need it to make canonical links to phpbb2.
I have "rewrite" so the documents look like https://yourdomain.com/topic123.html. Or if there is other page it looks like https://yourdomain.com/topic123-10.html
Also i added pages numbers to code.
This goes to page_header.php
Code: |
if ($start != 0)
{
$onpage = "-{$start}";
}
else
{
$onpage = '';
}
if ($topic_id != 0)
{
$canonical = "<link rel=\"canonical\" href=\"https://yourdomain.ru/topic{$topic_id}{$onpage}.html\"/>";
}
else
{
$canonical = '';
}
|
And this in variables also in page_header.php
Code: | 'CANONICAL' => $canonical, |
|
|
Back to top |
|
|
Salvatos Board Member
Joined: 19 Feb 2009
Posts: 449 Location: Québec
|
Posted: Tue Sep 18, 2018 11:16 am Post subject: Re: Recommend how to get topic_id in header? Canonical |
|
|
I'm glad you could sort it out!
|
|
Back to top |
|
|
vlad77 Board Member
Joined: 31 May 2015
Posts: 122
|
Posted: Wed Oct 31, 2018 9:05 am Post subject: Re: Recommend how to get topic_id in header? Canonical |
|
|
I made a mod for <link rel="canonical" Rel_Canonical_URL.txt
"Basic" mod se_1.2.1.zip
Description: |
|
Download |
Filename: |
se_1.2.1.zip |
Filesize: |
4.67 KB |
Downloaded: |
1155 Time(s) |
Description: |
|
Download |
Filename: |
Rel_Canonical_URL.txt |
Filesize: |
3.65 KB |
Downloaded: |
1292 Time(s) |
|
|
Back to top |
|
|
|