admin管理员组

文章数量:1388853

This is my first post, so maybe I'll miss some informations, sorry :bowing.

I have almost the same issue as Paul T did (Permalinks: custom post type -> custom taxonomy -> post), namely :

I have a custom post type called sioban_cpt, which have a custom taxonomy sioban_taxo.

A sioban_cpt can have more than one sioban_taxo.

I would like my urls to route in this manner:

www.mysite/sioban_cpt/  =>  archive-sioban-cpt.php
www.mysite/sioban_cpt/%sioban_taxo%/ => taxonomy-sioban_taxo.php
**www.mysite/sioban_cpt/%postname%/ => single-sioban_cpt.php**

So :

www.mysite/sioban_cpt/ => shows all posts with sioban_cpt post-type (this works)
www.mysite/sioban_cpt/foo/ => shows all posts with the foo taxonomy(this works)
www.mysite/sioban_cpt/bar => shows the indivual post under the sioban_cpt post-type (this doesn't work, it display the error template, although www.mysite/sioban_cpt/bar is the shown permalink in the WP admin post edition page)

As Paul T, I try to avoid plugins.

I don't know if a solution is possible, because I want the taxonomy and the posts of the CPT both as descendants of the cpt, and conversely I don't want the posts of the CPT as children of the taxo.

Here is my code in function.php :

/**
 * create new custom taxonomy (has to be called before CPT declaration)
 *
 * @return void
 */
function si0b_ct_year()
{
    /* Property Type */
    $labels = array(
       'name'                       => _x('Years', 'Taxonomy General Name', 'siobanone'),
       'singular_name'              => _x('Year', 'Taxonomy Singular Name', 'siobanone'),
       'menu_name'                  => __('Année de participation', 'siobanone'),
       'all_items'                  => __('Toutes les années', 'siobanone'),
       'new_item_name'              => __('Nouvelle année', 'siobanone'),
       'add_new_item'               => __('Ajouter une nouvelle année', 'siobanone'),
       'edit_item'                  => __('Modifier l\'année', 'siobanone'),
       'update_item'                => __('Actualiser l\année', 'siobanone'),
       'view_item'                  => __('Voir l\année', 'siobanone'),
       'separate_items_with_commas' => __('Separate years with commas', 'siobanone'),
       'add_or_remove_items'        => __('Add or remove years', 'siobanone'),
       'search_items'               => __('Search Years', 'siobanone'),
       'not_found'                  => __('Not Found', 'siobanone'),
       'no_terms'                   => __('No years', 'siobanone'),
       'items_list'                 => __('Years list', 'siobanone'),
       'items_list_navigation'      => __('Years list navigation', 'siobanone'),
   );
    $rewrite = array(
       'slug'                => __('aec-year', 'siobanone'),
       'with_front'          => false
   );
    $args = array(
       'labels'                     => $labels,
       'public'                     => true,
       'show_ui'                    => true,
       'show_admin_column'          => true,
       'show_in_nav_menus'          => true,
       'show_in_quick_edit'         => true,
       'rewrite'                    => $rewrite,
       'show_in_rest'               => true,
       'has_archive'                => true,
   );
    register_taxonomy('aec-year', 'artists', $args);
}

add_action('init', 'si0b_ct_year', 10);

/*To create new custom post type */
function si0b_cpt_artists()
{
    // CPT : artists

    $labels = array(
       'name'                => _x('Artists', 'Post Type General Name', 'siobanone'),
       'singular_name'       => _x('Artist', 'Post Type Singular Name', 'siobanone'),
       'menu_name'           => __('Artists', 'siobanone'),
       'name_admin_bar'      => __('Artists', 'siobanone'),
       'parent_item_colon'   => __('Parent Item:', 'siobanone'),
       'all_items'           => __('All artists', 'siobanone'),
       'add_new_item'        => __('Ajouter nouvel.le artiste', 'siobanone'),
       'add_new'             => __('Ajouter', 'siobanone'),
       'new_item'            => __('New Item', 'siobanone'),
       'edit_item'           => __('Edit Item', 'siobanone'),
       'update_item'         => __('Update Item', 'siobanone'),
       'view_item'           => __('View Item', 'siobanone'),
       'search_items'        => __('Search Item', 'siobanone'),
       'not_found'           => __('Not found', 'siobanone'),
       'not_found_in_trash'  => __('Not found in Trash', 'siobanone'),
   );
    $rewrite = array(
       'slug'                => _x('artistes/%aec-year%', 'siobanone'),
       'with_front'          => false
   );
    $args = array(
       'label'               => __('artists', 'siobanone'),
       'description'         => _x('Artists', 'siobanone'),
       'labels'              => $labels,
       'supports'            => array('title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'excerpt'),
       'taxonomies'          => array('aec-year'),
       'hierarchical'        => false,
       'public'              => true,
       'show_ui'             => true,
       'show_in_menu'        => true,
       'menu_position'       => 5,
       'menu_icon'           => 'dashicons-images-alt',
       'show_in_admin_bar'   => true,
       'show_in_nav_menus'   => true,
       'can_export'          => true,
       'has_archive'         => 'artistes',
       'exclude_from_search' => false,
       'rewrite'             => $rewrite,
   );
    register_post_type('artists', $args);
}

add_action('init', 'si0b_cpt_artists', 10);

/**
 *  add a filter to post_type_link to substitute the taxo aec-year in individual CPT artists permalinks
 *
 * @param [type] $post_link
 * @param [type] $post
 * @return void
 */
function si0b_artists_permalinks($post_link, $post)
{
    if (is_object($post) && $post->post_type == 'artists') {
        $terms = wp_get_object_terms($post->ID, 'aec-year');
        if ($terms) {
            return str_replace('%aec-year%', $terms[0]->slug, $post_link);
        }
    }
    return $post_link;
}
add_filter('post_type_link', 'si0b_artists_permalinks', 1, 2);

Thanks for your help...

This is my first post, so maybe I'll miss some informations, sorry :bowing.

I have almost the same issue as Paul T did (Permalinks: custom post type -> custom taxonomy -> post), namely :

I have a custom post type called sioban_cpt, which have a custom taxonomy sioban_taxo.

A sioban_cpt can have more than one sioban_taxo.

I would like my urls to route in this manner:

www.mysite/sioban_cpt/  =>  archive-sioban-cpt.php
www.mysite/sioban_cpt/%sioban_taxo%/ => taxonomy-sioban_taxo.php
**www.mysite/sioban_cpt/%postname%/ => single-sioban_cpt.php**

So :

www.mysite/sioban_cpt/ => shows all posts with sioban_cpt post-type (this works)
www.mysite/sioban_cpt/foo/ => shows all posts with the foo taxonomy(this works)
www.mysite/sioban_cpt/bar => shows the indivual post under the sioban_cpt post-type (this doesn't work, it display the error template, although www.mysite/sioban_cpt/bar is the shown permalink in the WP admin post edition page)

As Paul T, I try to avoid plugins.

I don't know if a solution is possible, because I want the taxonomy and the posts of the CPT both as descendants of the cpt, and conversely I don't want the posts of the CPT as children of the taxo.

Here is my code in function.php :

/**
 * create new custom taxonomy (has to be called before CPT declaration)
 *
 * @return void
 */
function si0b_ct_year()
{
    /* Property Type */
    $labels = array(
       'name'                       => _x('Years', 'Taxonomy General Name', 'siobanone'),
       'singular_name'              => _x('Year', 'Taxonomy Singular Name', 'siobanone'),
       'menu_name'                  => __('Année de participation', 'siobanone'),
       'all_items'                  => __('Toutes les années', 'siobanone'),
       'new_item_name'              => __('Nouvelle année', 'siobanone'),
       'add_new_item'               => __('Ajouter une nouvelle année', 'siobanone'),
       'edit_item'                  => __('Modifier l\'année', 'siobanone'),
       'update_item'                => __('Actualiser l\année', 'siobanone'),
       'view_item'                  => __('Voir l\année', 'siobanone'),
       'separate_items_with_commas' => __('Separate years with commas', 'siobanone'),
       'add_or_remove_items'        => __('Add or remove years', 'siobanone'),
       'search_items'               => __('Search Years', 'siobanone'),
       'not_found'                  => __('Not Found', 'siobanone'),
       'no_terms'                   => __('No years', 'siobanone'),
       'items_list'                 => __('Years list', 'siobanone'),
       'items_list_navigation'      => __('Years list navigation', 'siobanone'),
   );
    $rewrite = array(
       'slug'                => __('aec-year', 'siobanone'),
       'with_front'          => false
   );
    $args = array(
       'labels'                     => $labels,
       'public'                     => true,
       'show_ui'                    => true,
       'show_admin_column'          => true,
       'show_in_nav_menus'          => true,
       'show_in_quick_edit'         => true,
       'rewrite'                    => $rewrite,
       'show_in_rest'               => true,
       'has_archive'                => true,
   );
    register_taxonomy('aec-year', 'artists', $args);
}

add_action('init', 'si0b_ct_year', 10);

/*To create new custom post type */
function si0b_cpt_artists()
{
    // CPT : artists

    $labels = array(
       'name'                => _x('Artists', 'Post Type General Name', 'siobanone'),
       'singular_name'       => _x('Artist', 'Post Type Singular Name', 'siobanone'),
       'menu_name'           => __('Artists', 'siobanone'),
       'name_admin_bar'      => __('Artists', 'siobanone'),
       'parent_item_colon'   => __('Parent Item:', 'siobanone'),
       'all_items'           => __('All artists', 'siobanone'),
       'add_new_item'        => __('Ajouter nouvel.le artiste', 'siobanone'),
       'add_new'             => __('Ajouter', 'siobanone'),
       'new_item'            => __('New Item', 'siobanone'),
       'edit_item'           => __('Edit Item', 'siobanone'),
       'update_item'         => __('Update Item', 'siobanone'),
       'view_item'           => __('View Item', 'siobanone'),
       'search_items'        => __('Search Item', 'siobanone'),
       'not_found'           => __('Not found', 'siobanone'),
       'not_found_in_trash'  => __('Not found in Trash', 'siobanone'),
   );
    $rewrite = array(
       'slug'                => _x('artistes/%aec-year%', 'siobanone'),
       'with_front'          => false
   );
    $args = array(
       'label'               => __('artists', 'siobanone'),
       'description'         => _x('Artists', 'siobanone'),
       'labels'              => $labels,
       'supports'            => array('title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'excerpt'),
       'taxonomies'          => array('aec-year'),
       'hierarchical'        => false,
       'public'              => true,
       'show_ui'             => true,
       'show_in_menu'        => true,
       'menu_position'       => 5,
       'menu_icon'           => 'dashicons-images-alt',
       'show_in_admin_bar'   => true,
       'show_in_nav_menus'   => true,
       'can_export'          => true,
       'has_archive'         => 'artistes',
       'exclude_from_search' => false,
       'rewrite'             => $rewrite,
   );
    register_post_type('artists', $args);
}

add_action('init', 'si0b_cpt_artists', 10);

/**
 *  add a filter to post_type_link to substitute the taxo aec-year in individual CPT artists permalinks
 *
 * @param [type] $post_link
 * @param [type] $post
 * @return void
 */
function si0b_artists_permalinks($post_link, $post)
{
    if (is_object($post) && $post->post_type == 'artists') {
        $terms = wp_get_object_terms($post->ID, 'aec-year');
        if ($terms) {
            return str_replace('%aec-year%', $terms[0]->slug, $post_link);
        }
    }
    return $post_link;
}
add_filter('post_type_link', 'si0b_artists_permalinks', 1, 2);

Thanks for your help...

Share Improve this question asked Mar 4, 2020 at 18:45 SiobanSioban 112 bronze badges 6
  • Have a look at this question (and the answer linked from that question). – Sally CJ Commented Mar 5, 2020 at 0:32
  • Thanks a lot Sally ! I can't test the code before next week, but it seems to be the solution

    本文标签: