Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress custom post type archive with description

Tags:

wordpress

I have a common design pattern I'm not entirely sure how to best execute in WordPress. The layout is a listing of post teasers (title, trimmed body, image) on an overview page. Let's say example.com is a boating safety company, and at example.com/classes there's a listing of their posts of the class type using archive.php or archive-$posttype.php

So far it's dead simple and default WordPress behaviour. But, I want to have some intro information about this type of information in general on this overview page. Furthermore, let's say I have 10 custom post types hypothetically, and each one would follow this pattern with a listing and a general introductory paragraph on the archive page.

What I don't want to do is have a page for each of these types, and a custom query done for each of them in a page template. If there were a way to associate a description, and even better custom fields, with the post type itself (not on posts of that type but the type itself) that would be the ideal scenario. Ideally I'd like to have this all wrapped into my archive.php template.

Does anybody know of a good way to pull this off?

This may or may not be helpful, but I'll be creating post types in code but using Advanced Custom Fields for the custom fields themselves.

like image 645
nathansh Avatar asked Nov 14 '14 01:11

nathansh


1 Answers

read up on creating post types there is a option for has archive, set it to no, it wont create the standard urls for list view etc. You can modify archive.php to include all post types as well.

I think you are confused about the way wordpress custom post types exist on your server. Basically they only exist in php code, so everytime the wp system loads, your custom code runs to create the custom post type...you are basically setting the rules of the post type at this stage -- so

  1. Custom post type name
  2. Arguments for post type (google create post type)
  3. Custom taxonomies
  4. Meta boxes for admin screen (recommended instead of using custom fields...)

Wordpress if it sees the posttype is public & has_archive will resolve any url requests for the post-type with the appropriate template...for example a post type of "mypost". In this case typing www.example.com/mypost will return back the archive view from your theme (archive.php or archive-mypost.php). This file is located in your theme and you can modify it to include a archive of all post types (replace get_posts with wp_query for total control over posts)

On these archive pages you will see a function called get_posts / wp_query and then standard loop structure to loop through each item. These functions query the wp_posts table in the database to return rows with the post_type you are querying (asking the function to find)(see the DB and the column post_type in wp_posts). There is also a secondary table to store unlimited extra fields (dont take me literally there, but in theory & memory constraints pushed aside, there is no limit set by WP on the extra fields) in a table called post_meta which uses the post id (unique identifer of the row in wp_posts). You can display this information by using get_post_meta() (google it)

So where does that leave you assuming there is a theme installed? (prob best to create a child theme here)

  1. create your custom post types in the functions file with a have_archive value of false in the arguments array
  2. Add meta box function for the custom fields you want in your functions file (google this, very easy to do)
  3. Modify your archive.php to run a new query for all post types (google wp_query, you can query a array of post types)
  4. Any other info like descriptions, default save behaviour can be set using php in your functions file and hooked to the action you want to modify (eg save post, etc).
like image 162
David Avatar answered Sep 28 '22 10:09

David