web-dev-qa-db-fra.com

Afficher les messages de type de message personnalisé Saisissez la page de catégorie sur le front-end

J'ai créé mon type de publication personnalisé (bizdirectory), créé une nouvelle publication (Test Business) et créé une nouvelle catégorie (Business Directory). Mais si j'ajoute la catégorie Répertoire des entreprises (slug = entreprise) à ma navigation principale, le lien indique, il est enregistré en tant que catégorie, mais le message "rien n'a été trouvé".

Si je recherche Test Business sur mon site (à l'aide de la barre de recherche sur la page d'accueil), il le trouve - l'URL qu'il indique est - http://www.domain.com/dev/? bizdirectory = test-business - mon code pour functions.php est ci-dessous:

// Load Custom Post Type 
function add_bizdirectory() {
    $labels = array( 
        'name' => __( 'Businesses', 'text_domain' ),
        'singular_name' => __( 'Business', 'text_domain' ),
        'add_new' => __( 'Add New Business', '${4:Name}', 'text_domain' ),
        'add_new_item' => __( 'Add New Business', 'text_domain}' ),
        'edit_item' => __( 'Edit Business', 'text_domain' ),
        'new_item' => __( 'New Business', 'text_domain' ),
        'view_item' => __( 'View Business', 'text_domain' ),
        'search_items' => __( 'Search Businesses', 'text_domain' ),
        'not_found' => __( 'No Businesses found', 'text_domain' ),
        'not_found_in_trash' => __( 'No Businesses found in Trash', 'text_domain' ),
        'parent_item_colon' => __( 'Parent Business:', 'text_domain' ),
        'menu_name' => __( 'Business Directory', 'text_domain' ),
    );
    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => 'description',
        'taxonomies' => array( 'category' ),
        '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', 
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'revisions', 'post-formats' ),
    );
    register_post_type( 'bizdirectory', $args );
}
add_action( 'init', 'add_bizdirectory' );
// End

J'ai essayé toutes les options de règles de réécriture, à l'exception de celle dans laquelle je supprime manuellement les règles de réécriture de wp_options - elles n'apparaissaient pas dans wp_options.

J'apprécie toute aide ou conseil que vous pourriez me donner :)

1
Paxjah

J'ai trouvé la réponse, j'avais complètement mal compris les différences entre les types de publication standard et les types de publication personnalisés, ainsi que leurs taxonomies. J'ai trouvé plusieurs articles très utiles et le code ci-dessous est ce avec quoi j'ai fini, ainsi que quelques articles que j'ai trouvés utiles. J'espère que d'autres trouveront cela utile.

// Load Custom Post Type 
function add_directory() {
    $labels = array( 
    'name'               => __( 'Businesses', 'text_domain' ),
        'singular_name'      => __( 'Business', 'text_domain' ),
        'add_new'            => __( 'Add New Business', '${4:Name}', 'text_domain' ),
        'add_new_item'       => __( 'Add New Business', 'text_domain}' ),
        'edit_item'          => __( 'Edit Business', 'text_domain' ),
        'new_item'           => __( 'New Business', 'text_domain' ),
        'view_item'          => __( 'View Business', 'text_domain' ),
        'search_items'       => __( 'Search Businesses', 'text_domain' ),
        'not_found'          => __( 'No Businesses found', 'text_domain' ),
        'not_found_in_trash' => __( 'No Businesses found in Trash', 'text_domain' ),
        'parent_item_colon'  => __( 'Parent Business:', 'text_domain' ),
        'menu_name'          => __( 'Business Directory', 'text_domain' ),
    );
    $args = array( 
        'labels'              => $labels,
        'hierarchical'        => true,
        'description'         => 'description',
        'taxonomies'          => array( 'biz-cat' ),
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_position'       => 5,
        //'menu_icon'         => '',
        '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', 'custom-fields', 'revisions', 'post-formats' ),
    );
    register_post_type( 'bizdirectory', $args );
}
add_action( 'init', 'add_directory' );

// Create Custom Taxonomy
function directory_create_taxonomies() 
{
    register_taxonomy( 'biz-cat', array( 'bizdirectory' ), array(
        'hierarchical' => true,
        'label' => 'Business Categories',
        'singular_name' => 'Business Category',
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'biz-cat' )
    ));
}
add_action( 'init', 'directory_create_taxonomies', 0 );

// 'bizdirectory' is the registered post type name
function directory_columns($defaults) {
    // 'biz-cat' is the registered taxonomy name
    $defaults['biz-cat'] = 'Business Category';
    return $defaults;
}
function directory_custom_column($column_name, $post_id) {
    $taxonomy = $column_name;
    $post_type = get_post_type($post_id);
    $terms = get_the_terms($post_id, $taxonomy);

    if ( !empty($terms) ) {
        foreach ( $terms as $term )
            $post_terms[] = "<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " . esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
        echo join( ', ', $post_terms );
    }
    else echo '<i>Not assigned.</i>';
}
add_filter( 'manage_project_posts_columns', 'directory_columns' );
add_action( 'manage_project_posts_custom_column', 'directory_custom_column', 10, 2 );
0
Paxjah

Il vaut mieux séparer les catégories pour les publications et utiliser des types de taxonomie personnalisés pour les catégories CPT.

Vous souhaitez ajouter cette ligne à votre code cpt de registre

'taxonomies'   => array( 'bizdirectory-type' ),

Remplacer cette ligne dans votre code.

'taxonomies' => array( 'category' ),

Et puis ajoutez ce code dans votre functions.php

add_action( 'init', 'wpsites_custom_taxonomy_types' );
function wpsites_custom_taxonomy_types() {

register_taxonomy( 'bizdirectory-type', 'bizdirectory',
    array(
        'labels' => array(
            'name'          => _x( 'Types', 'taxonomy general name', 'wpsites' ),
            'add_new_item'  => __( 'Add New Bizdirectory Type', 'wpsites' ),
            'new_item_name' => __( 'New Bizdirectory Type', 'wpsites' ),
        ),
        'exclude_from_search' => true,
        'has_archive'         => true,
        'hierarchical'        => true,
        'rewrite'             => array( 'slug' => 'bizdirectory-type', 'with_front' => false ),
        'show_ui'             => true,
        'show_tagcloud'       => false,
    ));

}
1
Brad Dalton