Jump to content
mjut

Display specific link if page is published only

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

Share this post


Link to post
Share on other sites

There's also a shortcut:

if (!$p->isUnpublished()) :

 

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
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 3
  • Thanks 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...