Jump to content

Create new child as top sibling rather than bottom?


MarcC
 Share

Recommended Posts

I have a script that is creating new pages under a specific parent, but the new pages are added to the bottom of the list of siblings. Is there a way to get new pages to default to the top of the sibling list rather than the bottom?

Failing that, I'd need to sort by a date field, but after I added the field, it doesn't show up in the list of "sort by" fields in the parent. (Actually wondering now...maybe the field has to belong to the parent as well as the child templates)

Thanks!

Link to comment
Share on other sites

The parent doesn't need the field. Your date field needs to be set to 'Autojoin' for it to be available as 'sort by' option in the parent page. You can find this when you edit the field, under 'advanced'.

Your original questions i'm not sure. I think this would require some hacking.

Link to comment
Share on other sites

There is a way.


// first get parent and it's first child
$parent = $pages->get(1001);
$first = $parent->child;

// create new page with same sort as first child
$p = new Page();
$p->template = $templates->get("basic-page");
$p->parent = $parent;
$p->title = "Make me first";
$p->sort = $first->sort;
$p->save();

// move first child to second position
$first->sort++;
$first->save();

Now your new page is at first position.

However this only work if you got manual sorting on parent of course.

  • Like 3
Link to comment
Share on other sites

You could also just set it to sort by "date created" (reverse) in the parent's sort setting?

The parent doesn't need the field. Your date field needs to be set to 'Autojoin' for it to be available as 'sort by' option in the parent page. You can find this when you edit the field, under 'advanced'.

This used to be the case up until last month, but is no longer. PW will let you sort by any fields even if they aren't auto-joined.

  • Like 2
Link to comment
Share on other sites

You could also just set it to sort by "date created" (reverse) in the parent's sort setting?

This used to be the case up until last month, but is no longer. PW will let you sort by any fields even if they aren't auto-joined.

Thx for the heads up on this.

Link to comment
Share on other sites

  • 3 weeks later...

Obviously I'm adding my 2 cents weeks later as I'm catching up on topics I've not read, but I had a similar situation with images.

The scenario was a gallery where the client wanted to add new photos over time, and they obviously wanted the new photos to appear first without reordering them manually, so the solution was:

foreach ($page->images->reverse() as $galleryimage) {
// code here as normal

Pretty sure you could use foreach $page->children()->reverse() as well, but might not be suitable for this particular scenario. That said, the info might be useful to others so I thought it was worth posting.

Link to comment
Share on other sites

The scenario was a gallery where the client wanted to add new photos over time, and they obviously wanted the new photos to appear first without reordering them manually, so the solution was:

Also wanted to mention that in 2.2 a "push-to-top" and "push-to-bottom" option was added for images. Hover over any image in a list and you'll see them as up/down arrows. Click the up arrow and it pushes it to be the first image at the top. Click the down arrow and it pushes it to the bottom. This makes your client's sorting scenario a lot easier, especially when dealing with lots of images.

post-2-0-41968300-1342023599_thumb.png

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