FORUMS CLOSED DUE TO SPAM. YOU STILL CAN ADD POST!

Any way to get a sortable date as a page part?

Feed 23 posts, 6 voices

Avatar
486 posts

I’m still wondering if might be possible to do events using page parts. But in order to do that I need the date to be sortable. Is there any way to store a date as a page part and be able to sort by that?

 
Avatar
257 posts

Hi Ricks, it may be worth exploring the tags system to do this – set out a date structure for the events then do a php sort on the page you’re calling – I’m trying to figure out a way to use a plugin to manage events but am not quite there yet. The plugin is developed up to the stage of having events in the database and an admin plugin to add / edit them. I am hopefully going to release it in November but if you need something now, tagging may be a good step…

 
Avatar
486 posts

thanks andy. i didn’t think about tags. i’ll check that out.

 
Avatar
6 posts

I had the same problem, and tags didn’t seem right to me, semantically.

Anyway, I cleaned the code up a little and put it in a plugin.

It provides the function cs_children_by_part.

Example: The main page is “Events”, every event is a direct subpage of “Events” and has a part named “date”, which contains the date of the event in the format “YYYY-MM-DD”.

cs_children_by_part($this, 'date') -> all events ordered by the content of their date part, ascending order, when called from the “Events” page.

cs_children_by_part(find_page_by_uri('/events'), 'date', 'desc', 5) -> the 5 newest Event pages when called from another page.

Let me know if it works for you.

 
Avatar
1493 posts

Hi Christian – sounds like a great idea! I tried it on a dev site, though … I think I need more documentation! :) When I used

<?php cs_children_by_part($this, 'date'); ?>

in the parent page, then I got nothing. But when I tried (just guessing):

<?php echo cs_children_by_part($this, 'date'); ?>

then all I got “echoed” was array, of course. This is with 0.9.4, the parent page is called “tour-info”, and the page part is “date”. Any ideas?

 
Avatar
6 posts

Oops, sorry. I should have mentioned that the function returns an array of Page objects.

Use it just like you would the array returned by $page->children() or similar:

<?php foreach (cs_children_by_part($this, 'date') as $event): ?>
<h2><?php echo $event->title(), " ", $event->content('date') ?></h2>
<?php echo $event->content() ?>
<hr />
<?php endforeach ?>
 
Avatar
1493 posts

Thanks for that! I knew there had to be a foreach lurking somewhere. It’s working nicely!

One thought for an enhancement: would there be any chance of adding a param to set the display order of the date elements? The “input” is YYYY-MM-DD, of course. It would be nice to display that as dd-mm-yyyy, or “mm dd, yyyy”, or whatever. What do you think?

 
Avatar
1493 posts

Also, a nice tweak to Christian’s code:

<?php foreach (cs_children_by_part($this,'date') as $event): ?>
<h4><?php echo $event->link(),', ',$event->content('date'); ?></h4>
<hr />
<?php endforeach ?>

That gets you just a listing, with date, of the “event” with a click-through to its own page. This is lotsa fun! :)

Also, (d’oh!) I realized I needed to have the page filter for the date part set to -none-, otherwise the date got wrapped in <p>...</p>, and thus bumped onto a different line from the title/link. (I usually use Markdown.)

 
Avatar
6 posts

Cool that you find it useful!

As for the display order, you have to munge it by hand. One way to get at the parts of the date is list($year, $month, $day) = explode('-', $event->content('date')), then you can display those however you want.

 
Avatar
486 posts

thanks christian. i’ll try out what you’ve got soon as i get a chance.

 
Avatar
10 posts

Is there a way to exclude certain pages from a given query, based on a part?

 
Avatar
1493 posts

Is there a way to exclude certain pages from a given query, based on a part?

In the example above, the plugin is looking for a “date” page-part. If one of the “children” pages does not have that part, then it is omitted from the the foreach loop. So you exclude pages by deleting the page-part (i.e., that tab using the red “-” icon when editing the page). I.e., no “date” page-part, then it doesn’t show up.

 
Avatar
4 posts

Help! I think I’m trying to make this harder than it is. I want sort children by title or slug in ascending order. How do I go about doing that?

Thanks!

 
Avatar
1493 posts

@cdembinski – I think it is easier than it looks.

In the documentation, you get the impression that you can only order on date elements when displaying a list of sub-pages. In fact, you can also use title or slug to order the list. The following code:

<ul id="nav_sub">
<?php foreach ($this->children(array('order' => 'page.title ASC')) as $child): ?>
    <li><?php echo $child->link($child->title, (url_start_with($child->url) ? ' class="current"': null)); ?></li>
<?php endforeach; ?>
</ul>

will sort by the Title of the page in ascending order. But you could equally alter that second line to read:

<?php foreach ($this->children(array('order' => 'page.slug DESC')) as $child): ?>

To sort by slug in descending order, etc. Have a look at the “page” table in phpMyAdmin to see the other values you could use.

Hope that helps! [And documentation updated!]

 
Avatar
4 posts

Thanks for the response David.

However, if I copy and paste this into my page I get nothing. Does it matter if the “parent” page I’m putting this into is a child itself?

Here’s what I’m trying to do. I’m setting up a directory, where directory is my “home” page. Category pages are set up as children pages of the directory. I want to set the individual listings for each category up as a child of the category so that they will be sorted alphabetically as I continue to add new listings.

Example:
I.Directory (HOME)

A. Accessories (Child)

1. ABC Shop Listing (Child of Accessories)

2. DEF Shop Listing (Child of Accessories)

Hope this makes sense. I do realize that I will have to echo the content of the page and not just the title.

Thanks,
Chris

 
Avatar
1493 posts

Hi Chris – If you put the code from my previous post in the “body” of your “A. Accessories” page (from your example), you should get a listing like:

  • ABC Shop Listing
  • DEF Shop Listing

where those are also linked to the “ABC” and “DEF” pages, in alphabetical order, if they are “children” to the “Accessories” page.

Do you have the site live online? or are you developing on “localhost”? If we can take a look at what you are attempting, the solutions might also be more helpful!

 
Avatar
4 posts

Here’s a link to the page that I pasted the code into. http://www.shopindieonline.com/indi/?mens-shop.html

This page has two hidden child pages.

 
Avatar
1493 posts

This page has two hidden child pages.

And if these are the two you want listed, that explains the problem! Have a look at the docs on page status definitions. “Hidden” pages are not found by the $this->children method.

If you want the pages to be “hidden”, you need to use a variation on the code above:

<ul id="nav_sub">
<?php foreach ($this->children(array('order' => 'page.title ASC'),array('order' => 'page.title ASC'),true) as $child): ?>
    <li><?php echo $child->link($child->title, (url_start_with($child->url) ? ' class="current"': null)); ?></li>
<?php endforeach; ?>
</ul>

The additional array() with the ,true tells Frog to include “hidden” pages with the $this->children method. (It’s in the small print in the doc page listed above!)

I see you have a lot of “categories”, though. With this technique, you will need to put that code in every “category” page. I suppose there are other options, but it looks like all your pages are created now so I don’t think any of the “time savers” will now save you time!

Hope that works!

 
Avatar
4 posts

Thanks David! You’re fabulous!!

Guess I need to go back and read the documentation more carefully.