自定义帖子类型、分类和固定链接

时间:2010-12-13 作者:RodeoRamsey

这让我抓狂,我相信这很简单,但我搜索的东西都没有简单的结构(一切都很复杂)。

我有一个自定义的帖子类型product_listing 和自定义分类法product_cat (这是分层的,应该有相似的类别)。

我只是希望我的URL如下所示:

mysite.com/products/category1/product-name1 
mysite.com/products/category2/product-name2
但就我的一生而言,无论我做什么,我都会遇到令人恐惧的404问题。页面工作正常,帖子工作正常,但我的自定义帖子无法正常工作。他们表现为:

mysite.com/products/product-name1
mysite.com/products/product-name2
这实际上是有效的!我只是想在那里看到我的自定义分类法,而且我想能够访问taxonomy.php 我已通过转到以下位置设置模板:

mysite.com/products/category1/
mysite.com/products/category2/
我的鼻涕虫都不一样,我也不希望它们是一样的。这是我的functions.php 文件:

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

// register the new post type
register_post_type( \'product_listing\', array( 
    \'labels\'                 => array(
        \'name\'               => __( \'Products\' ),
        \'singular_name\'      => __( \'Product\' ),
        \'add_new\'            => __( \'Add New\' ),
        \'add_new_item\'       => __( \'Create New Product\' ),
        \'edit\'               => __( \'Edit\' ),
        \'edit_item\'          => __( \'Edit Product\' ),
        \'new_item\'           => __( \'New Product\' ),
        \'view\'               => __( \'View Products\' ),
        \'view_item\'          => __( \'View Product\' ),
        \'search_items\'       => __( \'Search Products\' ),
        \'not_found\'          => __( \'No products found\' ),
        \'not_found_in_trash\' => __( \'No products found in trash\' ),
        \'parent\'             => __( \'Parent Product\' ),
    ),
    \'description\'           => __( \'This is where you can create new products on your site.\' ),
    \'public\'                => true,
    \'show_ui\'               => true,
    \'capability_type\'       => \'post\',
    \'publicly_queryable\'    => true,
    \'exclude_from_search\'   => false,
    \'menu_position\'         => 2,
    \'menu_icon\'             => get_stylesheet_directory_uri() . \'/images/tag_orange.png\',
    \'hierarchical\'          => true,
    \'_builtin\'              => false, // It\'s a custom post type, not built in!
    \'rewrite\'               => array( \'slug\' => \'products\', \'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( \'product_cat\', array( \'product_listing\' ), array(
        \'hierarchical\'  => true,
        \'labels\'        => $labels,
        \'show_ui\'       => true,
        \'query_var\'     => true,
        //\'rewrite\'     => true,
        \'rewrite\'       => array( \'slug\' => \'%category%\', \'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\', \'product_listing\', array(
        \'hierarchical\'  => false,
        \'labels\'        => $labels,
        \'show_ui\'       => true,
        \'query_var\'     => true,
        //\'rewrite\'     => array( \'slug\' => \'scents\' ),
    ) );
}
我还有另一个自定义分类法scents 理想情况下,我希望有一些友好的url,但我对此持开放态度。我想通过访问mysite.com/products/scents 但它们不必是特定类别的。

有人能帮我吗?

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

改变slug 在post类型参数中products/%product_cat%, 和slug 在分类法参数中products, 然后刷新重写规则。WordPress现在应该处理/products/my-product-cat/post-name/!

最后,我们需要帮助WordPress生成permalinks(现成的,它不会识别permastruct标签%product_cat%):

/**
 * Inject term slug into custom post type permastruct.
 * 
 * @link   http://wordpress.stackexchange.com/a/5313/1685
 * 
 * @param  string  $link
 * @param  WP_Post $post 
 * @return array
 */
