Roots Discourse

Plugin Custom Archives isn’t functioning with Sage 9, because it seems like it can’t find the (blade) template

Hi,

I want to use the Custom Archives plugin in combination with Sage 9. But the plugin isn’t functioning with Sage 9, because it seems like it can’t find the right (blade) template.

I think this part needs to be changed, but I can’t get it working. I found a topic about Using get_template_part() in Sage 9 with Blade and tried to do the following:

I copied custom-archives.php to theme/app theme/app/custom-archives.php and included it in functions.php + added: namespace App;

And changed this part:

$directory = get_template_directory();
// Get the page template file.
$template = get_post_meta( $post->ID, '_wp_page_template', true );
// Fallback if no template given.
if ( '' == $template || false === $template || 'default' == $template ) {
    $template = 'page.php';
    // Does page.php not exist?
    if ( ! file_exists( $directory . '/' . $template ) ) {
        $template = 'index.php';
    }
}

To this:

$directory = get_template_directory();

// Get the page template file.
$template = get_post_meta( $post->ID, '_wp_page_template', true );

// Fallback if no template given.
if ( '' == $template || false === $template || 'default' == $template ) {

    $template = \App\template_path(locate_template('views/page.blade.php'));
    
    // Does page.php not exist?
    if ( ! file_exists( $directory . '/' . $template ) ) {
        $template = 'index.php';
    }
}

Does somebody know what to do?

Thanks!

Sage 9 echo’s the template rather than return it.
So my thinking is that you can use the filter on this line:

maybe something like:

add_filter('pre_custom_archive_template', function (string $template): void {
    // This gets the Controller variables for your template.
    $data = collect(get_body_class())->reduce(function ($data, $class) use ($template) {
        return apply_filters("sage/template/{$class}/data", $data, $template);
    }, []);

    // Now echo the template with our Controller data passed to the template.
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    echo template($template, $data);

    // We return an empty PHP file so that nothing extra is output by the plugin.
    return get_stylesheet_directory() . '/index.php';
});

I haven’t tested it but this is usually what to do for most situations like this.

Let me know how that goes!

Thanks, using the the filter didn’t work. I got i working with:

...
// Fallback if no template given.
if ( '' == $template || false === $template || 'default' == $template ) {

    // This gets the Controller variables for your template.
    $data = collect(get_body_class())->reduce(function ($data, $class) use ($template) {
        global $post_type;

        $data['query'] =   new \WP_Query( array(
            'post_type'     => $post_type,
        ) );

        return apply_filters("sage/template/{$class}/data", $data, $template);
    }, []);

    $template = \App\template(locate_template(array_map(function($template) use ($post_type) {
        return sprintf($template, $post_type);
    }, array(
        'views/archive-%s.blade.php',
        'views/page-archive-%s.blade.php',
        'views/page.blade.php',
        'views/index.blade.php',
    ))), $data);


}

// Now echo the template with our Controller data passed to the template.
echo $template;

// We return an empty PHP file so that nothing extra is output by the plugin.
return get_stylesheet_directory() . '/index.php';
...

Is there a beter way to return the query?

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