WordPress custom post type archive with description - 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.

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
Custom post type name
Arguments for post type (google create post type)
Custom taxonomies
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)
create your custom post types in the functions file with a have_archive value of false in the arguments array
Add meta box function for the custom fields you want in your functions file (google this, very easy to do)
Modify your archive.php to run a new query for all post types (google wp_query, you can query a array of post types)
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).

register_post_type() has a description arg which defaults to an empty string. You can use that and get the description like e.g.:
$obj = get_post_type_object( 'my_post_type' );
echo $obj->description;

If i understand correctly, you just need a different description loaded for each post type but maintaining a single archive.php, basically you just need to display the description like so:
<?php
if (get_the_post_type_description()) {
echo get_the_post_type_description();
}
?>

Related

Can I set a custom link on a custom post type in WordPress?

I am creating custom post types in WordPress using the "Types" plugin. Those post types are not supposed to have a post page associated with them that the user would navigate to by default when clicking a particular post link. They are supposed to be merely titles that I am displaying in a grid and that should link out to separate sites. Every post title has an individual link associated with it. I am styling those post titles using the "Grid Element" creator of the "Visual Composer" plugin, so I can set which attributes of the post I want to display (in my case only the title).
When I am creating the post type, I am adding a custom field for the link url that I can then set when I create the individual post. However, I am not seeing an option to set that custom link to be the link for the post itself. I can add the link as a separate UI element of the post but I want the post title itself to be the link. Is this possible and how?
Very possible, but you will have to do some coding of your own, rather than relying on visual composer. Within the template you are using, in the loop that pulls the posts, you need to grab the meta data from that field for the current post:
<?php
// loop stuff here...
$link = get_post_meta(get_the_id(), your-key-here, true);
echo '<p>'.the_title().'</p>';
// other loop stuff...
?>
In the above, your-key-here refers to the name of the custom field you've set up for the link that gets set by the post author. The above also assumes you know enough about WordPress to know where you need to edit this code in, in the template. The code does not check for empty values and handle with a fallback, so yes, this code can be improved upon, but it sill do what you've asked in the question.
EDIT: A link for reference regarding pulling meta data/custom fields data; https://developer.wordpress.org/reference/functions/get_post_meta/

How can I change a wordpress custom post back to a standard post?

I have inherited a legacy wordpress installation which has a complex template with a couple of custom post types.
I want to migrate it to a simpler theme and convert all the custom post types back to regular posts.
I have gone into the wp_posts table and changed the values in the post_type column from "product" or "service" or whatever back to "post" and the posts in question now load into the template, but they don't show up in the list of posts in the admin page. If I load the editor on another post, and change the ID in the url bar to one of my would-be normal posts, they will load up and I can edit them, but I just can't make them show up in the posts list.
Is there somewhere else I need to change things to make Wordpress see them as standard posts?

WordPress - specific file for custom post type

I know I can create custom files for specific custom-post-types
Unfortunately, the method works for me in one case:
page-results.php for CPT = results
but does not work for page-news.php where CPT = news
or does not work for page-events.php where CPT = events
Why? can you have just a single file like that?
This might clarify some things for you:
Wordpress Template Hierarchy
The problem is, your Custom Post Types aren't actually "Pages". Even a "Page" isn't technically an actual Page, but a hierarchical Custom Post Type called "Page". Confusing? Yes. But there's a method to the madness.
The built-in Post Type known as "Pages" do function differently than the built-in Post Type called "Posts", which is why they have their own default templates and hierarchy. However, despite the fact that Wordpress allows you to extend their core to include your own Custom Post Types, at their root they're still considered "Posts" and will not follow the "Page" branch.
Your two options if you're following the standard Template naming conventions is to name your Custom Post Archive Templates "archive-results.php", "archive-news.php", and "archive-events.php".
As for your Custom Post Single Post Template, you would name them "single-results.php", "single-news.php", and "single-events.php".
If you want to fall back on an archive or single template for all three custom post types, simply don't define the separate post types and define an "archive.php" and "single.php" file.
Here's some more information on Custom Post Type Templates

Using WordPress Content Item as a Taxonomy

Is there a way (or a plugin) to make a given content type in WordPress (i.e. posts, pages, media, custom post types) act as a taxonomy? I basically want to setup a one-to-many relationship of one item in a given post type to many items of a different type.
So basically, I have a custom post type, call it Authors. I want to use a Post Type rather than a taxonomy because I need a lot more meta data than a taxonomy allows.
I then have another custom post type called Books.
Is there a way to put a box similar to the Categories or Tags metaboxes on the Author write page that display a list of all the books. I can then choose from a list of all the books, thus creating a relationship.
If this doesn't already exist, I guess I'll write a plugin for it. I thought I'd give this a try first, just in case.
Have you considered adding new meta fields to a custom taxonomy? Unfortunately, you ned to either create a new table for the meta storage or use wp_options, but it does work and is relatively future-friendly. Remember to start your option names with an underscore, though.
http://www.strangework.com/2010/07/01/how-to-save-taxonomy-meta-data-as-an-options-array-in-wordpress/

custom post types wordpress

I have created a custom post type called show.. How do I tell wordpress to go to show.php for loading the content instead of single.php?
This is crucial since single.php does not carry the code for the extra added fields I have, thats why I want to use show.php
thanks
The WordPress docs say your template file must be single-show.php, not just show.php.
single-<post-type>.php
The single post template used when a single post from a custom post type is queried. For example, single-books.php would be used for displaying single posts from the custom post type books. index.php is used if the query template for the custom post type is not present.
1.Copy single.php.
2.Rename it single-show.php.
3.Now modify this template as per your requirement.

Resources