Custom post types and associate custom post template
-
I have created a child theme for my site, with a new functions.php and style.css file in the child theme folder. I switched my site to use the child theme and it appears to be working correctly.
I have added the following to my child theme functions.php file to create a custom post type and add it to my admin menu, called Dogs (on the admin menu). This appears to work as expected.
if ( ! function_exists(‘custom_post_type_dog’) ) {
// Register Custom Post Type
function custom_post_type_dog() {$labels = array(
‘name’ => _x( ‘Dogs’, ‘Post Type General Name’, ‘text_domain’ ),
‘singular_name’ => _x( ‘Dog’, ‘Post Type Singular Name’, ‘text_domain’ ),
‘menu_name’ => __( ‘Dogs’, ‘text_domain’ ),
‘name_admin_bar’ => __( ‘Dog’, ‘text_domain’ ),
‘archives’ => __( ‘Dog Archives’, ‘text_domain’ ),
‘attributes’ => __( ‘Dog Attributes’, ‘text_domain’ ),
‘parent_item_colon’ => __( ‘Parent Dog:’, ‘text_domain’ ),
‘all_items’ => __( ‘All Dogs’, ‘text_domain’ ),
‘add_new_item’ => __( ‘Add New Dog’, ‘text_domain’ ),
‘add_new’ => __( ‘Add New’, ‘text_domain’ ),
‘new_item’ => __( ‘New Dog’, ‘text_domain’ ),
‘edit_item’ => __( ‘Edit Dog’, ‘text_domain’ ),
‘update_item’ => __( ‘Update Dog’, ‘text_domain’ ),
‘view_item’ => __( ‘View Dog’, ‘text_domain’ ),
‘view_items’ => __( ‘View Dog’, ‘text_domain’ ),
‘search_items’ => __( ‘Search Dog’, ‘text_domain’ ),
‘not_found’ => __( ‘Not found’, ‘text_domain’ ),
‘not_found_in_trash’ => __( ‘Not found in Trash’, ‘text_domain’ ),
‘featured_image’ => __( ‘Featured Image’, ‘text_domain’ ),
‘set_featured_image’ => __( ‘Set featured image’, ‘text_domain’ ),
‘remove_featured_image’ => __( ‘Remove featured image’, ‘text_domain’ ),
‘use_featured_image’ => __( ‘Use as featured image’, ‘text_domain’ ),
‘insert_into_item’ => __( ‘Insert into Dog’, ‘text_domain’ ),
‘uploaded_to_this_item’ => __( ‘Uploaded to this Dog’, ‘text_domain’ ),
‘items_list’ => __( ‘Dogs list’, ‘text_domain’ ),
‘items_list_navigation’ => __( ‘Dogs list navigation’, ‘text_domain’ ),
‘filter_items_list’ => __( ‘Filter Dogs list’, ‘text_domain’ ),
);
$args = array(
‘label’ => __( ‘Dog’, ‘text_domain’ ),
‘description’ => __( ‘Custom post that contains the details of a single Dog’, ‘text_domain’ ),
‘labels’ => $labels,
‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘comments’, ‘custom-fields’ ),
‘taxonomies’ => array( ‘category’, ‘post_tag’ ),
‘hierarchical’ => false,
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘menu_position’ => 5,
‘menu_icon’ => ‘dashicons-buddicons-activity’,
‘show_in_admin_bar’ => true,
‘show_in_nav_menus’ => true,
‘can_export’ => true,
‘has_archive’ => true,
‘exclude_from_search’ => false,
‘publicly_queryable’ => true,
‘capability_type’ => ‘page’,
);
register_post_type( ‘dog_listing’, $args );}
add_action( ‘init’, ‘custom_post_type_dog’, 0 );
}I have created a new dog_listing.php file in my child theme folder by copying most of the parent theme’s single-post.php file content.
<?php
colormag_sidebar_select();/**
* Hook: colormag_after_body_content.
*/
do_action( 'colormag_after_body_content' );get_footer();
This theme is shown under 'Dog Attributes', 'Template' when I create a new Dog from the admin screen – which is what I want. It does not appear when I create a standard post or page – this is what I expected and wanted to happen.
My problem is that I do not seem to be able to prevent other post templates being offered to the user under the Dog Attributes, Template drop-down when creating a new Dog. As an example, I see default template, theme, elementor etc. listed as other choices.).
How do I ensure that only my new dog_listing template is used for all Dog posts that are created and that it is automatically selected? Ideally, I wouldn't even show the user creating the new Dog post the list of templates, just force the correct one to be used.
Lastly, how do I ensure that content entered into the new custom Dog item that they have created is correctly displayed using the template's layout when viewed. I am aware that the template doesn't currently include any formatting for content, but it would be useful to have an example to work with?
-
Hi there,
It looks like you’re in the wrong forum. It appears that you’re using the open-source WordPress software and therefore you need to seek help at these forums:
https://wordpress.org/support/forums/
These forums are for WordPress.com hosted sites only. If you want to know more about the differences between WordPress.com and the WordPress software you can read this document:
-
- The topic ‘Custom post types and associate custom post template’ is closed to new replies.