function wpse_5308_post_type_link( $link, $post ) {
    if ( $post->post_type === \'product_listing\' ) {
        if ( $terms = get_the_terms( $post->ID, \'product_cat\' ) )
            $link = str_replace( \'%product_cat%\', current( $terms )->slug, $link );
    }

    return $link;
}

add_filter( \'post_type_link\', \'wpse_5308_post_type_link\', 10, 2 );
需要注意的是,这将为按名称排序的帖子获取第一个产品类别。如果您要为单个产品分配多个类别,我可以很容易地更改它确定在permalink中使用哪个类别的方式。

让我知道你是怎么做的,我们可以解决其他问题!

SO网友:Jeff

谢谢@TheDeadMechanic,你的回答帮助了我,但只是部分帮助。我想做RodeoRamsey要求的同样的事情,但要使用嵌套类别(即:mysite.com/products/category1/child-category-1/grandchild-category-1/product-name) 而你的解决方案并不能解决这个问题。

我最终为我的问题找到了一个有效的扩展解决方案,因此如果其他人需要嵌套的类别/子类别,您可以看到detailed solution 关于我自己的问题。希望它能帮助其他人,并感谢最初的步骤。

SO网友:Chris

我不确定wp是否支持这种开箱即用的结构,但您可以很容易地创建自己的重写规则来做到这一点。

在此处查看以前的答案Author url rewrite.

您可以更改线路

$newrules[\'author/([^/]+)/songs/?$\'] = \'index.php?post_type=songs&author=$matches[1]\';
大概是

$newrules[\'products/([^/]+)/([^/]+)/?$\'] = \'index.php?post_type=product_listing&product_cat=$matches[1]&name=$matches[2]\';
这里的product\\u cat部分可能是多余的-我不确定是否需要它。

您可以添加任何您喜欢的规则,它们将优先于内置规则。

SO网友:Leo Koo

事实上,这很容易。你只需要一句话。这是我的密码

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(\'product_cat\', array(\'product_listing\'), array(
        \'hierarchical\' => true,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'hierarchical\' => true),
    ));
并应用于GenerateWP生成的我的评论CPT的分类法。com。我在自己的WordPress网站上使用这个,https://www.wpstarters.com

function reviews_category_taxonomy() {

    $labels = array(
        \'name\'                       => _x( \'Reviews Categories\', \'Taxonomy General Name\', \'reviews_category\' ),
        \'singular_name\'              => _x( \'Reviews Category\', \'Taxonomy Singular Name\', \'reviews_category\' ),
        \'menu_name\'                  => __( \'Reviews Category\', \'reviews_category\' ),
        \'all_items\'                  => __( \'All Review Categories\', \'reviews_category\' ),
        \'parent_item\'                => __( \'Parent Review Category\', \'reviews_category\' ),
        \'parent_item_colon\'          => __( \'Parent Review Category:\', \'reviews_category\' ),
        \'new_item_name\'              => __( \'New Review Category Name\', \'reviews_category\' ),
        \'add_new_item\'               => __( \'Add New Review Category\', \'reviews_category\' ),
        \'edit_item\'                  => __( \'Edit Review Category\', \'reviews_category\' ),
        \'update_item\'                => __( \'Update Review Category\', \'reviews_category\' ),
        \'view_item\'                  => __( \'View Review Category\', \'reviews_category\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'reviews_category\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'reviews_category\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used\', \'reviews_category\' ),
        \'popular_items\'              => __( \'Popular Review Categories\', \'reviews_category\' ),
        \'search_items\'               => __( \'Search Items\', \'reviews_category\' ),
        \'not_found\'                  => __( \'Not Found\', \'reviews_category\' ),
        \'no_terms\'                   => __( \'No Review Categories\', \'reviews_category\' ),
        \'items_list\'                 => __( \'Review Categories list\', \'reviews_category\' ),
        \'items_list_navigation\'      => __( \'Review Categories list navigation\', \'reviews_category\' ),
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => false,
        \'show_in_rest\'               => true,
        \'rewrite\' => array( \'hierarchical\' => true ),
    );
    register_taxonomy( \'reviews_category\', array( \'wps_reviews\' ), $args );

}
add_action( \'init\', \'reviews_category_taxonomy\', 0 );
你所需要的就是把“重写”=>数组(“分层”=>true),

结束

相关推荐