// 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,我相信您可以处理函数提取。
如果你有问题,请告诉我进展如何