自定义帖子类型和分类的WordPress重写规则


9

过去,由于对我遇到的问题进行了大量的Google搜索,因此我发现该位置过去是很好的信息来源。我的问题与WordPress使用的详细重写规则有关。

我已经建立了一个自定义后类型,称为项目,而且我已经注册了一个名为自定义分类项目。除了rewrite slug选项,所有其他方法都很有效,因为它们最终会发生冲突-很有可能是由于rewrite规则所致。

基本上,这就是我要实现的结构:

  • example.com/work/%taxonomy%/%post_name%/ (用于帖子)
  • example.com/work/%taxonomy%/ (列出属于特定分类术语的帖子)
  • example.com/work/ (转到包含taxonomy.php的page-work.php,以列出与该分类法相关的所有帖子)

这是我到目前为止的代码,但是我需要编写WP_Rewrite规则的帮助,因为这有点让我感到困惑。

$labels = array(
    'name' => _x('Projects', 'post type general name'),
    'singular_name' => _x('Project', 'post type singular name'),
    'add_new' => _x('Add New', 'project item'),
    'add_new_item' => __('Add New Project'),
    'edit_item' => __('Edit Project'),
    'new_item' => __('New Project'),
    'view_item' => __('View Project'),
    'search_items' => __('Search Projects'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'hierarchical' => true,
    'rewrite' => array('slug'=>'work', 'with_front'=>false),
    'show_ui' => true,
    '_builtin' => false, // It's a custom post type, not built in!
    'capability_type' => 'post',
    'query_var' => "project", // This goes to the WP_Query schema
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail', 'comments', 'author', 'excerpt')
);

register_post_type('project' , $args);

// Showcase Taxonomy
register_taxonomy('projects', array('project'), array(
    'public' => true,
    'hierarchical' => true,
    'label' => 'Project Categories', 
    'singular_label' => 'Project Category',
    'query_var' => true,
    'rewrite' => array('slug'=>'work', 'with_front'=>false, 'hierarchical'=>true)
    )
);

非常感谢您的帮助!:-)



@ChristopherDavis谢谢,我将进一步研究这些,看看我如何生活。
matt_d_rat 2011年

1
我认为可以通过查看混合自定义帖子类型和分类重写结构来回答此问题如果该问题对您没有帮助,请编辑此问题以表明它有何不同。
Jan Fabry'7

Answers:


1

希望这可以解决您的问题

function my_custom_post_type() {
$labels = array(
    'name' => _x('Projects', 'post type general name'),
    'singular_name' => _x('Project', 'post type singular name'),
    'add_new' => _x('Add New', 'project item'),
    'add_new_item' => __('Add New Project'),
    'edit_item' => __('Edit Project'),
    'new_item' => __('New Project'),
    'view_item' => __('View Project'),
    'search_items' => __('Search Projects'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => '',
    'menu_name' => 'Projects' 
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
        'hierarchical' => false,
        'has_archive' => true,
    'rewrite' => array('slug'=>'work', 'with_front'=>false),
    'show_ui' => true,
    '_builtin' => false, // It's a custom post type, not built in!
    'capability_type' => 'post',
        'query_var' => true, // This goes to the WP_Query schema
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail', 'comments', 'author', 'excerpt')
);

register_post_type( 'work' , $args );

}
function my_custom_taxonomies() {

    $labels = array(
        'name' => __( 'Taxonomy', 'taxonomy general name' ),
        'singular_name' => __( 'Taxonomy', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Taxonomy' ),
        'all_items' => __( 'All Taxonomy' ),
        'parent_item' => __( 'Parent Taxonomy' ),
        'parent_item_colon' => __( 'Parent Taxonomy:' ),
        'edit_item' => __( 'Edit Taxonomy' ), 
        'update_item' => __( 'Update Taxonomy' ),
        'add_new_item' => __( 'Add New Taxonomy' ),
        'new_item_name' => __( 'New Taxonomy Name' ),
        'menu_name' => __( 'Taxonomy' ),
    );  

    register_taxonomy( 'taxonomy', array('work'), array (
                    'labels' => $labels,
                    'hierarchical' =>false,
                    'show_ui' => true,
                    'rewrite' => array( 'slug' => 'work/taxonomy'),
                    'query_var' => true,
                    'show_in_nav_menus' => true,
                    'public' => true,
            ));
}

add_action('init', 'my_custom_post_type', 0);
add_action('init', 'my_custom_taxonomies', 10);

您需要创建的是archive-work.php(您的帖子类型存档)和taxonomy.php,它们将用于显示您的自定义分类存档。


不要忘记为自己的分类名称更改“分类”。不要使用与post_type相同的值。尝试将类别用于第一次尝试。工作/类别,register_taxonomy('category,array('work'),array(......
nonsensecreativity

1

我遇到了同样的问题,经过很多努力,最终还是得到了这个解决方案。
只需将其添加到您的代码中

global $wp_rewrite;
$wp_rewrite->flush_rules(); 

function my_custom_post_type() {
    $labels = array(
        'name' => _x('Projects', 'post type general name'),
        'singular_name' => _x('Project', 'post type singular name'),
        'add_new' => _x('Add New', 'project item'),
        'add_new_item' => __('Add New Project'),
        'edit_item' => __('Edit Project'),
        'new_item' => __('New Project'),
        'view_item' => __('View Project'),
        'search_items' => __('Search Projects'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => 'Projects' 
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
            'hierarchical' => false,
            'has_archive' => true,
        'rewrite' => array('slug'=>'work', 'with_front'=>false),
        'show_ui' => true,
        '_builtin' => false, // It's a custom post type, not built in!
        'capability_type' => 'post',
            'query_var' => true, // This goes to the WP_Query schema
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail', 'comments', 'author', 'excerpt')
    );

    register_post_type( 'work' , $args );

    global $wp_rewrite;   
    $wp_rewrite->flush_rules();    // this should help 
}

5
$ wp_rewrite-> flush_rules()不应经常运行,而应仅在激活或停用钩子上运行,或尽可能少地运行。它在这里说的是:codex.wordpress.org/Rewrite_API/flush_rules ALSO与此函数几乎相同:codex.wordpress.org/Function_Reference/flush_rewrite_rules
Jared

另一方面,这就是我的完成方式:pastebin.com/k7QvxKLi
Jared

@Jared感谢您的指点,但是当它集成到我们的主题中时(即不通过插件),我想不出一种方法来做到这一点。请提出建议。
Dipesh KC 2012年

functions.php在这种情况下,代码就可以了。插件和主题的代码完全相同,唯一的区别是主题始终存在functions.php或包含在其中的文件functions.php
Jared 2012年

2
我建议使用after_switch_theme挂钩,它是3.3(IIRC)的新功能。
克里斯蒂安

0

更详细的解释在另一篇文章中,但这是您需要添加的基本部分:

  1. 注册您的分类法和cpt。确保分类单元的重写段为“ basename”,而cpt的重写段为“ basename /%tax_name%”。

  2. 像这样告诉wordpress如何处理“%tax_name%”:

    function filter_post_type_link($link, $post)
    {
    if ($post->post_type != 'custom_post_type_name')
        return $link;
    
    if ($cats = get_the_terms($post->ID, 'taxonomy_name'))
    {
        $link = str_replace('%taxonomy_name%',array_pop($cats)->term_id, link); // see custom function defined below
    }
    return $link;
    }
    add_filter('post_type_link', 'filter_post_type_link', 10, 2);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.