Page 1 of 1

Get Page_Alias by PageID

Posted: Tue Feb 27, 2007 8:15 pm
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

Re: Get Page_Alias by PageID

Posted: Thu Mar 01, 2007 2:42 pm
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

Re: Get Page_Alias by PageID

Posted: Thu Mar 01, 2007 3:00 pm
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"}
.

Re: Get Page_Alias by PageID

Posted: Thu Mar 01, 2007 3:04 pm
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.

Re: Get Page_Alias by PageID

Posted: Thu Mar 01, 2007 3:40 pm
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

Re: Get Page_Alias by PageID

Posted: Thu Mar 01, 2007 3:54 pm
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.

Re: Get Page_Alias by PageID

Posted: Thu Mar 01, 2007 4:06 pm
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}

Re: Get Page_Alias by PageID

Posted: Thu Mar 01, 2007 4:09 pm
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.