自定义POST类型Single-{Custom}.php不起作用

时间:2013-04-22 作者:BobbyDank

我用机器名special\\u media\\u post定制了一个post类型,wordpress根本看不到single-special\\u media\\u post。php。我完全不知所措。它始终默认为索引。php

以下是我的自定义帖子类型及其分类的代码:

//Post and Taxonomy stuff
//Register Custom Post Type
function special_media_post() {
$labels = array(
    \'name\'                => _x( \'Media Posts\', \'Post Type General Name\', \'text_domain\' ),
    \'singular_name\'       => _x( \'Media Post\', \'Post Type Singular Name\', \'text_domain\' ),
    \'menu_name\'           => __( \'Media Post\', \'text_domain\' ),
    \'parent_item_colon\'   => __( \'Media Post:\', \'text_domain\' ),
    \'all_items\'           => __( \'All Media Posts\', \'text_domain\' ),
    \'view_item\'           => __( \'View Media Post\', \'text_domain\' ),
    \'add_new_item\'        => __( \'Add New Media Post\', \'text_domain\' ),
    \'add_new\'             => __( \'New Media Post\', \'text_domain\' ),
    \'edit_item\'           => __( \'Edit Media Post\', \'text_domain\' ),
    \'update_item\'         => __( \'Update Media Post\', \'text_domain\' ),
    \'search_items\'        => __( \'Search Media Posts\', \'text_domain\' ),
    \'not_found\'           => __( \'No media posts found\', \'text_domain\' ),
    \'not_found_in_trash\'  => __( \'No media posts found in Trash\', \'text_domain\' ),
);

$rewrite = array(
    \'slug\'                => \'mediapost\',
    \'with_front\'          => true,
    \'pages\'               => true,
    \'feeds\'               => true,
);

$args = array(
    \'label\'               => __( \'mediapost\', \'text_domain\' ),
    \'description\'         => __( \'Post Type for Media\', \'text_domain\' ),
    \'labels\'              => $labels,
    \'supports\'            => array( \'title\', \'editor\', \'custom-fields\', ),
    \'taxonomies\'          => array( \'year\', \'type\' ),
    \'hierarchical\'        => false,
    \'public\'              => true,
    \'show_ui\'             => true,
    \'show_in_menu\'        => true,
    \'show_in_nav_menus\'   => true,
    \'show_in_admin_bar\'   => true,
    \'menu_position\'       => 5,
    \'can_export\'          => true,
    \'has_archive\'         => true,
    \'exclude_from_search\' => false,
    \'publicly_queryable\'  => true,
    \'query_var\'           => \'mediapost\',
    \'rewrite\'             => $rewrite,
    \'capability_type\'     => \'page\',
);

register_post_type( \'special_media_post\', $args );
}

// Register Custom Taxonomy
function media_year()  {
$labels = array(
    \'name\'                       => _x( \'Years\', \'Taxonomy General Name\', \'text_domain\' ),
    \'singular_name\'              => _x( \'Year\', \'Taxonomy Singular Name\', \'text_domain\' ),
    \'menu_name\'                  => __( \'Year\', \'text_domain\' ),
    \'all_items\'                  => __( \'All Years\', \'text_domain\' ),
    \'parent_item\'                => __( \'Parent Year\', \'text_domain\' ),
    \'parent_item_colon\'          => __( \'Parent Year:\', \'text_domain\' ),
    \'new_item_name\'              => __( \'New Year Name\', \'text_domain\' ),
    \'add_new_item\'               => __( \'Add New Year\', \'text_domain\' ),
    \'edit_item\'                  => __( \'Edit Year\', \'text_domain\' ),
    \'update_item\'                => __( \'Update Year\', \'text_domain\' ),
    \'separate_items_with_commas\' => __( \'Separate years with commas\', \'text_domain\' ),
    \'search_items\'               => __( \'Search years\', \'text_domain\' ),
    \'add_or_remove_items\'        => __( \'Add or remove years\', \'text_domain\' ),
    \'choose_from_most_used\'      => __( \'Choose from the most used yearss\', \'text_domain\' ),
);

$rewrite = array(
    \'slug\'                       => \'year\',
    \'with_front\'                 => true,
    \'hierarchical\'               => true,
);

$capabilities = array(
    \'manage_terms\'               => \'manage_categories\',
    \'edit_terms\'                 => \'manage_categories\',
    \'delete_terms\'               => \'manage_categories\',
    \'assign_terms\'               => \'edit_posts\',
);

$args = array(
    \'labels\'                     => $labels,
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => true,
    \'query_var\'                  => \'year\',
    \'rewrite\'                    => $rewrite,
    \'capabilities\'               => $capabilities,
);

register_taxonomy( \'year\', \'special_media_post\', $args );
}

// Register Custom Taxonomy
function media_type()  {
$labels = array(
    \'name\'                       => _x( \'Types\', \'Taxonomy General Name\', \'text_domain\' ),
    \'singular_name\'              => _x( \'Type\', \'Taxonomy Singular Name\', \'text_domain\' ),
    \'menu_name\'                  => __( \'Type\', \'text_domain\' ),
    \'all_items\'                  => __( \'All Types\', \'text_domain\' ),
    \'parent_item\'                => __( \'Parent Type\', \'text_domain\' ),
    \'parent_item_colon\'          => __( \'Parent Type:\', \'text_domain\' ),
    \'new_item_name\'              => __( \'New Type Name\', \'text_domain\' ),
    \'add_new_item\'               => __( \'Add New Type\', \'text_domain\' ),
    \'edit_item\'                  => __( \'Edit Type\', \'text_domain\' ),
    \'update_item\'                => __( \'Update Type\', \'text_domain\' ),
    \'separate_items_with_commas\' => __( \'Separate types with commas\', \'text_domain\' ),
    \'search_items\'               => __( \'Search types\', \'text_domain\' ),
    \'add_or_remove_items\'        => __( \'Add or remove types\', \'text_domain\' ),
    \'choose_from_most_used\'      => __( \'Choose from the most used types\', \'text_domain\' ),
);

$rewrite = array(
    \'slug\'                       => \'type\',
    \'with_front\'                 => true,
    \'hierarchical\'               => true,
);

$capabilities = array(
    \'manage_terms\'               => \'manage_categories\',
    \'edit_terms\'                 => \'manage_categories\',
    \'delete_terms\'               => \'manage_categories\',
    \'assign_terms\'               => \'edit_posts\',
);

$args = array(
    \'labels\'                     => $labels,
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => true,
    \'query_var\'                  => \'media_type\',
    \'rewrite\'                    => $rewrite,
    \'capabilities\'               => $capabilities,
);

register_taxonomy( \'type\', \'special_media_post\', $args );
}

// Hook into the \'init\' action
add_action( \'init\', \'special_media_post\', 0 );

// Hook into the \'init\' action
add_action( \'init\', \'media_year\', 0 );

// Hook into the \'init\' action
add_action( \'init\', \'media_type\', 0 );
如果你还需要看别的东西,我可以把它放上去,但如果我在里面放一个回声“Hello World”,它甚至看不到。因此,它根本看不到任何一个特别的媒体帖子。php或archive-special\\u media\\u帖子。php

4 个回复
最合适的回答,由SO网友:vancoder 整理而成

访问permalinks页面(将刷新它),然后再次检查。WordPress可能只需要轻轻推一下就可以识别出您添加到层次结构中的内容。

SO网友:Wesley Cheung

更改代码

发件人:

 \'has_archive\'         => true,
收件人:

 \'has_archive\'         => false,
然后转到永久链接页面,更改为默认值并返回到“pretty permalink”

%postname%/

现在应该可以了。

原因是它没有转到单个-{custom\\u post\\u type}。php页面是因为has\\u存档。当has\\u archive设置为true时,它将查找存档-{custom\\u post\\u type}。php而不是单个页面。

希望这能奏效。

SO网友:danilocgsilva

这是一个很好的做法,还使用register_activation_hook()register_deactivation_hook() 创建新内容类型时。

新的内容类型似乎总是无法重写。要避免这种情况,请在register\\u activation\\u hook()回调中放入flush_rewrite_rules() 以及注册新内容功能。我不知道为什么,但这样做似乎可以避免这个问题。外观:

register_activation_hook( __FILE__, \'your_active_hook\' );

function your_active_hook() {
    special_media_post();
    flush_rewrite_rules();
}

SO网友:Fränk

我复制了你的代码,通过管理员刷新了重写规则,现在当我访问媒体帖子时,主题使用了正确的模板。

你需要刷新重写规则once 使用after\\u switch\\u主题挂钩。这将确保在用户激活主题后自动刷新重写规则。

您可以使用此代码(直接从Codex中):

add_action( \'init\', \'theme_prefix_cpt_init\' );
function theme_prefix_cpt_init() {
    register_post_type( ... );
}

function theme_prefix_rewrite_flush() {
    flush_rewrite_rules();
}
add_action( \'after_switch_theme\', \'theme_prefix_rewrite_flush\' );
有关更多信息,请查阅WordPress Codex:http://codex.wordpress.org/Function_Reference/register_post_type

编辑:在这种情况下,Inspect Rewrite Rules插件非常方便,因为它允许您查看连接到自定义帖子类型的规则:http://wordpress.org/extend/plugins/rewrite-rules-inspector/

另请注意,推荐放置自定义帖子类型的位置是插件,而不是主题。

结束