How to get particular page URL in magento - magento

I would like to get a page's URL key in Magento.
For example, I have a CMS page called "What's New" with the identifier (or URL key) "whats_new". Its correct URL is therefore http://mysite.com/whats_new
Currently I use this code to echo its location:
<?php echo Mage::getBaseUrl();?>whats_new
I feel it's bad practice because its identifier (or URL key) is administrable; if its URL key or identifier changes then the link will break. What is the proper way to echo its dynamic URL key? Perhaps something similar to Wordpress's get_permalink('10')?

I think this will do what you want:
<?php echo Mage::helper('cms/page')->getPageUrl( $pageId ) ?>
Replace $pageId with the correct id for the page you are linking to and it should work.

Try this
<?php echo $this->getUrl('whats_new');?>
If you need to add url key dynamically then
<?php echo $this->getUrl($yourDynamicVariable);?>
of course you must implement the features that you need to fill the variable if url key is changed

You shoud use <?php echo Mage::getUrl('page-url.html); ?>

In CMS Page
{{store _direct="url_key"}}
If you want in .phtml file then
<?php echo Mage::helper('cms/page')->getPageUrl('url_key') ?>

Mage::getUrl(null, array('_direct' => $page->getIdentifier()));

It is also possible to retrieve the CMS page URL using the page identifier like,
<?php echo Mage::helper('cms/page')->getPageUrl('cms_page_identifier') ?>

You shoud use
{{store direct_url="whats_new/"}}
<?php echo $this->getUrl('whats_new');?>

Related

Processwire Add css class to page

I there a possibility to add a classname to a page?
I can't figure out how to implement such feature or if it already exists.
I'm using Processwire 3.0.42.
Put something like this in your template where the body is:
<body class="<?php echo $page->template->name; ?>">
That will give your page body tag a class equal to page template name.
You can add a page title in the same way.
<body class="<?php echo $page->name; ?>">
Don't be worried about adding a class to every page. The overhead of doing this is negligible.
If you wish to add a different class you would need to add a field to your template and append it to the code above.
As always in ProcessWire, everything is under your control. Alternatives to #ivangretsky's perfectly good answer would include-
Simply include a conditional in your template file. (This doesn't scale well if you need to add other classes to other pages using the same template.)
<?php
$bodyClass = '';
if($page->id == 1021) $bodyClass = 'my-class';
?>
<body class="<?php echo $bodyClass; ?>">
NB Using $page->id is better than $page->name, for example, as the ID doesn't change while the name could.
You could also add a field to the page template definition. Add a field called something like 'Body Class'. Then use the content of that field in your template file.
<body class="<?php echo $page->body-class; ?>">
This will scale better than my other suggestion, and there are options in the ProcessWire backend to hide or partially hide the field during normal use if you don't want users messing with its value.
One 'gotcha' from the CSS spec to be aware of is that CSS identifiers, including class names and IDs cannot start with a digit, so you cannot just use $page->id.
There are lot of options and it depends on your needs and maybe imagination :-).
You can use page name, template name, page id, or maybe combination.
<body class="<?php echo $page->template->name; ?> page-id-<?php echo $page->id; ?>">
// Output
<body class="your-template-name page-id-1234">
This way you can target template, page, or both.
Or, as mentioned by #ivangretsky, you can add custom field to page. And again, you can combine this aproach with template name, etc.
It depends on your needs and what you want to achieve.
Notice:
Using $page->id is better than $page->name, as the ID doesn't change while the name could. #DaveP

How to echo out Magento meta tags in theme files

I have a client that is requesting independent H1 tags which can be different to the auto generated page titles in Magento, i.e usually these would be category names, page titles, product titles etc.
As there is no field for this in Magento backend I thought the best way to overide would be if the user puts the custom H1 in the meta tags field and then I pull that data in the theme files if it exists or show standard title if not.
So for instance in:
/app/design/frontend/base/default/template/catalog/category/view.phtml
We have:
<h1><?php echo $_helper->categoryAttribute($_category, $_category->getName(), 'name') ?></h1>
My idea was to use the default meta keyword head tags and have something like this:
If this is not empty show it
<?php echo htmlspecialchars($this->getKeywords()) ?>
else show default title
<?php echo $_helper->categoryAttribute($_category, $_category->getName(), 'name') ?>
Problem is the <?php echo htmlspecialchars($this->getKeywords()) ?> does not show anything when placed outside of its defult head.phtml template.
Anyone have any ideas how to get the meta keywords in any theme file in Magneto?
Magento ver. 1.4.1.1
Thanks!
Try using the following:
$keyWords = Mage::getStoreConfig('design/head/default_keywords')
This will ensure that the keywords are loaded. If you review app/code/core/Mage/Page/Block/Html/Head.php, you'll find that the function getKeywords() renders the same result (be it with caching the keywords).

How to not have the Welcome Message cached in Magento

I'm trying to not have my "Welcome Message" on Magento Cart header cached by my full page cache module. Everything I've tried has led to complete failure. There has to be a way.
I'm using Magentos persistent cart option and I've discovered there is some difference in the welcome message with this option that the module developers may not have accounted for. Don't know really.
It's kind of like the "welcome message is it own module but in another way it's not, It's kind of a php one line on the header page.
Now my fpc module has an option in administration to exclude modules from being cached but you have to give the modules "name" You know i.e. name="some_name". The welcome message isn't like the rest of the other modules that I can tell. Here is the php in the header:
<p class="welcome-msg"><?php echo $this->getWelcome() ?> <?php echo $this->getAdditionalHtml() ?></p>
There is nothing in the parentheses, so I've been trying to give this welcome message a name. I don't know how else to do it.
So I created a static block in adminisration with this in it:
{{block type="core/template" name"header.welcome" as="welcome" template="page/html/welcome.phtml"}}
Then I created a phtml file called welcome.phtml with this in it:
<p class="welcome-msg"><?php echo $this->getWelcome() ?> <?php echo $this->getAdditionalHtml() ?></p>
Then in the header I added this:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('welcome')->toHTML(); ?>
That usually works with about anything. But not this time. Nothing. So under the block page/html_header I added this to page.xml:
<block type="core/template" name="header.welcome" as="welcome"/>
This is mt latest attempt. Does anyone have any ideas about how to go about this? I like the welcome message being dynamic.
thanks
The welcome message is just a function of the header block. Lesti_Fpc needs the welcome message in a seperated block. In Magento 1.8 this is solved and the name of the block is welcome. In Magento 1.7 there is a semi-solution in core...
This issue is solved here: (source)
https://gordonlesti.com/lestifpc-magento-1-7-and-the-welcome-message/
I think I have made some progress for this problem. However I am not getting the solution. What I have done is created a new block in the app/design/frontend/default/layout/page.xml file.
I have added this:
<block type="page/html_welcome" name="testwelcome" as="testwelcome"/>
There seems to be a built in core function called "welcome". It can be viewed at app/code/core/Mage/Page/Block/Html/Welcome.php. So That is the reference in the page.xml file.
Then in the header.phtml file in app/design/frontend/default/template/page.html I placed a call for:
<?php echo $this->getChildHtml('testwelcome') ?>
And finally I created a new template file called testwelcome.phtml in app/design/frontend/default/template with the following code:
<p class="welcome-msg"><?php echo $this->getWelcome() ?> <?php echo $this->getAdditionalHtml() ?></p>
I can get the welcome message to display, but I can't seem to get it to render any changes.

codeigniter dynamic data template

I have this template view at view/include
<?php $this->load->view('include/header'); ?>
<?php $this->load->view($sidebar_column); ?>
<?php $this->load->view($result_column); ?>
<?php $this->load->view($footer_row); ?>
<?php $this->load->view('include/footer'); ?>
and i have the footer_row html at view/include
<div> this is footer row <?php echo $username ?></div>
then i call the footer_row in my controller
$data['footer_row'] = 'include/footer_row';
$this->load->view('include/template',$data);
My question, the footer_row is logged in user info and it appear in EVERY pages. With the above method I use, I have to call and retrieve the user info in every controller. How can i make it reusable so i don't need to repeat myself.
Reusability comes from making use of your constructors, and parent classes. Have a look at my previous answers:
Header and Footer in CodeIgniter
Constructor session validation for different functions
Instead of using this method why dont you use a template library which is easy to use. I would recommend Philsturgeons Template Library but there are some more you can use any that fits to your requirements.
Williams Concepts
http://williamsconcepts.com/ci/codeigniter/libraries/template/
Phil Sturgeons's
http://philsturgeon.co.uk/demos/codeigniter-template/user_guide/
Most Simple
http://maestric.com/doc/php/codeigniter_template
And Finally Binpresses's
http://www.binpress.com/app/codeigniter-template-library/223

Codeigniter URL for navigation

I can't figure out how to do the url links.
Basically I have my navigation bar, and I don't know which CodeIgniter URL code to use and how to implement it.
Is what I'm doing here right?:
<?php $this->load->helper('url'); ?>
<li>About Us</li>
I tried to do an anchor like this, but when I load the page it just turns up blank:
<?php echo anchor('views/about.html', 'About Us', title='About Us'); ?>
What am I doing wrong?
There are two ways to make links:
CodeIgniter helper style:
<?php echo anchor('about', 'About us', 'title="About us link"'); ?>
More common HTML with URL echo:
About us
Both will output:
About us
Though if I understand what you are trying to achieve, your mistake is elsewhere.
You don't include the views part, as your URL should point to the controller, not a view. The only case is if you have a controller named views.
CodeIgniter is set up so that it doesn't include file extensions like .html in URL's by default. It does if you've set them up in your config file in $config['url_suffix'] = '';, which is null by default.
See if you've made any of these mistakes.
That is another way on how you do the URL if you are using the URL helper in CI. You should try this, make the base_url() as the value for href. Try this,
About Us
You have to try like this
About Us
or you can give like
About Us
and in the "about" function you put
$this->load->view('about');
but i think the firstone will works for you fine.

Resources