自定义发布类型、分类、固定链接、归档的问题

时间:2012-05-18 作者:chrispo

我设置了两种自定义帖子类型:节拍和曲目。我为这两种自定义帖子类型设置了分类“标记”和“样式”。

自定义帖子类型的永久链接在函数中设置。php:beats/%style%/%postname%和tracks/%style%/%postname%

我有一个问题,当url是/beats/%style%和/tracks/%style%而没有postname(归档)时。然后,所有以%样式%显示的“beats”和“tracks”帖子都将可见。不仅是节拍或风格税的曲目。

当我对get\\u post\\u type()进行回显时,回显总是“beats”。即使url为/tracks/%style%。

这是我的代码:

<?php

add_action(\'init\', \'reg_music\');

function reg_music() {
    register_taxonomy(
    \'style\',
    array (
    0 => \'beats\',
    1 => \'tracks\',
    ),
    array(
        \'label\' => \'Styles\',
        \'singular_label\' => \'Style\',
        \'hierarchical\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'slug\' => \'beats\',\'slug\' => \'tracks\'),
    )
);

$labels = array(
    \'name\' => _x(\'Beats\', \'post type general name\'),
    \'singular_name\' => _x(\'Beat\', \'post type singular name\')
);

$args = array(
    \'labels\' => $labels,
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'query_var\' => \'beats\',
    \'capability_type\' => \'post\',
    \'hierarchical\' => true,
    \'menu_position\' => null,
    \'supports\' => array(\'title\',\'editor\',\'excerpt\',\'trackbacks\',\'custom-fields\',\'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\',),
    \'rewrite\' => array(
        \'slug\' => \'beats/%style%\',
        \'with_front\' => false
    ),
    \'has_archive\' => true
); 

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

 $labels = array(
    \'name\' => _x(\'Tracks\', \'post type general name\'),
    \'singular_name\' => _x(\'Track\', \'post type singular name\')
);

$args = array(
    \'labels\' => $labels,
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'query_var\' => \'tracks\',
    \'capability_type\' => \'post\',
    \'hierarchical\' => true,
    \'menu_position\' => null,
    \'supports\' => array(\'title\',\'editor\',\'excerpt\',\'trackbacks\',\'custom-fields\',\'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\',),
    \'rewrite\' => array(
        \'slug\' => \'tracks/%style%\',
        \'with_front\' => false
    ),
    \'has_archive\' => true
); 

  register_post_type( \'tracks\' , $args );
  // flush_rewrite_rules();
}

add_filter(\'post_type_link\', \'music_permalink_structure\', 10, 4);
function music_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, \'%style%\' ) ) {
    $event_type_term = get_the_terms( $post->ID, \'style\' );
    $post_link = str_replace( \'%style%\', array_pop( $event_type_term )->slug,         $post_link );
}
return $post_link;
}

register_taxonomy(\'tags\',array (
0 => \'beats\',
1 => \'tracks\',
),array( \'hierarchical\' => false, \'label\' => \'Tags\',\'show_ui\' => true,\'query_var\' =>     true,\'rewrite\' => array(\'slug\' => \'tag\'),\'singular_label\' => \'Tag\') );

?>

1 个回复
SO网友:Michael Ecklund

我知道你可能不想再使用另一个插件了,但我可以建议你使用一个高度可配置的插件来处理自定义字段/组、自定义帖子类型和自定义分类吗?

非常好,简单,易于使用的插件。没有硬核广告。不会让你的wordpress网站陷入困境。该插件也在积极开发和更新。

Download Types - Custom Fields and Custom Post Types Management

我几乎在每次wordpress安装中都使用这个插件。你所做的应该很棒。

注意:Joost de Valk在Yoast website.

结束

相关推荐

Custom post type permalinks

我有一个名为“数字”的自定义帖子类型,我想实现URL的“帖子”如下http://website.com/post-name 而不是http://website.com/number/post-name.我试图将register\\u post\\u type中的“rewrite”参数设置为:\'rewrite\' => array( \'slug\' => \'\', \'with_front\' => false ), 但它没有起作用。即使我保存了永久链接设置以刷新它。(我将永久链接设