WordPress.org

Ready to get started?Download WordPress

Forums

Custom post types: archive-{posttype}.php not being recognized (7 posts)

  1. stevecoy
    Member
    Posted 3 years ago #

    I have a custom post type, registered as ps_product. My template file single-ps_product.php works perfectly, but archive-ps_product.php is not being recognized at all--it's defaulting to archive.php.

    Here is how I'm registering the post type and the custom taxonomy, perhaps one of my array values is wrong or missing, or I'm not naming my template file correctly.

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
    
    	$labels = array(
    		'name' => _x('Products', 'post type general name'),
    		'singular_name' => _x('Product', 'post type singular name'),
    		'add_new' => _x('Add New', 'book'),
    		'add_new_item' => __('Add New Product'),
    		'edit_item' => __('Edit Product'),
    		'new_item' => __('New Product'),
    		'view_item' => __('View Product'),
    		'search_items' => __('Search Products'),
    		'not_found' =>  __('No products found'),
    		'not_found_in_trash' => __('No products found in Trash'),
    		'parent_item_colon' => '',
    		'menu_name' => 'Products'
    
    	  );
    		$icon = get_bloginfo ('template_directory');
    		$icon .= '/images/admin-ps-products-icon.png';
    
    	  $productargs = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'show_in_menu' => true,
    		'query_var' => true,
    		'rewrite' => array('slug' => 'product', 'with_front' => 'false'),
    		'capability_type' => 'post',
    		'has_archive' => 'products',
    		'hierarchical' => false,
    		'menu_position' => 20,
    		'menu_icon' => $icon,
    		'taxonomies' => array('product_cat', 'post_tag'),
    		'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields', 'page-attributes')
    	  ); 
    
    	register_post_type( 'ps_product', $productargs);
    }
    
    //hook into the init action and call create_book_taxonomies when it fires
    add_action( 'init', 'create_product_taxonomies', 0 );
    
    function create_product_taxonomies()
    {
      $labels = array(
        'name' => _x( 'Product Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'Product Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Product Categories' ),
        'all_items' => __( 'All Product Categories' ),
        'parent_item' => __( 'Parent Product Category' ),
        'parent_item_colon' => __( 'Parent Product Category:' ),
        'edit_item' => __( 'Edit Product Category' ),
        'update_item' => __( 'Update Product Category' ),
        'add_new_item' => __( 'Add New Product Category' ),
        'new_item_name' => __( 'New Product Category Name' ),
        'menu_name' => __( 'Product Categories' ),
      ); 	
    
      register_taxonomy('product_cat', array('ps_product'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'products' ),
      ));
    }
  2. Can you try with the default 2010 theme?

  3. stevecoy
    Member
    Posted 3 years ago #

    I activated the default theme, copied the above code to functions.php, and copied my archive-ps_products.php into the theme folder. Then I deactivated all plugins.

    Still no success.

  4. Andrew Nacin
    Lead Developer
    Posted 3 years ago #

    Works for me.

    Added the code, flushed the rewrite rules. Went to /product/product-1/, and saw my product. Went to /products/, and saw archive.php load. Created archive-ps_product.php simply with "Test" as the content, and it loaded on refresh of /products/.

  5. stevecoy
    Member
    Posted 3 years ago #

    OK, I got /products working fine. Here is the dev site:

    http://semler.joobble.com/products

    I guess then what isn't working are any sub-category archives and and nav menu items based on the custom taxonomy.

    From the main navigation, click Products or any one of the drop-downs (Aviation, Fluid Handling, Liquid Purification). These are all Product Category-type menu items. These should all have the same grid-type layout as /products. But instead they are reverting to the archive.php layout.

    Do I need to create separate archive-ps_product-{category}.php files for each of my categories? If so this is no big deal, but I thought the point of creating a custom template file was so that WP would use it for ALL archives of that custom post type/taxonomy.

  6. stevecoy
    Member
    Posted 3 years ago #

    Oh wow.

    taxonomy-ps_product.php did the trick.

    Then what is the point of even having archive-{posttype.php}?

  7. Paul Gregory
    Member
    Posted 3 years ago #

    Your taxonomy happens to be named the same as your posttype, with the same slug. Not everyone will do that.

    /products/fluid-purification is using "products" as taxonomy (so it uses taxonomy-ps_product).
    /products/page/2 is using "products" as posttype (so it uses archive-ps_product).

    I have a custom post type (job) with three custom taxonomies (sectors, locations, type). I had pages that filtered by the taxonomy but which weren't using archive-job.php. Finding your reference here to taxonomy-[whatever] was actually very helpful! Now taxonomy-sector etc work great for me.

    As my taxonomies and posttypes are named differently, it is more obvious what is going on: the page that filters by taxonomy isn't actually filtering by post type, it's just coincidence that all the results are from one post type (although not an unexpected coincidence given that the taxonomy is exclusive to that posttype).

    So there *is* a point to archive-{posttype}, and it's the point you'd expect it to have.

    To reiterate, the relevant template is selected based on the requested information, not on the results - and pay close attention to what's actually requested.

    I hope this helps someone.

    --

    Now, if there is a flaw here, it's that I can use
    /taxonomy/
    or
    /posttype/
    but
    /posttype/taxonomy/
    doesn't work (for me at least; not yet sure whether I'm missing something or WP is).

    Obviously stevecoy doesn't actually want to be using
    /products/products/
    but by using /posttype/taxonomy/ all the items would use the posttype archive as expected.

    I can totally see how people may expect /taxonomy/ to return a /posttype/ template when the taxonomy is unique to that post type, because I fell into that trap myself.

Topic Closed

This topic has been closed to new replies.

About this Topic