custom post types wordpress - 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.

Related

Parent permalink slug before CPT slug

I'm currently busy making a FAQ for my website but i'm struggling with the permalink structure.
I have made a regular page where people can see an overview of the FAQs. I called that page Klantadvies.
I installed a Custom Post Type plugin and made a post type called faq. I also made a taxonomy which is called onderwerp.
Unfortunately the plugin won't allow me to have a page parent in the slug. So i'm wondering if it's possible to have my page slug (klantadvies) in front of the custom post type slug. For example:
http://mypage.com/klantadvies/faq/onderwerp
Thnx in advance!
Rather than using a plugin for your post types and taxonomies, I'd recommend you use these two solutions:
https://generatewp.com/post-type/
https://generatewp.com/taxonomy/
You can use those to create your Post Type and Taxonomies. Within each of those configs you're allowed to setup the slugs / URL structure that you want to use.
On top of that you're going to need to setup a custom rewrite rule to handle echoing out the values you're looking for on the appropriate layout.
https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

WordPress custom post type archive with description

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();
}
?>

How to create template for post in wordpress

I know how to create a custom template for a specific wordpress page. However, I would like to create a template for a specific custom post type. Is that possible and if it is possible, how can I do that?
To add a custom template for custom post types you can use single-{posttype}.php template. You can look at the template hierarchy diagram for more details.
For example if your post type is called books, your filename for the template should be single-books.php
If you have created a custom post type
If you have created a custom post type then you need to create a template who's name will be smiler to the custom post type for example if you have created a custom post type "product" then you will create a template with name product.php in your theme directory and to display a particulate post of your custom post type you will create another template with name single-product.phpthis template will be used to display your custom post and remember whatever custom post you create same name will be used to name your custom post type template and do not forget to add single in the beginning.
Template for a particulate post or a post of particulate category
And to display particular post in different template and to use different template to display post of particular category you can follow this link.

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

Wordpress custom post type page

When creating a custom post type and fill it with some posts wordpress generates a page for that custom post type.
But I want to be able to add content to that page so I create a regular page and choose a template where I manually call the custom post type posts and loop them out. And now I can just add content in the wysiwyg editor. But this causes a conflict between these two pages, especially if the CPT has the same name as the page.
And now the question: Is there a way to always show the page where I have chosen the template for the CPT to always show? Even when someone try's to manually enter the url for the CPT generated page?
Here is what I understood from your question. You have custom post type. Then you created a page-template, that loops through your custom post types and output them. Now when you create a new page and assign it the page-template you have created you run into problems, if it has the same name as the custom post type.
Solution: I think what you need to do is change the custom-page slug from the admin menu. Then you would be able to directly reference it by the URL.

Resources