Jump to content

Recommended Posts

Posted (edited)

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
Posted

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
Posted

Also,

<?php echo $pages->get(1210)->url; ?>

1210 being the page ID.

<a href="<?php echo $pages->get(1210)->url; ?>">Products</a>

 

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

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?

Posted
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

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
  • Recently Browsing   0 members

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