我的存档-posttype.php模板未加载

时间:2013-02-23 作者:Gab

我正在尝试创建一个存档照片。我的照片自定义帖子类型的php模板。我的问题是,当我查看页面时,它是归档文件。使用的是php模板,而不是存档照片。php。我只是想要存档照片。查看特定自定义帖子类型的归档页面时要使用的php。

我错过了什么?

请注意,在最后,我已将照片自定义帖子类型设置为主循环。

非常感谢您的时间和帮助。

<?php
//custom post type
add_action( \'init\', \'create_post_type\' );
function create_post_type() {
    register_post_type( \'photos\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Photos\' ),
                \'singular_name\' => __( \'Photo\' )
            ),
        \'public\' => true,
        \'has_archive\' => true,
        \'rewrite\' => array(\'slug\' => \'photo\'),
        \'menu_position\' => 5,
        \'taxonomies\' => array(\'genre\'),
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'post-formats\'),
        )
    );
    flush_rewrite_rules( false );
    //this enables post formats.
    add_theme_support( \'post-formats\', array( \'image\', \'chat\', \'video\', \'gallery\' ) );
    remove_post_type_support( \'post\', \'post-formats\' );
}
//hook into the init action and call create_photos_taxonomies when it fires
add_action( \'init\', \'create_photos_taxonomies\', 0 );
//create taxonomy, genres for the post type "photos"
function create_photos_taxonomies() 
{
  // Add new taxonomy, make it hierarchical (like categories)
  $labels = array(
    \'name\' => _x( \'Genres\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Genre\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Genres\' ),
    \'all_items\' => __( \'All Genres\' ),
    \'parent_item\' => __( \'Parent Genre\' ),
    \'parent_item_colon\' => __( \'Parent Genre:\' ),
    \'edit_item\' => __( \'Edit Genre\' ), 
    \'update_item\' => __( \'Update Genre\' ),
    \'add_new_item\' => __( \'Add New Genre\' ),
    \'new_item_name\' => __( \'New Genre Name\' ),
    \'menu_name\' => __( \'Genre\' ),
  );    
  register_taxonomy(\'genre\',array(\'photos\'), array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'photos\' ),
  ));
}
function my_post_class( $classes ) {
    global $post;
    $terms = wp_get_object_terms( $post->ID, \'genre\' );
    foreach ( $terms as $genre ) {
        $classes[] = $genre->slug;
    }
    return $classes;
}
add_filter( \'post_class\', \'my_post_class\' );
//show posts of post type \'photos\', \'page\' on home page
add_action( \'pre_get_posts\', \'add_my_custom_post_type\' );
//make photos custom post type the default post type for the main loop
function add_my_custom_post_type( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( \'post_type\', array( \'photos\' ) );
    return $query;
}
?>

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

如注释中所确定的,页面URI为?post_type=photos&genre=boudoir...

原因是"archive-{$post_type}.php" 在这种情况下不使用是因为页面是分类法存档,而不是自定义帖子类型存档。WordPress正在寻找taxonomy-genre.php 而不是archive-photos.php, 而找不到它,就是在拖欠旧款archive.php. 这是a wonderful cheat-sheet on the template hierarchy, 这很有用,可能值得在你的前臂上纹身。

结束

相关推荐

我的存档-posttype.php模板未加载 - 小码农CODE - 行之有效找到问题解决它

我的存档-posttype.php模板未加载

时间:2013-02-23 作者:Gab

我正在尝试创建一个存档照片。我的照片自定义帖子类型的php模板。我的问题是,当我查看页面时,它是归档文件。使用的是php模板,而不是存档照片。php。我只是想要存档照片。查看特定自定义帖子类型的归档页面时要使用的php。

我错过了什么?

请注意,在最后,我已将照片自定义帖子类型设置为主循环。

非常感谢您的时间和帮助。

<?php
//custom post type
add_action( \'init\', \'create_post_type\' );
function create_post_type() {
    register_post_type( \'photos\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Photos\' ),
                \'singular_name\' => __( \'Photo\' )
            ),
        \'public\' => true,
        \'has_archive\' => true,
        \'rewrite\' => array(\'slug\' => \'photo\'),
        \'menu_position\' => 5,
        \'taxonomies\' => array(\'genre\'),
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'post-formats\'),
        )
    );
    flush_rewrite_rules( false );
    //this enables post formats.
    add_theme_support( \'post-formats\', array( \'image\', \'chat\', \'video\', \'gallery\' ) );
    remove_post_type_support( \'post\', \'post-formats\' );
}
//hook into the init action and call create_photos_taxonomies when it fires
add_action( \'init\', \'create_photos_taxonomies\', 0 );
//create taxonomy, genres for the post type "photos"
function create_photos_taxonomies() 
{
  // Add new taxonomy, make it hierarchical (like categories)
  $labels = array(
    \'name\' => _x( \'Genres\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Genre\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Genres\' ),
    \'all_items\' => __( \'All Genres\' ),
    \'parent_item\' => __( \'Parent Genre\' ),
    \'parent_item_colon\' => __( \'Parent Genre:\' ),
    \'edit_item\' => __( \'Edit Genre\' ), 
    \'update_item\' => __( \'Update Genre\' ),
    \'add_new_item\' => __( \'Add New Genre\' ),
    \'new_item_name\' => __( \'New Genre Name\' ),
    \'menu_name\' => __( \'Genre\' ),
  );    
  register_taxonomy(\'genre\',array(\'photos\'), array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'photos\' ),
  ));
}
function my_post_class( $classes ) {
    global $post;
    $terms = wp_get_object_terms( $post->ID, \'genre\' );
    foreach ( $terms as $genre ) {
        $classes[] = $genre->slug;
    }
    return $classes;
}
add_filter( \'post_class\', \'my_post_class\' );
//show posts of post type \'photos\', \'page\' on home page
add_action( \'pre_get_posts\', \'add_my_custom_post_type\' );
//make photos custom post type the default post type for the main loop
function add_my_custom_post_type( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( \'post_type\', array( \'photos\' ) );
    return $query;
}
?>

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

如注释中所确定的,页面URI为?post_type=photos&genre=boudoir...

原因是"archive-{$post_type}.php" 在这种情况下不使用是因为页面是分类法存档,而不是自定义帖子类型存档。WordPress正在寻找taxonomy-genre.php 而不是archive-photos.php, 而找不到它,就是在拖欠旧款archive.php. 这是a wonderful cheat-sheet on the template hierarchy, 这很有用,可能值得在你的前臂上纹身。

相关推荐