Get Page_Alias by PageID

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Locked
Robbwell

Get Page_Alias by PageID

Post by Robbwell »

I would like to code my href as /about-us/ instead of index.php?pageid=21
but if the client decides to change the alias to about-the-company I need this to automatically adjust in the code.

Is there a way to grab the page_alias and page_name of a page by its page_id even if I am not currently on said page..?

So I would like something like:

$page_id = 21;
$page_alias = get_page_alias_by_id( $page_id );
$page_name = get_page_name_by_id( $page_id );

$html = '' . $page_name . '';

Thanks for your help.

RC
Robbwell

Re: Get Page_Alias by PageID

Post by Robbwell »

Any takers, is this an impossible request..?

I am sure it can be done with the $gCms->GetHierarchyManager() somehow but I just cannot get it correctly.

This would be a great help and I am sure would benifit everyone.

Thanks for your help.

RC
mahjong

Re: Get Page_Alias by PageID

Post by mahjong »

No need to code your own function since can use

Code: Select all

{cms_selflink page="page_id"}
instead of

Code: Select all

{cms_selflink page="page_alias"}
.
Last edited by mahjong on Thu Mar 01, 2007 3:20 pm, edited 1 time in total.
User avatar
kermit
Power Poster
Power Poster
Posts: 693
Joined: Thu Jan 26, 2006 11:46 am

Re: Get Page_Alias by PageID

Post by kermit »

what exactly are you trying to accomplish that isn't already available through these options in config.php? (more info in the docs)

#Show mod_rewrite URLs in the menu? You must enable 'use_hierarchy' for this to work for modules
$config['assume_mod_rewrite'] = true;

#Extension to use if you're using mod_rewrite for pretty URLs.
$config['page_extension'] = '/';

also included is a sample .htaccess file in ./docs/

note that this won't work on windows/iis -- all the more reason to use apache on linux. ;)

also: the {cms_selflink} tag can be used to create links to other pages within the site, instead of 'hard coding' them --  if you reference a page by 'id' instead of 'alias' in the tag, the link will update if someone changes its alias.
eternity (n); 1. infinite time, 2. a seemingly long or endless time, 3. the length of time it takes a frozen pizza to cook when you're starving.
4,930,000,000 (n); 1. a very large number, 2. the approximate world population in 1986 when Microsoft Corp issued its IPO. 3. Microsoft's net profit (USD) for the quarter (3 months) ending 31 March 2007.
CMSMS migration and setup services | Hosting with CMSMS installed and ready to go | PM me for Info
Robbwell

Re: Get Page_Alias by PageID

Post by Robbwell »

What I am trying to do is put a page alias in my custom tag, since I cannot use {cms_selflink} within the php code, I need a way to grab the alias by the id. The following code is for a graphic horizontal menu with buttons that stay highlighted when they are on their own pages.

So in my template I have {main_nav button="1"}

But I would rather not code the href as href="index.php?page=40"
I would rather get_alias_by_pageid(40) so my link looks like href="/the-show/"

Code: Select all

// if a specific button is passed to be highlighted
if( isset( $params[ 'button' ] ) )
	$button = $params[ 'button' ];
else
	// default all buttons off
	$button = 0;
	
$strHTML = '
<ul>
	<li id="nav_the_show"><a href="' .  get_alias_by_pageid(40) . '" class="img_rplc' . ( $button == 1? ' current' : '' ) . '">The Show</a></li>
	<li id="nav_buy_ticket"><a href="index.php?page=18" class="img_rplc' . ( $button == 2? ' current' : '' ) . '">Buy Tickets</a></li>
	<li id="nav_buy_subscription"><a href="index.php?page=19" class="img_rplc' . ( $button == 3? ' current' : '' ) . '">Buy a Subscription</a></li>
	<li id="nav_plan_your_visit"><a href="index.php?page=16" class="img_rplc' . ( $button == 4? ' current' : '' ) . '">Plan Your Visit</a></li>
	<li id="nav_groups"><a href="index.php?page=17" class="img_rplc' . ( $button == 5? ' current' : '' ) . '">Groups</a></li>
	<li id="nav_education"><a href="index.php?page=34" class="img_rplc' . ( $button == 6? ' current' : '' ) . '">Education</a></li>
	<li id="nav_support_us"><a href="index.php?page=27" class="img_rplc' . ( $button == 7? ' current' : '' ) . '">Support Us</a></li>
	<li id="nav_work_with_us"><a href="index.php?page=20" class="img_rplc' . ( $button == 8? ' current' : '' ) . '">Work With Us</a></li>
