Can't create custom post type


#1

Hi, I am trying to create custom post type (Projects), did by instruction https://getkirby.com/docs/reference/panel/samples/portfolio, but I am getting this: https://take.ms/GvYHS ( Invalid section type (""))

Also create “Projects” page, but it’s showing this: https://take.ms/3qRm6 (This page has no blueprint setup yet)

Seems did all like described, can’t understand what is wrong.


#2

Please post your blueprint, probably some indentation error.


#3

Thanks, which one: projects.yml? or project.yml?


#4

Best both


#5

projects.yml https://take.ms/Apuz7
project.yml https://take.ms/JrT5O


#6

I’m missing the site/sections/project.yml blueprint. And by the way the second line in your pages/projects.yml should be removed.

On a side note, please don’t post images of your code but rather post it here as code blocks.


#7
headline: Projects
layout: cards
template: projects
image:
  query: page.images.template("cover").first
  ratio: 3/2
  cover: true
info: "{{ page.year }}"

#8

Ok, thanks. So, all is copied from instruction.


#9

First part seems to work after some changes: https://take.ms/OVhci

headline: Projects
type: pages
template: project
image:
  query: page.images.template("cover").first
  ratio: 3/2
  cover: true
info: "{{ page.year }}"

Update: but it’s probably wrong, as subpages are acting like pages: https://take.ms/gDmnwo
I am so frustrated, trying to find instructions, but they seem to lack details. For example in cookbook there is article about custom post types, but it shows output, and no step by step instruction how to create custom post type https://getkirby.com/docs/cookbook/templating/custom-post-types


#10

You are showing us an image, but I don’t know where that page list is shown. Looks like that pops up in another part of site?


#11

It was from panel, where all pages are listed. Seems very hard to understand for me, so I just copied all that I had for blog section, which works fine, and now projects seems to work :slight_smile:

But another question: which code would show the list of projects on Projects page? I mean what should be in /templates/projects.php?


#12

Something like

<?php foreach($page->children()->listed() as $project): ?>
<h1><?= $project->title() ?></h1>
<?php endforeach ?>

And then depending what information you store in your pages.

https://getkirby.com/docs/guide/templates/basics


#13

Thank you, it’s working! Also can you please help to understand, how to change this: when I am ready to publish Project, its position by default is last. I can do it first manually, but if it’s 100 projects, that’s not convenient, also normally newest project should be displayed first. So how can I change this setting and show newest Project first by default?

And also is it compulsory for any page or subpage to have this Draft stage? Or it can be set to be published straight?


#14

The draft state is compulsory, but you can use hooks to auto-publish pages directly after creation, in this case it would be the page.create:after hook.

If you use a date field in your projects, you could switch from (manual) sorting by number to sorting by date: https://getkirby.com/docs/reference/panel/blueprints/page#sorting

In the Panel or on frontend or both?


#15

Thank you, tried this sorting by date and it’s working:
num: '{{ page.created.toDate("Ymd") }}'
Yes, same sort should be in panel and on frontpage.

Still, sometimes there will be need of additional manual sorting, so is it possible to have sorting by “date created” by default, plus this: https://take.ms/jNbT7?

Or can be different: can be manual sorting, only that by default should be number 1.


#16

Date based sorting doesn’t work with manual sorting, it just wouldn’t make sense. If you want manual sorting, you have to use standard numbered sorting. Theoretically, there is no need to set the default number for new pages to 1, because you can simply flip the order (using desc or asc or flip()) to get the desired result.

Please note that sorting the pages manually by dragging them with those handels actually changing the folder number.

Would you mind posting your images here?


#17

I think clear with panel, but don’t understand, what code should I write in template? Tried different by these examples (https://getkirby.com/docs/reference/panel/blueprints/page#sorting), but not working.

I have:
<?php foreach($page->children()->listed() as $project): ?>
How should I change this to sort by date field desc?

Or would be best if they inherit the same sort from panel.

What do you mean about images? Which I should post?


#18
<?php foreach($page->children()->listed()->sortBy('date', 'desc') as $project): ?>

#19

Thank you! Working. First I confused with code for multiple fields. Also interesting, that it’s working with this code too:
<?php
$projects = page(‘projects’)->children()->listed()->sortBy(function ($page) {
return $page->date()->date();
}, ‘desc’);
foreach($projects as $project): ?>