WhatsApp: +39 339 289 627 8
E-mail: info@creazione-siti-web.eu

 

Custom Post Type con Custom Taxonomy

 

Per creare custom post type:

function prefix_register_name() { $labels = array( 'name' => __( 'Plural Name', 'text-domain' ), 'singular_name' => __( 'Singular Name', 'text-domain' ), 'add_new' => _x( 'Add New Singular Name', 'text-domain', 'text-domain' ), 'add_new_item' => __( 'Add New Singular Name', 'text-domain' ), 'edit_item' => __( 'Edit Singular Name', 'text-domain' ), 'new_item' => __( 'New Singular Name', 'text-domain' ), 'view_item' => __( 'View Singular Name', 'text-domain' ), 'search_items' => __( 'Search Plural Name', 'text-domain' ), 'not_found' => __( 'No Plural Name found', 'text-domain' ), 'not_found_in_trash' => __( 'No Plural Name found in Trash', 'text-domain' ), 'parent_item_colon' => __( 'Parent Singular Name:', 'text-domain' ), 'menu_name' => __( 'Plural Name', 'text-domain' ), ); $args = array( 'labels' => $labels, 'hierarchical' => false, 'description' => 'description', 'taxonomies' => array(), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_admin_bar' => true, 'menu_position' => null, 'menu_icon' => null, 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'post', 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'trackbacks', 'comments', 'revisions', 'page-attributes', 'post-formats', ), ); register_post_type( 'slug-post', $args ); } add_action( 'init', 'prefix_register_name' ); //attivare miniatura dei post per tutta il tema hook after_setup_theme: add_theme_support('post-thumbnails');

Per creare taxonomy:

function my_taxonomies_name() { $labels = array( 'name' => _x( 'Plural Name Taxonomy', 'Taxonomy plural name', 'text-domain' ), 'singular_name' => _x( 'Singular Name Taxonomy', 'Taxonomy singular name', 'text-domain' ), 'search_items' => __( 'Search Plural Name', 'text-domain' ), 'popular_items' => __( 'Popular Plural Name', 'text-domain' ), 'all_items' => __( 'All Plural Name', 'text-domain' ), 'parent_item' => __( 'Parent Singular Name', 'text-domain' ), 'parent_item_colon' => __( 'Parent Singular Name', 'text-domain' ), 'edit_item' => __( 'Edit Singular Name', 'text-domain' ), 'update_item' => __( 'Update Singular Name', 'text-domain' ), 'add_new_item' => __( 'Add New Singular Name', 'text-domain' ), 'new_item_name' => __( 'New Singular Name Name', 'text-domain' ), 'add_or_remove_items' => __( 'Add or remove Plural Name', 'text-domain' ), 'choose_from_most_used' => __( 'Choose from most used Plural Name', 'text-domain' ), 'menu_name' => __( 'Singular Name Taxonomy', 'text-domain' ), ); $args = array( 'labels' => $labels, 'public' => true, 'show_in_nav_menus' => true, 'show_admin_column' => false, 'hierarchical' => true, 'show_tagcloud' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'query_var' => true, 'capabilities' => array(), ); register_taxonomy( 'taxonomy-slug', array( 'slug-post' ), $args ); } add_action( 'init', 'my_taxonomies_name' );

Per stampare taxonomy nei template del tema:

print_r(get_terms( array('taxonomy-slug'))); $terms = get_terms(array( 'taxonomy' => 'taxonomy-slug', 'hide_empty' => false, )); if (!is_wp_error($terms) && !empty($terms)) { foreach ($terms as $term) { $link = get_term_link($term); a tag href="' . esc_url($link) . '"; echo esc_html($term->name); echo 'a tag chiusura'; } }