Search code examples
wordpressarchive

Link to an archive page that displays all posts


I'm having a little trouble with Wordpress archives, or more accurately, trying to create a custom one.

I'm making a custom theme for a client, and they specified that they want three items from each category (in this case, News (the default post type) and reviews (a custom post type)) to be present on the homepage.

Now that was the easy part (two loops on the homepage, wp_query 3 posts per page), however they want the user to be able to click on a button, in order to view all of the posts of that specific type (rather than by category).

Archives Button

However I have no idea how i'd even approach this.

I understand I can make a custom page-template and apply it to a page, but realistically I'd like to make something that's packaged within the theme and works "straight out of the box", as it were, so the client doesn't have to make the page himself in order to make it work.

Thanks in advance for your help!


Solution

  • There are a few things to do:

    1. When you call register_post_type($name,$args), add 'rewrite' => true and 'has_archive' => true to the $args array. (note: don't add it to the label section, that's a mistake that people make often!)

    2. Then you create a page called archive-custom_post_type.php and place it in your theme folder. Add some php/html to it (containing a wp loop etc.) and replace "custom_post_type" with your type, obviously :-) If you want the same archive page for all your content types, you could also use the archive.php default template file. Here you can find how wordpress searches for available templates: http://codex.wordpress.org/images/1/18/Template_Hierarchy.png

    Then the page should be available using http://yoursite/custom_post_type/

    If not, you should go to the wp-settings and save your permalink structure again. http://yoursite//wp-admin/options-permalink.php -> save.