Jump to content

Display specific link if page is published only


mjut
 Share

Recommended Posts

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

@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%!

  • Like 1
Link to comment
Share on other sites

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.

  • Like 4
  • Thanks 1
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...