组织定制邮寄类型档案模板

时间:2014-04-27 作者:Locke

因为在我所有尝试获取自定义帖子类型存档的过程中,我只得到了一个404

尝试在存档中组织不同的帖子类型。php

我正在努力做到这一点archive.php

<?php
/*===== Start Conditionals for Categories =====*/


$post = $wp_query->post;
$post_type = get_post_type($post);


  if($post_type == \'mysuper_post_type\')  {
   load_template( trailingslashit( get_template_directory() ) . \'layouts/archive-super_posttype.php\' );
} else {
    load_template( trailingslashit( get_template_directory() ) . \'layouts/category-default.php\' );
}

}

/*===== End Conditionals for Categories =====*/

?>
这是注册我的CPT的代码

// BrandVoice
function brand_voice() {

    $section = \'brand_voice\';
    $slug = \'brand-voice\';
    $names = \'Brands Voice\';
    $name = \'Brand Voice\';
    $desc = \'Adding Brand Voice\';
    $key = $section;
    $taxonomy = \'brand\';
    $slug{$taxonomy} = $taxonomy;
    $names{$taxonomy} = \'Brands\';
    $name{$taxonomy} = \'Brand\';

    $labels = array(
        \'name\'                => _x( $name, $desc, $section),
        \'singular_name\'       => _x( $name, $desc, $section),
        \'menu_name\'           => __( $names, $section ),
        \'parent_item_colon\'   => __( \'Parent \'.$name.\':\', $section ),
        \'all_items\'           => __( \'All \'.$names.\'\', $section ),
        \'view_item\'           => __( \'View \'.$names.\'\', $section ),
        \'add_new_item\'        => __( \'Add New \'.$name.\'\', $section ),
        \'add_new\'             => __( \'Add New \'.$name.\'\', $section ),
        \'edit_item\'           => __( \'Edit \'.$name.\'\', $section ),
        \'update_item\'         => __( \'Update \'.$name.\'\', $section ),
        \'search_items\'        => __( \'Search \'.$names.\'\', $section ),
        \'not_found\'           => __( \'Not found\', $section ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', $section ),
        );

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

    $supports = array(
        \'title\',
        \'editor\',
        \'excerpt\',
        \'author\',
        \'thumbnail\',
        \'comments\',
        \'revisions\',
        \'post_formats\',
        );

    $taxonomies = array(
        \'category\',
        \'post_tag\',
        );

    $args = array(
        \'label\'               => __( $key, $section ),
        \'description\'         => __( $desc, $section ),
        \'menu_icon\'           => \'dashicons-\'.$slug.\'\',
        \'labels\'              => $labels,
        \'supports\'            => $supports,
        \'taxonomies\'          => $taxonomies,
        \'hierarchical\'        => true,
        \'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\'           => $key,
        \'rewrite\'             => $rewrite,
        \'capability_type\'     => \'page\'
        );


    register_post_type( $key, $args );

}

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

这里有几件事。我不会在这里使用变量。老实说,我真的看不出变量的有用性。不要不必要地使代码复杂化。也不要在你的名字中使用下划线。

您已经将代码放在了一个函数中,这并没有错。但是,如果在函数中注册,该函数至少应该连接到init

我引用codex - register_post_type

创建或修改帖子类型。register\\u post\\u type只能通过“init”操作调用。如果在“init”之前调用,它将根本不起作用,如果稍后调用,新post类型的各个方面将无法正常工作。

我也不会使用您在archive.php 你需要看看template hierarchy. Wordpress使用归档。php来显示CPT。您还可以创建一个自定义存档模板,如果存在该模板,则将使用该模板来显示您的CPT。你可以简单地复制你的档案。php模板并将其重命名为archive-{自定义帖子类型}。php,在您的情况下,归档品牌语音。php。Wordpress现在将使用存档品牌语音。php显示您的帖子类型“brand voice”。你也可以为你的单曲做同样的事。php模板。

要记住一件事,要使此归档页面正常工作,您需要\'has_archive\' => true, 当你注册你的帖子类型时。

您也不应该在翻译字符串中使用post类型名称作为文本域名。目前,您的翻译字符串无法工作。如果你打开你的风格。css,在标题中你会发现这样一行。(这是第214个主题)

Text Domain: twentyfourteen  
这是您将在可翻译字符串中使用的文本域名。如果将其更改为其他内容,则字符串在翻译时将无法工作。您的字符串应该如下所示

_x( \'Brand Voice\', \'Adding Brand Voice\', \'text-domain-name\' ),
您也可以省略文本域名。您的字符串应该是

_x( \'Brand Voice\', \'Adding Brand Voice\' ),

Don\'t forget to flush your permalinks.

毕竟,您的代码应该是这样的

function register_brand_voice() {

    $labels = array(
        \'name\'                => _x( \'Brand Voice\', \'Adding Brand Voice\' ),
        \'singular_name\'       => _x( \'Brand Voice\', \'Adding Brand Voice\' ),
        \'menu_name\'           => __( \'Brand Voice\' ),
        \'parent_item_colon\'   => __( \'Parent Brand Voice\' ),
        \'all_items\'           => __( \'All Brand Voice\' ),
        \'view_item\'           => __( \'View Brand Voice\' ),
        \'add_new_item\'        => __( \'Add New Brand Voice\' ),
        \'add_new\'             => __( \'Add New Brand Voice\' ),
        \'edit_item\'           => __( \'Edit Brand Voice\' ),
        \'update_item\'         => __( \'Update Brand Voice\' ),
        \'search_items\'        => __( \'Search Brand Voice\' ),
        \'not_found\'           => __( \'Not found\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\' ),
        );

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

    $supports = array(
        \'title\',
        \'editor\',
        \'excerpt\',
        \'author\',
        \'thumbnail\',
        \'comments\',
        \'revisions\',
        \'post_formats\',
        );

    $taxonomies = array(
        \'category\',
        \'post_tag\',
        );

    $args = array(
        \'label\'               => __( \'brand-voice\', $section ),
        \'description\'         => __( \'Adding Brand Voice\', $section ),
        \'menu_icon\'           => \'dashicons-brand-voice\'
        \'labels\'              => $labels,
        \'supports\'            => $supports,
        \'taxonomies\'          => $taxonomies,
        \'hierarchical\'        => true,
        \'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\'           => $key,
        \'rewrite\'             => $rewrite,
        \'capability_type\'     => \'page\'
        );


    register_post_type( \'brand-voice\', $args );

}

add_action( \'init\', \'register_brand_voice\' );

结束

相关推荐