自定义帖子类型和分类URL重写

时间:2013-06-19 作者:pylady

我正在使用Wordpress 3.5.1为我的个人网站创建自定义主题。我对WordPress自定义帖子类型和分类法有问题,详情如下:

自定义帖子类型名称:articlesarticle-categoriesarticles (与我的cpt相同)当前我的URL如下:

  • example.com/articles --> archive-articles.php (有效)

  • example.com/articles/taxo-parent --> taxonomy.php (有效)

  • example.com/articles/taxo-child --> taxonomy.php (有效)

  • example.com/articles/post-title --> 我使用single-articles.php, 它不起作用

    我希望我的URL如下:

    • example.com/articles

    • example.com/articles/taxo-parent

    • example.com/articles/taxo-parent/taxo-child

    • example.com/articles/taxo-parent/taxo-child/post-title

    处理这个案子和搜索/阅读谷歌的帖子已经一周了,但仍然没有运气,非常感谢你们能帮助我。

    谢谢

    编辑:

    我使用WP Custom Post Type UI插件创建自定义帖子类型和自定义分类法

3 个回复
SO网友:csehasib

我已经使用了我的珠宝后类型风箱代码,您只需更换您的后类型&;分类学只需复制(&A);粘贴到函数文件上。

<?php

//Register a Jewelry post type.
function jewelry_post_register() {
    $labels = array(
        \'name\'               => _x( \'Jewelries\', \'post type general name\', \'twentytwelve\' ),
        \'singular_name\'      => _x( \'Jewelry\', \'post type singular name\', \'twentytwelve\' ),
        \'menu_name\'          => _x( \'Jewelries\', \'admin menu\', \'twentytwelve\' ),
        \'name_admin_bar\'     => _x( \'Jewelry\', \'add new on admin bar\', \'twentytwelve\' ),
        \'add_new\'            => _x( \'Add New\', \'Jewelry\', \'twentytwelve\' ),
        \'add_new_item\'       => __( \'Add New Jewelry\', \'twentytwelve\' ),
        \'new_item\'           => __( \'New Jewelry\', \'twentytwelve\' ),
        \'edit_item\'          => __( \'Edit Jewelry\', \'twentytwelve\' ),
        \'view_item\'          => __( \'View Jewelry\', \'twentytwelve\' ),
        \'all_items\'          => __( \'All Jewelries\', \'twentytwelve\' ),
        \'search_items\'       => __( \'Search Jewelries\', \'twentytwelve\' ),
        \'parent_item_colon\'  => __( \'Parent Jewelries:\', \'twentytwelve\' ),
        \'not_found\'          => __( \'No Jewelries found.\', \'twentytwelve\' ),
        \'not_found_in_trash\' => __( \'No Jewelries found in Trash.\', \'twentytwelve\' )
    );

    $args = array(
        \'labels\'             => $labels,
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'jewelries\' ),
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'custom-fields\' )
    );

    register_post_type( \'jewelry\', $args );

}
add_action( \'init\', \'jewelry_post_register\' );


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

    $args = array(
        \'hierarchical\'      => true,
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'jewelry-category\' ),
    );

    register_taxonomy( \'jewelry-category\', array( \'jewelry\' ), $args );

}
add_action( \'init\', \'create_jewelry_taxonomies\', 0 );



function my_custom_template_include( $template ) {
    if ( get_query_var(\'post_type\') == \'jewelry\' ) {

        if ( is_single() ) {
            if ( $single = locate_template( array( \'single-jewelry.php\') ) )
                return $single;
        }

    }
    return $template;
}

add_filter( \'template_include\', \'my_custom_template_include\' );
add_filter( \'template_include\', \'insert_my_template\' );

function insert_my_template( $template )
{
    if ( \'jewelry\' === get_post_type() && !is_single() )
        return dirname( __FILE__ ) . \'/archive-jewelry.php\';


    return $template;
}


?>
根据你的问题,我的网站运行得很好。

SO网友:ksr89

您不能命名custom post_type 名称和taxonomy slug 这是因为当你研究分类法时

example.com/articles/taxo-parent --> taxonomy.php

example.com/articles/taxo-child --> taxonomy.php
但对于post\\u类型的文章,它将使用相同的url

example.com/post_type/post_in_that_post_type_slug
因此,这使得分类法和post\\u类型的url发生冲突,因为它们的初始url都是相同的

example.com/post_type/ = example.com/articles/

example.com/taxonomy/ = example.com/articles/

SO网友:Fatih Toprak

注册帖子类型后,应该更新permalink选项。

http://codex.wordpress.org/Function_Reference/flush_rewrite_rules

比它应该起作用的还要多。

结束

相关推荐