如何编写插件/固定链接:定制贴子类型->定制分类和定制贴子类型->定制分类(一个CPT和多个分类)?

时间:2020-03-04 作者:Sioban

这是我的第一篇帖子,所以我可能会错过一些信息,抱歉:鞠躬。

我的问题和Paul T几乎一样(Permalinks: custom post type -> custom taxonomy -> post), 即:

我有一个自定义的post类型,名为sioban\\u cpt,它有一个自定义的分类法sioban\\u taxo。

sioban\\U cpt可以more than one sioban\\u taxo。

我希望我的URL以这种方式路由:

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

www.mysite.com/sioban_cpt/ => shows all posts with sioban_cpt post-type (this works)
www.mysite.com/sioban_cpt/foo/ => shows all posts with the foo taxonomy(this works)
www.mysite.com/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.com/sioban_cpt/bar is the shown permalink in the WP admin post edition page)
作为PaulT,我尽量避免使用插件。

我不知道解决方案是否可行,因为我希望分类法和CPT的帖子都是CPT的后代,相反,我不希望CPT的帖子是taxo的子类。

这是我的函数代码。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);
谢谢你的帮助。。。

1 个回复
SO网友:Sioban

感谢Sally CJ,以下是对我有效的解决方案:

/*To create new custom taxonomy  */
function si0b_ct_year()
{
    /* Property Type */
    $labels = array(
        \'name\'                       => _x(\'Years\', \'Taxonomy General Name\', \'siobanone\'),
        \'singular_name\'              => _x(\'Year\', \'Taxonomy Singular Name\', \'siobanone\')
    );

    $rewrite = array(
        \'slug\'                     => \'artistes\',
        \'with_front\'   => false,
        \'hierarchical\' => true,
    );

    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'rewrite\'                    => $rewrite,
        \'show_in_rest\'               => true,
    );
    register_taxonomy(\'aec-year\', array(\'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\'),
    );

    $rewrite = array(
        \'slug\'                => \'artistes\',
        \'with_front\'          => false,
    );
    $args = array(
        \'label\'               => __(\'artists\', \'siobanone\'),
        \'description\'         => __(\'Artists\', \'siobanone\'),
        \'labels\'              => $labels,
        \'taxonomies\'          => array(\'aec-year\', \'post_tag\'),
        \'hierarchical\'        => true,
        \'public\'              => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'query_var\'           => \'artists\',
        \'rewrite\'             => $rewrite,
        \'show_in_rest\'               => true,
    );
    register_post_type(\'artists\', $args);
}

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

/**Allow this :
 * www.mysite.com/sioban_cpt/foo/ => shows all posts with the foo taxonomy
 * www.mysite.com/sioban_cpt/bar => shows the indivual post under the sioban_cpt post-type
**/
function si0b_parse_request( $wp ) {
    $path      = \'artistes\'; // rewrite slug; no trailing slashes
    $taxonomy  = \'aec-year\'; // taxonomy slug
    $post_type = \'artists\';  // post type slug

    if ( preg_match( \'#^\' . preg_quote( $path, \'#\' ) . \'/#\', $wp->request ) &&
        isset( $wp->query_vars[ $taxonomy ] ) ) {
        $slug = $wp->query_vars[ $taxonomy ];
        $slug = ltrim( substr( $slug, strrpos( $slug, \'/\' ) ), \'/\' );

        if ( ! term_exists( $slug, $taxonomy ) ) {
            $wp->query_vars[\'name\']       = $wp->query_vars[ $taxonomy ];
            $wp->query_vars[\'post_type\']  = $post_type;
            $wp->query_vars[ $post_type ] = $wp->query_vars[ $taxonomy ];
            unset( $wp->query_vars[ $taxonomy ] );
        }
    }
}
add_action( \'parse_request\', \'si0b_parse_request\' );

相关推荐

将参数传递给自定义帖子类型Single,同时保持URL干净

我正在将get参数传递给干净url中的单个自定义帖子类型。因此,例如,将有定制的职位类型“服务”,其中将有单个职位“园艺”。/services/gardening/leeds 这将转换为以下内容,以便可以在模板上访问变量:/services/gardening?location=leeds 但在浏览器中仍显示以下内容:/services/gardening/leeds 我只是想知道在使用自定义帖子类型之前是否有人这样做过?谢谢