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

时间:2011-06-16 作者:matt_d_rat

我发现这个地方在过去是一个很好的信息来源,通过大量的谷歌搜索我遇到的问题。我的问题与WordPress使用的详细重写规则有关。

我已经设置了一个名为“project”的自定义帖子类型,并注册了一个名为“project”的自定义分类法。除了重写段塞选项外,其他一切都很好,因为它们最终会发生冲突,很可能是因为重写规则。

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

  • example.com/work/%taxonomy%/%post_name%/ (对于帖子)example.com/work/%taxonomy%/ (列出属于特定词汇的帖子)example.com/work/ (转到page-work.php,其中包括taxonomy.php,列出与该分类法相关的所有帖子)
这是我迄今为止的代码,但我需要帮助编写WP\\U重写规则,因为这是我有点困惑的地方。

$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)
    )
);
非常感谢您的帮助!:-)

3 个回复
SO网友:nonsensecreativity

希望这能解决你的问题

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);
您需要创建的是归档工作。php(您的帖子类型存档)和分类法。php,用于显示自定义分类法归档。

SO网友:Dipesh KC

我也有同样的问题,经过很多努力,我最终找到了这个解决方案
只需将此添加到您的代码中

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 
}

SO网友:Jeff

更详细的解释是on another post, 但以下是您需要添加的基本部分:

注册分类法和cpt。确保taxo的重写段标为“basename”,cpt的重写段标为“basename/%tax\\u name%”。

告诉wordpress如何处理“%tax\\u 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);

结束

相关推荐

curl problem or permalinks

我刚刚配置了我的VPS,我使用的是Centos,一切都很好,但如果我将永久链接设置为自定义结构,然后接受主页,没有帖子出现,它会显示404页,我想这是因为我没有启用curl,但我不知道我的php在哪里。我的centos中的ini文件?好的,我的卷曲被启用了,我检查过了phpinfo(); 这里是URLhttp://74.117.158.182/info.php但如果我在我的wordpress中设置了永久链接,那么接受主页,所有都会给我404页,你可以在这个URL上查看http://mbas.co.in如果