Roots Discourse

Sage 10 - How to add folder/directories inside App

So I want to create a custom post type folder, in future I mgiht want to create more of different sorts.

Now, my question is, how do I register/create new folders such as PostTypes inside the ‘App’ folder, and make sage use that?

This is what I got:

Either provide the full path to your file (i.e. PostTypes/case-studies) or modify this loader to load files as you like.

I’m not that well versed with the code above.

How would you go about it?

My idea is to have somethingg like:

    collect(['PostTypes'])
    ->each(function ($folder, $file) {
        if (! locate_template($file = "app/{$folder}/case-studies.php", true, true)) {
            wp_die(
                /* translators: %s is replaced with the relative file path */
                sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file)
            );
        }
    });

But then, how would you make the ‘file’ dynamic? That’s the error I get when I replace case-studies with {$file}: Error locating app/PostTypes/0.php for inclusion..

The code that ships with Sage that you’ve screenshotted is only intended to load files, it has no built in capacity to understand directories, or to load files from a directory. It iterates over an array of strings, concatenates a string in each iteration, and tries to load the file at the resulting path. If you want more complex behavior than that, you’ll need to implement it yourself.

To load the file you’ve shown, you would do this:

- collect(['helpers', 'setup', 'filters', 'admin'])
+ collect(['helpers', 'setup', 'filters', 'admin', 'PostTypes/case-studies'])

To understand why your experiment is returning an error, you should read up on Laravel Collections (which is what collect() creates) and how their methods work: https://laravel.com/docs/8.x/collections#available-methods

2 Likes

This topic was automatically closed after 42 days. New replies are no longer allowed.