Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
How do I get the domain out of the current link?
Hi!
I want to get the domain out of the current link in a linklist to use as a CSS class name on the anchor element. If there is some way I could set a class name for each link in the “Links” tab, that might be easier, but without custom fields on the “Links” tab, I don’t see a way to do that.
After googling about, I came up with the following script to get the domain name out of a specified URL:
<txp:php>
$pax_url = 'http://www.example.com/section/?query=string';
$pax_domain = parse_url($pax_url, PHP_URL_HOST);
$pax_domain = preg_replace("'^www.'","",$pax_domain);
$pax_domain = str_replace(".","_",$pax_domain);
echo $pax_domain;
</txp:php>
Trouble is, I can’t get Textpattern to recognize the current link in context. Textpattern Solutions suggests $thislink['url']
to get the value of the current link_url, but when I put that into my script, it gives me the following notice:
Notice: Undefined variable: thislink on line 2
Here’s the relevant code in my link form:
<txp:php>
$pax_domain = parse_url($thislink['url'], PHP_URL_HOST);
$pax_domain = preg_replace("'^www.'","",$pax_domain);
$pax_domain = str_replace(".","_",$pax_domain);
echo $pax_domain;
</txp:php>
I’ve successfully used $thisarticle["title"]
in article forms to get the raw title input, so I can’t tell what I’m doing wrong. I hope it’s something obvious that someone here might be able to identify and correct.
Thanks in advance!
Offline
Re: How do I get the domain out of the current link?
I haven’t tried it out, but could it simply be a matter of adding global $thislink;
at the beginning of your txp:php section?
If you want to save the class in a variable for more readable code, add $variable
to the global too and save the result in a variable. For example:
<txp:php>
global $thislink, $variable;
$pax_domain = parse_url($thislink['url'], PHP_URL_HOST);
$pax_domain = preg_replace("'^www.'","",$pax_domain);
$variable['link_class'] = str_replace(".","_",$pax_domain);
</txp:php>
and then use <txp:variable name="link_class" />
.
TXP Builders – finely-crafted code, design and txp
Offline
Re: How do I get the domain out of the current link?
Yes! Thank you, Jakob! Packing it into a variable was the next step.
For some reason my usage of $thisarticle
always worked without global
, but now I know.
And knowing is half the battle. ;)
Offline