Martinus Posted September 14, 2022 Share Posted September 14, 2022 (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 September 15, 2022 by Martinus answered with solution Link to comment Share on other sites More sharing options...
pwired Posted September 14, 2022 Share Posted September 14, 2022 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> apihttps://processwire.com/api/ref/pages/get/https://processwire.com/api/ref/functions/pages/ infohttps://processwire.com/talk/topic/4909-site-url/ modulehttps://processwire.com/modules/process-menu-builder/ 2 Link to comment Share on other sites More sharing options...
Krlos Posted September 15, 2022 Share Posted September 15, 2022 Also, <?php echo $pages->get(1210)->url; ?> 1210 being the page ID. <a href="<?php echo $pages->get(1210)->url; ?>">Products</a> 1 Link to comment Share on other sites More sharing options...
Martinus Posted September 15, 2022 Author Share Posted September 15, 2022 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 More sharing options...
Pixrael Posted September 15, 2022 Share Posted September 15, 2022 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. 2 Link to comment Share on other sites More sharing options...
Martinus Posted September 15, 2022 Author Share Posted September 15, 2022 Oh, thanks. I did not know that. Good advice! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now