• WordPress 4.9.4

    Hi, I got issues by creating new Custom Post Type (CPT) with this code.
    For example I cant’t update permalinks correctly.
    Does anyone notice what could be wrong ?

    <?php
    
    add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'presta',
        array(
          'labels' => array(
            'name' => __( 'Prestas' ),
            'singular_name' => __( 'Presta' )
          ),
          'public' => true
        )
      );
    register_taxonomy( 'couleur', 'produit', array( 'hierarchical' => true, 'label' => 'Couleur', 'query_var' => true, 'rewrite' => true ) );
    }
    
    add_action( 'init', 'register_cpt_produit' );
    
    function register_cpt_produit() {
    
        $labels = array( 
            'name' => _x( 'Prestas', 'presta' ),
            'singular_name' => _x( 'Presta', 'presta' ),
            'add_new' => _x( 'Ajouter', 'projet' ),
            'add_new_item' => _x( 'Ajouter un presta', 'presta' ),
            'edit_item' => _x( 'Editer un presta', 'presta' ),
            'new_item' => _x( 'Nouveau presta', 'presta' ),
            'view_item' => _x( 'Voir le presta', 'presta' ),
            'search_items' => _x( 'Rechercher un presta', 'presta' ),
            'not_found' => _x( 'Aucun presta trouvé', 'presta' ),
            'not_found_in_trash' => _x( 'Aucun presta dans la corbeille', 'presta' ),
            'parent_item_colon' => _x( 'Presta parent :', 'presta' ),
            'menu_name' => _x( 'Prestas', 'presta' ),
        );
    
        $args = array( 
            'labels' => $labels,
            'hierarchical' => false,
            'description' => 'Les prestas de ma boutique.',
            'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'revisions' ),
            'taxonomies' => array( 'category', 'post_tag' ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_position' => 5,
    
            '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'
        );
    
        register_post_type( 'presta', $args );
    }
    
    ?>
Viewing 1 replies (of 1 total)
  • Hi there,

    you are calling register_post_type twice with the post type name “presta”. In addition, you are adding a custom taxonomy “couleur” to a post type named “produit”, but there isn’t any declaration of this post type in your code.

    For permalinks keep in mind that once you registered a new custom post type, you have to flush the permalinks cache. That could be achieved by visiting settings -> permalinks and hit “save” in the backend or by adding flush_rewrite_rules(); to your code.

    Regards,
    Marcus

Viewing 1 replies (of 1 total)

The topic ‘permalink issue with CPT Custom Post Type’ is closed to new replies.