</ul>
';

// write out the menu
echo $strHTML;
I managed to come up with the following which I ripped from /plugins/functions.cms_selflink.php:

Code: Select all

function get_alias_by_pageid( $page = 0 )
{
	global $gCms;
	
	# check if the page exists in the db
	$manager =& $gCms->GetHierarchyManager();
	$node =& $manager->sureGetNodeByAlias( $page );
	
	if (!isset($node)) return;
	
	$content =& $node->GetContent();
	
	if ($content !== FALSE)
	{
		$pageid = $content->Id();
		$alias = $content->Alias();
		$name = $content->Name();
		$url = $content->GetUrl();
		$menu_text = $content->MenuText();
		$titleattr = $content->TitleAttribute();
	
		return $alias;
	}
	else
	{
		return false;
	}
}
and put it in /lib/misc.functions.php

You could easily change the name of the function and return a different variable.

This seems to work fine, but if I am doing something that already exists somewhere, please let me know.

RC
User avatar
kermit
Power Poster
Power Poster
Posts: 693
Joined: Thu Jan 26, 2006 11:46 am

Re: Get Page_Alias by PageID

Post by kermit »

in an edit to the core file index.php to support bbclone, i use the following to grab the page alias, which is then passed on to the bbclone script:

$pageinfo->content_alias


dunno if it works in a tag or not... sorry.
Last edited by kermit on Thu Mar 01, 2007 3:57 pm, edited 1 time in total.
eternity (n); 1. infinite time, 2. a seemingly long or endless time, 3. the length of time it takes a frozen pizza to cook when you're starving.
4,930,000,000 (n); 1. a very large number, 2. the approximate world population in 1986 when Microsoft Corp issued its IPO. 3. Microsoft's net profit (USD) for the quarter (3 months) ending 31 March 2007.
CMSMS migration and setup services | Hosting with CMSMS installed and ready to go | PM me for Info
User avatar
chead
Forum Members
Forum Members
Posts: 63
Joined: Tue Feb 06, 2007 4:01 am

Re: Get Page_Alias by PageID

Post by chead »

If you only need this for a horizontal image menu, why not use the menumanager with a customized template? Most of the included menu templates already apply a CSS class for the currently-selected item. It seems like the code is trying to do what {menu} already does.  ???

Have you seen this tutorial?

Here's another approach with a rough version of a custom menu template (with extra indentation for readability):

Code: Select all

{if $count > 0}
</ul>
	{foreach from=$nodelist item=node}
		{if $node->current == true or $node->parent == true}
			<li><a href="{$node->url}" class="menumain_selected"
				style="background-image:url(mypath/menu_{$node->alias}.gif);">
				{$node->menutext}</a></li>
		{else}
			<li><a href="{$node->url}"
				style="background-image:url(mypath/menu_{$node->alias}.gif);">
				{$node->menutext}</a></li>
		{/if}
	{/foreach}
</ul>
{/if}
Last edited by chead on Thu Mar 01, 2007 7:28 pm, edited 1 time in total.
User avatar
kermit
Power Poster
Power Poster
Posts: 693
Joined: Thu Jan 26, 2006 11:46 am

Re: Get Page_Alias by PageID

Post by kermit »

chead wrote: If you only need this for a horizontal image menu, why not use the menumanager with a customized template? Most of the included menu templates already apply a CSS class for the currently-selected item. It seems like the code is trying to do what {menu} already does.  ???
i was thinkin' that too... he's just re-inventing the wheel.. and partially hard-coding it too.  dunno what kind of images are being used, but one of those image-replacement do-dads and gd might be able to make 'em on the fly for fully dynamic image menus.
Last edited by kermit on Thu Mar 01, 2007 4:12 pm, edited 1 time in total.
eternity (n); 1. infinite time, 2. a seemingly long or endless time, 3. the length of time it takes a frozen pizza to cook when you're starving.
4,930,000,000 (n); 1. a very large number, 2. the approximate world population in 1986 when Microsoft Corp issued its IPO. 3. Microsoft's net profit (USD) for the quarter (3 months) ending 31 March 2007.
CMSMS migration and setup services | Hosting with CMSMS installed and ready to go | PM me for Info
Locked

Return to “CMSMS Core”