自定义邮政类型和分类组合

时间:2011-10-27 作者:Mattvic

是否可以创建如下永久链接/模板结构:

www.example.com/attractions                  <-- post type archive
www.example.com/attractions/museums          <-- taxonomy archive
www.example.com/attractions/van-gogh-museum  <-- custom post type
经过几天的尝试,我只能使用两种组合中的一种:post-type+post-type-archive或taxonomy+taxonomy-archive

不知道下一步该怎么办:我是否忽略了什么,我是否需要一个插件,我是否应该以不同的方式设置它?

=== edit ===

(只是大声思考)

目前,我正在使用post类型和分类法重写功能,如下所示:

\'rewrite\' => array(\'slug\'=>\'attractions\'),
但也许我应该走另一条路,手动将“景点”设置为自定义帖子类型和分类归档的父页面。

===/edit===

我希望有人能给我指出正确的方向。非常感谢所有建议!

2 个回复
SO网友:EarnestoDev

// Add the new post_type and taxonomy
add_action(\'init\', function(){
    // Add a post_type named \'testype\'
    register_post_type($postype = \'testype\', $arguments = array(
        \'name\'                  => $postype,
        \'label\'                 => \'Test\',
        \'public\'                => true,
        \'exclude_from_search\'   => true,
        \'capability_type\'       => \'post\',
        \'supports\'              => array(
            \'title\', \'excerpt\', \'editor\', \'custom-fields\', \'comments\',
        ),
        \'show_in_nav_menus\'     => false,
        \'hierarchical\'          => false,
        \'publicly_queryable\'    => true,
        \'rewrite\'               => array(
            \'with_front\'    => false,
            \'pages\'         => true,
            \'feeds\'         => true,
            \'slug\'          => \'test/read\',
            // This is my custom work, not stock functionality
            // \'permastruct\'    => \'%id%\'
        ),
        \'query_var\'             => \'testing\',
        \'has_archive\'           => true,
        \'show_ui\'               => true,
        \'can_export\'            => true,
        \'_builtin\'              => false,
    ));
    register_taxonomy($postax = \'testax\', $postype, array(
        \'label\'         => \'Tax\',
        \'hierarchical\'  => false,
        \'rewrite\'       => array(
            \'with_front\'        => false,
            \'pages\'             => true,
            \'feeds\'             => true,
            \'slug\'              => \'test/browse\',
        ),
        \'public\'            => true,
        \'show_ui\'           => true,
        \'query_var\'         => true,
        \'show_in_nav_menus\' => true,
        \'show_tagcloud\'     => false,
    ));
});
// Add the \'snippet\' postype to the loop.
add_action(\'pre_get_posts\', function(\\WP_Query $query){
    if(!is_archive()) return; // Only for archives!
    // If suppress_filters is on, bail here :) (no idea what this really does)
    if(!empty($query->query_vars[\'suppress_filters\'])) return;
    // Add a new post type to the loop if we are visiting the archive
    // of a taxonomy assigned to the post.
    if(!empty($query->query_vars[\'testax\'])){
        $post_types = $query->get(\'post_type\');
        if(empty($post_types)) $post_types = array(\'post\');
        elseif(is_string($post_types)) $post_types = array($post_types);
        // Add the new post_type now
        $query->set(\'post_type\', array_merge($post_types, array(\'testype\')));
    }
    return;
});
Cheers! 这是完整的功能和评论。它有PHP 5.3语法,因为我不再使用5.2了,而且我也有点懒。如果仍然使用5.2,我相信您可以处理函数提取。

如果你有问题,请告诉我进展如何

SO网友:EarnestoDev

See if this answer helps:How to add a link(href) so when i click it, it renders (shows all) posts of custom post type (rich_media), from the current category?

您需要添加新的post_typepre_get_posts 让东西正常工作Term Archives (类别、标记)。并确保启用archive 在自定义帖子类型上。

结束

相关推荐

从Custom Taxonomy获取帖子列表

我可以很好地为我的自定义分类法获取类别id或slug,但我需要能够将所有帖子作为该分类法条目的数组获取。我的代码如下:$args = array( \'post_type\' => \'product\', \'post_status\' => \'publish\', \'posts_per_page\' => -1 );&#x