concept-image / wp-archive-base
A package for adding a base for managing archive to a theme with Acorn (base view composer for archive, pagination, filters, etc...)
Requires
- php: >=8.0
- roots/acorn: >=4.3
Suggests
README
WP Archive Base is a Composer package that provides a base for managing archive and tawonomy archive for WordPress Bedrock/Sage applications. For exemple it adds a View Composer class to extend for an archive view composer, pagination, filters management, etc...
Installation
You can install the package via Composer:
composer require concept-image/wp-archive-base
Register ServiceProvider
Register CptNewsServiceProvider with:
wp acorn package:discover
Publish Command
After installing the package, you need to publish the view files and styles. Run the following command:
wp acorn vendor:publish --tag="wp-archive-base-template"
Usage
After publishing the files you will be able to use them for building archive page with blazing fast speed.
Define the View composer
Once you have an archive-your-cpt.blade.php, you juste need to create a View composer file, for that you can use the command from Acorn :
wp acorn make:composer YourCPT
In this View composer file you juste have to replace the extended class Composer by ListingComposer just like that :
class News extends ListingComposer
Then you need to define wich View will be associated with the ListingComposer (post archvie and taxonomy archive) :
/**
* List of views served by this composer.
*/
protected static $views = [
'archive-your-cpt',
'taxonomy-your-cpt-cat',
];
You also need to give the custom post type name :
protected array $post_types = ['your-cpt'];
And final step for the view composer you can define filter if you need somme in your listing :
Minimal setup for a filter :
protected array $post_types = ['news'];
public function filters()
{
AbstractFilter::setPostTypes($this->post_types);
return collect([
new TaxonomyFilter([
'name' => 'news-cat',
'format' => 'link',
'has_archive' => true,
'all_label' => __('Toutes les actualités'),
])
]);
}
Use the component in your archive blade
You have 4 components to use in your archive blade :
<x-listing.listing-hero :title="$title" />
: to display the page hero with the title<x-listing.listing-filters :post-type="$post_type" :filters="$filters" />
: to display the filters<x-listing.listing-layout :empty-message="__('Aucune actualité à afficher.')" class="listingNews"> your loop with the card of your CPT </x-listing.listing-layout>
: display your card element in a grid with 3 columns<x-listing.listing-pagination :pagination="$pagination" />
: to display the pagination
Changelog
Please refer to CHANGELOG for more information.
Contributing
Please refer to CONTRIBUTING for more information.
License
Please refer to LICENSE for more information.