Jump to content

How do you sort sub-pages by publish date?


OpenBayou
 Share

Recommended Posts

I have a page called deals that has three sub-pages and those sub-pages have posts. Here's a short description of what my deals pages look like:

Deals
- Page A
- - Sub-page 1
- - Sub-page 2
- - Sub-page 3
- Page B
- - Sub-page 1
- Page C
- - Sub-page 1
- - Sub-page 2
- - Sub-page 3

Right now it's sorted by sub-pages in page A, sub-pages in page B and sub-pages in page C. How do I sort sub-pages by publish date? This is what I've done so far and it doesn't do anything.

<?php foreach($pages->get("/deals/")->children() as $post) { ?>
    <?php foreach($post->children("sort=-publish_date") as $child) { ?>
                  <?php echo $child->deals_category?>
            <?php } } ?>

Thanks for the help.

Link to comment
Share on other sites

Instead of...

foreach($post->children("sort=-publish_date") as $child) {

...try...

foreach($post->children()->sort("-publish_date") as $child) {

I assume "publish_date" is a custom field you have added to your template - otherwise perhaps you are looking for "created". More likely "published".

What's the deal with all the opening/closing PHP tags?

Link to comment
Share on other sites

1 hour ago, Robin S said:

What's the deal with all the opening/closing PHP tags?

Images, titles, description and adding <div>s

Edit: this is the final product

<?php foreach($pages->find("has_parent=/deals/, template=deals-post, sort=-created") as $child):?>
				<?php if(!$dealsTitle) : ?>
          <div class="top-id">
            <div class="title">
              <div class="title-id">
                <?php echo $child->parent->parent->title;?>
              </div>
              <div class="togger-button-deals togger-button">
                <img width="auto" height="20px" src="<?php echo $config->urls->templates?>imgs/arrow_down.svg">
              </div>
            </div>
            <div style="display:none" class="description-deals description">
              <?php echo $child->parent->parent->item_description;?>
            </div>
            <?php $dealsTitle = true;?>
          </div>
        <?php endif; ?>
				<div class="deals-item">
					<div class="deals-store-left">
						<img src="<?php echo $child->parent->post_image->first()->size(40,0)->url;?>">
          </div>
					<div class="deals-right">
            <div class="category">
                <div class="title">
                  <?php echo $child->deals_category?>
                </div>
            </div>
            <div class="deals-title">
              <?php echo $child->parent->title;?>: <?php echo $child->title;?>
            </div>
            <?php if($child->publish_until):?>
            <div class="deals-expire">
              <div class="title">
                expires: <?php echo date("M. d, Y",$child->getUnformatted("publish_until")); ?>
              </div>
            </div>
						<?php endif;?>
        	</div>
				</div>
			<?php endforeach; ?>

 

Link to comment
Share on other sites

Got it working! Thanks for the help.

// show each page from deals and using the template 'deals-post' that's sorted by date created.
<?php foreach($pages->find("has_parent=/deals/, template=deals-post, sort=-created") as $child):?>
	<?php echo $child->title?>
<?php endforeach; ?>

 

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