mjut Posted September 24, 2019 Share Posted September 24, 2019 Hello, I am having trouble achieving this: I am trying to place a link to a specific page in my template. But I want this link displayed only, if the page is published. With this code, its not working: <?php if ($pages->find("id=1241, status=published")) { ?> <a href="<?php echo $pages->get(1241)->url() ?>"><?php echo $pages->get(1241)->title() ?></a> <?php } ?> (The link is being displayed no matter the status of the page "1241") How can I fix this? Thanks for your help!!! Link to comment Share on other sites More sharing options...
Sergio Posted September 24, 2019 Share Posted September 24, 2019 (edited) <?php $p = $pages->get(1241); if (!$p->is(Page::statusUnpublished)) : ?> <a href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a> <?php endif; ?> https://processwire.com/api/ref/page/status/ Edited September 24, 2019 by Sergio 2 Link to comment Share on other sites More sharing options...
Sergio Posted September 24, 2019 Share Posted September 24, 2019 There's also a shortcut: if (!$p->isUnpublished()) : 1 Link to comment Share on other sites More sharing options...
mjut Posted September 24, 2019 Author Share Posted September 24, 2019 @Sergio Wow. This is working. I am no php expert. I am always having trouble on this... But I am on my way to get more comfortable on working with variables.... Thanks a lot! <?php $p = $pages->get(1241); if (!$p->isUnpublished()) : ?> <a href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a> <?php endif; ?> yup - tested! The short version works 100%! 1 Link to comment Share on other sites More sharing options...
adrian Posted September 24, 2019 Share Posted September 24, 2019 How about this: <?php $p = $pages->findOne(1241); if ($p->id) : ?> <a href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a> <?php endif; ?> Perhaps it's only a minor optimization, but it saves it returning any page object if the page isn't published. Be sure to read the docs on findOne() vs get() to understand fully, but basically find() and findOne() won't return unpublished pages unless to specifically request them to, but get() will ignore unpublished and hidden statuses and always return a page if everything else matches. 4 1 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