Jump to content
EyeDentify

Trouble Detecting if there are siblings to a page or not

Recommended Posts

Hello There fellow PW users.

I have been racking my brain looking at this code for a while know and the solution probably is simple.
But i need a fresch set of eyes on my problem.

I have this code:

 

<?PHP
/* render siblings or not */
$sibItems = $page->siblings();

if(count($sibItems) > 0) {

    echo('<ul class="unstyled-list">');
    echo('<li>Other post like this:</li>');
        
        /* render the siblings if any, exclude current page */
        foreach($page->siblings('limit=5', false) AS $sibling) {

            echo("<li><a href=\"{$sibling->url}\">{$sibling->title}</a></li>");

        }

    echo('</ul>');
}

?>

What i am trying to do is detect if there are any siblings to the current post, and if there are siblings then render them. If no siblings then not render. You get the picture.

But at this point the IF statement is run no mather what i do. and this post i am testing on has no siblings. It is a lone child under another page so it should not have siblings.

But the count() method gets a 1 integer anyway ?

So what am i doing wrong here?

I thought it should give me a 0 integer if there are no siblings.

Maybe i have forgotten something.

Thanks for all your help.

Share this post


Link to post
Share on other sites

I think page->siblings() returns the current page as well (not sure why)

So may have to do something like this (untested)

<?php $siblings = $page->siblings()->remove($page); ?>
  • Like 1

Share this post


Link to post
Share on other sites

I think page->siblings() returns the current page as well (not sure why)

So may have to do something like this (untested)

<?php $siblings = $page->siblings()->remove($page); ?>

You are absolutly right... but it is the default behavior of siblings() to return current page. I have to specify "false" to make it not do that.

I missed it. Thank you for your catch :)

https://processwire.com/api/ref/page/siblings/

I think you gave me the way to a solution. :)

solution:

<?PHP
$sibItems = $page->siblings(false);
?>

And now it works as i expected :)

Edited by EyeDentify
  • Like 1

Share this post


Link to post
Share on other sites

Pleased to hear it! (Didn't know about false, thanks).

No problem sir.

We are all here to help each other if we can. :)

Share this post


Link to post
Share on other sites

Alternatevely

if($page->parent->numChildren(true) > 1){ // true = only visible ones

}

This doesn't load the children/siblings pages just count.

  • Like 4

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