Page 1 of 1

Finding a page id from an alias - [ SOLVED ]

Posted: Thu Feb 04, 2010 6:59 pm
by tallphil
Hi all,

I'm writing a template which will output a link requiring a page id. I know the alias of the page that I'm trying to get to, is there a way to access the id of a page using it's alias? (not the current page)

Re: Finding a page id from an alias

Posted: Fri Feb 05, 2010 4:52 am
by Wishbone
You can make a UDT called something like 'idfromalias', containing:

Code: Select all

global $gCms;
$cntnt = $gCms->getContentOperations();

echo(GetPageIDFromAlias($cntnt->$params['alias']));
You can then access it in your template with {idfromalias alias='youralias'}

Re: Finding a page id from an alias

Posted: Fri Feb 05, 2010 3:21 pm
by tallphil
Sounds perfect! However, I now get the following error:

Code: Select all

Fatal error: Call to undefined function GetPageIDFromAlias() in /home/nanosim/public_html/lib/content.functions.php(771) : eval()'d code on line 4
I created the UDT exactly as suggested in your post.

Re: Finding a page id from an alias

Posted: Fri Feb 05, 2010 3:39 pm
by Wishbone
Whoops... I got that last line a little backwards. Replace the last line with:

Code: Select all

echo($cntnt->GetPageIDFromAlias($params['alias']));
I have it working on my test page.

Re: Finding a page id from an alias

Posted: Fri Feb 05, 2010 4:57 pm
by tallphil
Brilliant, worked first time! Thank you for your help, I've spent hours on this thing and I finally have it working ;)

FYI, I simply wanted to make the {edit} tag display a link to the edit page for a different page. In other words, on page1 have the {edit} tag go to the Admin Edit page for page2.

This is my (now working) solution:

Code: Select all

{capture assign='editlink'}{edit text="Edit This Different Page"}{/capture}
{capture assign='grouppage_id'}content_id={idfromalias alias='somealias'}{/capture}
{capture assign='current_id'}content_id={$content_id}{/capture}
<h3>{$editlink|replace:$current_id:$grouppage_id}</h3>
Out of interest, how would I go about putting this into a nice tidy UDT? Would I be able to access the {edit} module from within a UDT? Doesn't really matter as it's working now anyway.

Re: Finding a page id from an alias - [ SOLVED ]

Posted: Fri Feb 05, 2010 5:04 pm
by tallphil
Arg! I forgot to test this whilst logged out, and the edit link disappears when not logged into the admin back end! Which makes my efforts kind of pointless. Ah well!

Re: Finding a page id from an alias - [ SOLVED ]

Posted: Fri Feb 05, 2010 7:07 pm
by Wishbone
I guess you wouldn't want an edit link showing for users other than the admin. :)

Re: Finding a page id from an alias - [ SOLVED ]

Posted: Sat Mar 20, 2010 5:01 am
by Dr.CSS
Yes the {edit} tag only works if your logged into the back end, so I've always kind of found it silly...