0

I am trying to create a custom archive index page for a custom post type. I can't get it to work, I have followed this guide: https://codex.wordpress.org/Creating_an_Archive_Index.

Here's what I have been trying:

1. Created a custom post type:

function references(){
    register_post_type('cows',
     [
         'labels'       => [
             'name'          => __('Cows'),
             'singular_name' => __('Cow'),
         ],
         'public'       => true,
         'has_archive'  => true,
         'supports'     => ['title', 'editor', 'thumbnail'],
         'rewrite'      => array(
           'slug'       => 'cows'
         ),
     ]
    );
}
add_action('init', 'references');

2. Created a archive-cows.php page for all the custom post type's posts.

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
    the_title();
        //
        // Post Content here
        //
    } // end while
} // end if

3. Created a template called Cows in file: cows.php

  /*
    Template name: Cows
  */

  get_header();

  wp_get_archives(array(
    'type' => 'monthly',
    'post_type' => 'cows',
  ));
get_footer(); 

4. Created a new page (backend) and assigned it to template: Cows

At the moment WordPress is loading archive-cows.php (because post type slug: cows and page template slug: cows), where I would like it to load cows.php template file, and then in that template load the archives for cows (would not like to use a custom query without a archive page).

  • You should add a loop about all posts from your custom post type inside the cows.php template. Thats create a list if all posts from this post type and list theme. see wordpress.stackexchange.com/questions/165793/… – bueltge Jan 5 '18 at 9:39
  • Isn't best practice to use a archive-something.php to list all of the desired posts? If a user visites domain.dk/custom-post-archive-slug it would be blank if use of template file? :) – simon Jan 5 '18 at 10:01
  • This template is for a view on a archive, not in a best practice to reference inside a template. – bueltge Jan 7 '18 at 18:40
1

The first parameter passed to register_post_type is the post type key which is used by the theme template hierarchy. register_post_type documentation.

The cows.php is a page template, so to use it create a new page and select the 'Cows' template. You will need to alter the slug of this page so it doesn't conflict with the existing www.example/cows of the post type archive page.

  • Sorry. I forgot to change register_post_type from references to cows, in this example. Locally I use another setup. Hope this isn't misleading. It's edited now. So with the current edited setup it is not working :) – simon Jan 5 '18 at 10:25
  • Hmm. It doesn't seems to work. I can't get wordpress to display a custom index archive page, and then all the post for the archive/post-type. It's the wp_get_archives function that I can't get to work properly I think. – simon Jan 5 '18 at 11:05
  • To confirm, you need to view the page, not the post type archive. Eg. www.example.com/your-page-slug not www.example.com/cows. Also, you'll need to be running WordPress 4.4 or above to use the post_type parameter in wp_get_archives – Andrew Jan 5 '18 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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