Last active
July 29, 2020 09:38
-
-
Save NielsPilon/9806a0f780f153f859e7707d469d514a to your computer and use it in GitHub Desktop.
Custom Post example
This file contains 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
/*Assortiment Custom Post Type*/ | |
function madiba_custom_post_assortiment() { | |
$labels = array( | |
'name' => _x( 'Assortiment', 'post type general name' ), | |
'singular_name' => _x( 'Assortiment', 'post type singular name' ), | |
'add_new' => _x( 'Nieuwe bloem', 'crop' ), | |
'add_new_item' => __( 'Nieuwe bloem toevoegen' ), | |
'edit_item' => __( 'Wijzig bloem' ), | |
'new_item' => __( 'Nieuw item' ), | |
'all_items' => __( 'Alle bloemen' ), | |
'view_item' => __( 'Bekijk bloemen' ), | |
'search_items' => __( 'Zoeken in assortiment' ), | |
'not_found' => __( 'Niets gevonden ' ), | |
'not_found_in_trash' => __( 'Niets gevonden in de prullenbak' ), | |
'parent_item_colon' => '', | |
'menu_name' => 'Assortiment' | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => 'Nieuwe bloem toevoegen', | |
'public' => true, | |
'menu_position' => 5, | |
'supports' => array( 'title', 'editor', 'thumbnail' ), | |
'show_in_rest' => true, | |
'has_archive' => 'assortiment', | |
'rewrite' => [ | |
'slug' => __( 'assortiment/%soort%', 'jointswp' ), | |
'with_front' => false, | |
], | |
'hierarchical' => true, | |
'query_var' => true, | |
//'rewrite' => true, | |
//'publicly_queryable' => false, | |
); | |
register_post_type( 'assortiment', $args ); | |
} | |
add_action( 'init', 'madiba_custom_post_assortiment' ); | |
/* Assortiment custom taxonomy */ | |
function madiba_taxonomies_assortiment_soort() { | |
$labels = array( | |
'name' => _x( 'Soorten ', 'taxonomy general name' ), | |
'singular_name' => _x( 'Soort', 'taxonomy singular name' ), | |
'search_items' => __( 'Zoeken in alle soorten' ), | |
'all_items' => __( 'Alle soorten' ), | |
'parent_item' => __( 'Parent soort type' ), | |
'parent_item_colon' => __( 'Parent soort type:' ), | |
'edit_item' => __( 'Wijzig soort' ), | |
'update_item' => __( 'Soort updaten ' ), | |
'add_new_item' => __( 'Nieuwe soort toevoegen' ), | |
'new_item_name' => __( 'Nieuwe soort' ), | |
'menu_name' => __( 'Soorten' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => true, | |
'public' => true, | |
'show_in_rest' => true, | |
'query_var' => 'soort', | |
'rewrite' => [ 'slug' => __( 'assortiment', 'jointswp'), ], | |
'_builtin' => false, | |
); | |
register_taxonomy( 'soort', 'assortiment', $args ); | |
} | |
add_action( 'init', 'madiba_taxonomies_assortiment_soort', 0 ); | |
/* Permalink filter crops & species */ | |
add_filter('post_link', 'madiba_assortiment_permalink', 1, 3); | |
add_filter('post_type_link', 'madiba_assortiment_permalink', 1, 3); | |
function madiba_assortiment_permalink($permalink, $post_id, $leavename) { | |
if (strpos($permalink, '%soort%') === FALSE) return $permalink; | |
// Get post | |
$post = get_post($post_id); | |
if (!$post) return $permalink; | |
// Get taxonomy terms | |
$terms = wp_get_object_terms($post->ID, 'soort'); | |
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) | |
$taxonomy_slug = $terms[0]->slug; | |
else $taxonomy_slug = 'uncategorised'; | |
return str_replace('%soort%', $taxonomy_slug, $permalink); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment