0

Because in all my attempts to get a Custom Post Type Archive I only get a 404

Trying to organize different post-types in archive.php

I'm trying to do this, in 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 =====*/

?>

Here is the code to register my 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 );

}
9
  • And what is the result of your effort? You don't actually explain what goes wrong.
    – s_ha_dum
    Commented Apr 27, 2014 at 3:49
  • Thanks , well , I actually said that I always get a 404 error page.... , I usually do this to keep all my template well organized, so this just work well with categories o single (in_category , is_category, is_single).
    – Locke
    Commented Apr 27, 2014 at 3:52
  • 1
    what does your post type registration code look like? 404s happen before the template is loaded, so you need to fix the cause of the 404 before you do anything template related.
    – Milo
    Commented Apr 27, 2014 at 4:04
  • ok .. my custom_post_type looks like: gist.github.com/DreamsEngineering/9fa9dde7cca888286fcf
    – Locke
    Commented Apr 27, 2014 at 4:05
  • what URL are you trying to access the post type archive from? and do you have pretty permalinks enabled?
    – Milo
    Commented Apr 27, 2014 at 4:13

1 Answer 1

0

A few things here. I will not make use of variables here. To be honest, I really don't see the usefulness of variables here. Don't complicate your code unnecessarily. Also don't use underscores in your names.

You've place your code in a function, which is not wrong. However, if you do your registration in a function, that function should be hooked to at least the init hook.

I quote from the codex - register_post_type

Create or modify a post type. register_post_type should only be invoked through the 'init' action. It won't work at all if called before 'init', and aspects of the new post type will work incorrectly if called later.

I would also not use the conditionals that you have used in your archive.php You need to take a look at template hierarchy. Wordpress uses archive.php to display CPT's. You can also create a custom archive template which will be used instead if it exists to display your CPT. You can just simply copy your archive.php template and rename it archive-{custom-post-type}.php, in your case archive-brand-voice.php. Wordpress will now use archive-brand-voice.php to display your post type 'brand-voice'. You can do the same for your single.php template.

One thing to remember, to make this archive pages work, you will need to have 'has_archive' => true, when your register your post type.

You should also not use the post type name as text domain name in your translation strings. As it currently stands, your translation string won't work. If you open your style.css, in the header you will find a line like this. (This is from the twentyfourteen theme)

Text Domain: twentyfourteen  

This is the text domain name that you will use in your translatable strings. If you change this to anything else, your strings won't work when translate them. your string should look like this

_x( 'Brand Voice', 'Adding Brand Voice', 'text-domain-name' ),

You can also omit the text domain name. Your string should then be

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

Don't forget to flush your permalinks.

After all said, your code should look like this

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' );
1
  • Thanks for the time you took to write this explanation, I finally made this work and I follow your advices... Only to add , I use varibales because I'm using like 4 more Custom_Post_Types, so to save time , I use variables :P Thanks Again!
    – Locke
    Commented Apr 28, 2014 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.