Jump to content

[SOLVED] Link to a specific page


Martinus
 Share

Recommended Posts

What is the right way?

In a template file I am trying to make a single link to a specific page. Within the <a href="">Products</a>. I know it's easy while looping by pages->find. But how do I manually do this the right way? /products/ does not work and 'products' is not working either. This gives trouble in the path. 

I am thinking this is asked so many times, but it's difficult to find in the forum. I was reading through the API too but could not find it.

Edited by Martinus
answered with solution
Link to comment
Share on other sites

examples
<a href="<?php echo $pages->get('/products/')->url; ?>">Products</a>
<a href="<?php echo $pages->get('/about/')->url; ?>">About</a>
<a href="<?php echo $pages->get('/contact/')->url; ?>">Contact</a>

api
https://processwire.com/api/ref/pages/get/
https://processwire.com/api/ref/functions/pages/

info
https://processwire.com/talk/topic/4909-site-url/

module
https://processwire.com/modules/process-menu-builder/

 

 

  • Like 2
Link to comment
Share on other sites

  • Martinus changed the title to [SOLVED] Link to a specific page

Thankyou all. It worked.

But now the question about ID or Name ... which is better to use? I know people will say ID because name can change but ID stay the same. It's just not explaining much in the template if one can see ID but have no clue what page it is, right?

Link to comment
Share on other sites

35 minutes ago, Martinus said:

which is better to use?

If you plan to export and import pages at some point, ex. development to production. I recommend that you never use page IDs in selectors, or in other places where they acts as hard data. Because (during the import) the ID value of the source can change depending on the availability of the number in the destination, and that can destroy your code logic.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...