Include taxonomy slug in url?

时间:2012-10-24 作者:Rob

我有以下自定义帖子类型和自定义分类设置:

add_action( \'init\', \'create_post_type\' );
function create_post_type() {
    register_post_type( \'system\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Systems\' ),
                \'singular_name\' => __( \'System\' )
            ),
        \'capability_type\' => \'post\',
        \'supports\' => array(\'title\',\'editor\',\'comments\'),   
        \'public\' => true,
        \'has_archive\' => true,
        \'rewrite\' => array( \'slug\' => \'system\' ),
        )
    );
}

function news_init() {
    register_taxonomy(
        \'system\',
        \'system\',
        array(
            \'label\' => __( \'Product Category\' ),
            \'sort\' => true,
            \'hierarchical\' => true,
            \'args\' => array( \'orderby\' => \'term_order\' ),
            \'rewrite\' => array( \'slug\' => \'product-category\' )
        )
    );  
}
add_action( \'init\', \'news_init\' );
是否可以在URL中包含自定义分类名称?

目前,当我转到自定义帖子时,URL如下所示:

http://www.domain/product-category/(post-name)/

如何使其看起来像以下内容?

http://www.domain/(category-slug)/(post-name)/

3 个回复
SO网友:Daniel Sachs

这对WordPress来说可能有点挑战性,因为它不知道您指的是文章、页面还是分类法。不过,请将您的分类法重命名为product_category 并尝试将post类型参数中的slug改为system/%product_category%, 在你的分类论点中system. WordPress现在应该处理/system/your-product-category/post-name/

现成的WordPress无法识别永久链接结构标记%product\\u category%,因此我们需要声明它:

function filter_post_type_permalink($link, $post)
{
    if ($post->post_type != \'system\')
        return $link;

    if ($cats = get_the_terms($post->ID, \'product_category\'))
        $link = str_replace(\'%product_category%\', array_pop($cats)->slug, $link);
    return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_permalink\', 10, 2);

NOTES:

<这将只获取post orderedby名称的第一个产品类别

SO网友:Fiaz Husyn

首先DO NOT USE SAME POST TYPE & TAXONOMY, 事实上,您甚至不需要注册分类法,因为您需要类别。使用以下代码:

add_action( \'init\', \'create_post_type\' );
function create_post_type() {
    register_post_type( \'system\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Systems\' ),
                \'singular_name\' => __( \'System\' )
            ),
        \'capability_type\' => \'post\',
        \'supports\' => array(\'title\',\'editor\',\'comments\'),   
        \'public\' => true,
        \'has_archive\' => true,
        \'rewrite\' => array( \'slug\' => \'%category%\' ),
        \'taxonomies\' => array(\'category\')
        )
    );
}
现在,您将拥有可用于“系统”帖子类型的类别。还要注意,我已经更改了slug的值。现在,下一个与filter\\em>post\\u type\\u link的连接将用于用category的slug替换\'%category%\'。使用以下代码:

function replace_system_type_category( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, \'category\' );
        if( $terms ){
            return str_replace( \'%category%\' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;  
}
add_filter( \'post_type_link\', \'replace_system_type_category\', 1, 3 );
根据您的要求,将上述代码替换为类别的slug。您可以将代码用于自定义分类法,但不要将“system”用于帖子类型和分类法。

SO网友:Brad Dalton

这是我在本地测试的内容,它显示了您在设置页面中创建的分类类型名称,代码添加到分类归档页面上的permalink中。

add_action( \'init\', \'wpsites_create_custom_taxonomy_types\' );
function wpsites_create_custom_taxonomy_types() {

register_taxonomy( \'system-type\', \'system\',
    array(
        \'labels\' => array(
            \'name\'          => _x( \'Types\', \'taxonomy general name\', \'wpsites\' ),
            \'add_new_item\'  => __( \'Add New System Type\', \'wpsites\' ),
            \'new_item_name\' => __( \'New System Type\', \'wpsites\' ),
        ),
        \'exclude_from_search\' => true,
        \'has_archive\'         => true,
        \'hierarchical\'        => true,
        \'rewrite\'             => array( \'slug\' => \'system-type\', \'with_front\' => false ),
        \'show_ui\'             => true,
        \'show_tagcloud\'       => false,
    ));

}



add_action( \'init\', \'wpsites_system_custom_post_type\' );
function wpsites_system_custom_post_type() {

register_post_type( \'system\',
    array(
        \'labels\' => array(
            \'name\'          => __( \'System\', \'wpsites\' ),
            \'singular_name\' => __( \'System\', \'wpsites\' ),
        ),
        \'has_archive\'  => true,
        \'hierarchical\' => true,
        \'menu_icon\'    => \'dashicons-admin-network\',
        \'public\'       => true,
        \'rewrite\'      => array( \'slug\' => \'system\', \'with_front\' => false ),
        \'supports\'     => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'revisions\', \'page-attributes\' ),
        \'taxonomies\'   => array( \'system-type\' ),

    ));

}

结束

相关推荐

widgetlogic and permalinks

我试图使用widgetlogic在某些页面上有条件地显示菜单。每个菜单都使用如下标记is_page(array(\"Page Name\", \"Page Name 2\" ...)), 在我尝试更改permalinks之前,它一直工作得很好(因此所有菜单都会从各自的页面中消失)。我做错什么了吗?是否有解决方法?