Created
July 16, 2018 18:02
-
-
Save seebz/54732f446d17a511b098e24ed67cafcb to your computer and use it in GitHub Desktop.
Test CPT WP avec recherche
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Fruits | |
Description: J'aime les fruits. | |
Version: 1.0 | |
Author: Seebz | |
*/ | |
/** | |
* Enregistre notre CPT `fruit`. | |
* | |
* Éventuellement aller à l'adresse `/wp-admin/options-permalink.php` pour purger le cache `WP_Rewrite` | |
*/ | |
function test_register_post_type() { | |
$post_type = 'fruit'; | |
$args = array( | |
'labels' => array( | |
'name' => _x( 'Fruits', 'post type general name' ), | |
'singular_name' => _x( 'Fruit', 'post type singular name' ), | |
), | |
'public' => true, | |
'capability_type' => 'post', | |
'map_meta_cap' => true, | |
'menu_position' => 5, | |
'menu_icon' => 'dashicons-admin-post', | |
'hierarchical' => false, | |
'query_var' => false, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ), | |
); | |
// Permet d'avoir une page listant les fruits à l'adresse `/fruits/` (template `archive-fruit.php` si existant) | |
// La fonction `get_post_type_archive_link( 'fruit' )` devrait retourner l'url de cette page | |
// | |
// Autorise les recherches suivantes : | |
// - `/fruits/?s=banane` | |
// - '/?post_type=fruit&s=banane' | |
// | |
// https://codex.wordpress.org/Function_Reference/register_post_type#has_archive | |
$args['has_archive'] = 'fruits'; | |
// Permet d'avoir une url du type `/fruits/banane/` au lieu de `/?post_type=fruit&p=42` | |
// | |
// https://codex.wordpress.org/Function_Reference/register_post_type#rewrite | |
$args['rewrite'] = array( 'slug' => 'fruits' ); | |
return register_post_type( $post_type, $args ); | |
} | |
add_action( 'init', 'test_register_post_type' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment