自定义POST类型固定链接结构,该类别还用作档案

时间:2017-03-31 作者:EmilyH

我到处找了很久,没有找到任何我想要的东西,所以我想我应该在这里发布。这篇文章和答案非常接近,但我很难将其适应我的需要:How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name

以下是我想做的:

自定义post类型slug:业务

URL结构:

/business-listings/  (top level archive of all the custom post type posts)
/business-listings/whatever/ (archive of the custom post type posts that have the category of \'whatever\')
/business-listings/whatever/the-post/ (the single custom post type post)
帖子类型中的每个帖子都只有一个类别(如果这有区别的话)。

我尝试过像“Custom Post Type Permalinks”这样的插件,但这不允许我将/business-listings/whatever/用作存档(即使刷新重写规则,它也会返回404)。到目前为止,我正在使用“自定义帖子类型UI”创建自定义帖子类型,但如果在函数中手动添加它们更简单的话。php我完全支持。

1 个回复
最合适的回答,由SO网友:EmilyH 整理而成

我应该仔细看看上面的链接。这个问题中有一个包含答案的链接(https://wordpress.stackexchange.com/a/5313/100538). 下面是我用来获得所需效果的代码:

///// CUSTOM POST TYPES /////

// register the new post type
register_post_type( \'business\', array( 
    \'labels\'                 => array(
        \'name\'               => __( \'Businesses\' ),
        \'singular_name\'      => __( \'Business\' ),
        \'add_new\'            => __( \'Add New\' ),
        \'add_new_item\'       => __( \'Create New Business\' ),
        \'edit\'               => __( \'Edit\' ),
        \'edit_item\'          => __( \'Edit Business\' ),
        \'new_item\'           => __( \'New Business\' ),
        \'view\'               => __( \'View Businesses\' ),
        \'view_item\'          => __( \'View Business\' ),
        \'search_items\'       => __( \'Search Businesses\' ),
        \'not_found\'          => __( \'No businesses found\' ),
        \'not_found_in_trash\' => __( \'No businesses found in trash\' ),
        \'parent\'             => __( \'Parent Business\' ),
    ),
    \'description\'           => __( \'This is where you can create new businesses on your site.\' ),
    \'public\'                => true,
    \'show_ui\'               => true,
    \'capability_type\'       => \'post\',
    \'publicly_queryable\'    => true,
    \'exclude_from_search\'   => false,
    \'has_archive\'           => true,
    \'menu_position\'         => 2,
    \'menu_icon\'             => \'dashicons-id\',
    \'hierarchical\'          => true,
    \'_builtin\'              => false, // It\'s a custom post type, not built in!
    \'rewrite\'               => array( \'slug\' => \'business-listings/%business_cat%\', \'with_front\' => true ),
    \'query_var\'             => true,
    \'supports\'              => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\' ),
) );


//hook into the init action and call create_book_taxonomies when it fires
add_action( \'init\', \'create_product_taxonomies\', 0 );
//add_action(\'admin_init\', \'flush_rewrite_rules\');

//create two taxonomies, genres and writers for the post type "book"
function create_product_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        \'name\'              => _x( \'Categories\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Category\', \'taxonomy singular name\' ),
        \'search_items\'      =>  __( \'Search Categories\' ),
        \'all_items\'         => __( \'All Categories\' ),
        \'parent_item\'       => __( \'Parent Categories\' ),
        \'parent_item_colon\' => __( \'Parent Categories:\' ),
        \'edit_item\'         => __( \'Edit Category\' ), 
        \'update_item\'       => __( \'Update Category\' ),
        \'add_new_item\'      => __( \'Add New Category\' ),
        \'new_item_name\'     => __( \'New Category Name\' ),
        \'menu_name\'         => __( \'Category\' ),
    );  

    register_taxonomy( \'business_cat\', array( \'business\' ), array(
        \'hierarchical\'  => true,
        \'labels\'        => $labels,
        \'show_ui\'       => true,
        \'query_var\'     => true,
        //\'rewrite\'     => true,
        \'rewrite\'       => array( \'slug\' => \'business-listings\', \'with_front\' => true ),
    ) );

    // Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        \'name\'                       => _x( \'Scents\', \'taxonomy general name\' ),
        \'singular_name\'              => _x( \'Scent\', \'taxonomy singular name\' ),
        \'search_items\'               =>  __( \'Search Scents\' ),
        \'popular_items\'              => __( \'Popular Scents\' ),
        \'all_items\'                  => __( \'All Scents\' ),
        \'parent_item\'                => null,
        \'parent_item_colon\'          => null,
        \'edit_item\'                  => __( \'Edit Scent\' ), 
        \'update_item\'                => __( \'Update Scent\' ),
        \'add_new_item\'               => __( \'Add New Scent\' ),
        \'new_item_name\'              => __( \'New Scent Name\' ),
        \'separate_items_with_commas\' => __( \'Separate scents with commas\' ),
        \'add_or_remove_items\'        => __( \'Add or remove scents\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used scents\' ),
        \'menu_name\'                  => __( \'Scents\' ),
    ); 

    register_taxonomy( \'scent\', \'business\', array(
        \'hierarchical\'  => false,
        \'labels\'        => $labels,
        \'show_ui\'       => true,
        \'query_var\'     => true,
        //\'rewrite\'     => array( \'slug\' => \'scents\' ),
    ) );
}

function wpse_5308_post_type_link( $link, $post ) {
    if ( $post->post_type === \'business\' ) {
        if ( $terms = get_the_terms( $post->ID, \'business_cat\' ) )
            $link = str_replace( \'%business_cat%\', current( $terms )->slug, $link );
        else
            $link = str_replace( \'%business_cat%\', \'uncategorized\', $link );
    }

    return $link;
}

add_filter( \'post_type_link\', \'wpse_5308_post_type_link\', 10, 2 );
所有的功劳都归功于死亡医生的回答,我从中拼凑出了这个答案。

相关推荐