I have created a custom post type "toornament" in my wp.
I have done a single-toornament.php in my plugin.
When i require it in my index.php, i have a fatal error...
Maybe it can be a pot configurating erro ? I give you the code below.
Do you know where it can be from ?
<?php
function toornament_post() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Toornaments', 'Post Type General Name', 'twentytwenty' ),
'singular_name' => _x( 'Toornament', 'Post Type Singular Name', 'twentytwenty' ),
'menu_name' => __( 'Toornaments', 'twentytwenty' ),
'parent_item_colon' => __( 'Parent Toornament', 'twentytwenty' ),
'all_items' => __( 'All Toornaments', 'twentytwenty' ),
'view_item' => __( 'View Toornament', 'twentytwenty' ),
'add_new_item' => __( 'Add New Toornament', 'twentytwenty' ),
'add_new' => __( 'Add New', 'twentytwenty' ),
'edit_item' => __( 'Edit Toornament', 'twentytwenty' ),
'update_item' => __( 'Update Toornament', 'twentytwenty' ),
'search_items' => __( 'Search Toornament', 'twentytwenty' ),
'not_found' => __( 'Not Found', 'twentytwenty' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwenty' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'Toornament', 'twentytwenty' ),
'description' => __( 'Toornament information et inscription', 'twentytwenty' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'query_var' => 'toornament',
'capability_type' => 'post',
'show_in_rest' => true,
);
// Registering your Custom Post Type
register_post_type( 'toornament', $args );
}
I have add this :
function get_custom_post_type_template( $single_template ) {
global $post;
if ( 'toornament' === $post->post_type ) {
$single_template = dirname( __FILE__ ) . '/single-toornament.php';
}
return $single_template;
}
add_filter( 'single_template', 'get_custom_post_type_template' )
if you create single-{post-type}.php template you should not include it to the index.php
For adding a single template for your Custom Post Type there are two ways:
Copy it to your Child-Theme
hook it via single_template filter hook. here you will find how to do it
your code to create a new Custom Post Type from your question works.
Related
function create_post_type_opportunities() {
register_post_type( 'opportunities',
// CPT Options
array(
'labels' => array(
'name' => __( 'Opportunities'),
'singular_name' => __( 'Opportunities')
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'opportunities'),
'supports' => array('title','thumbnail','editor','icon' ),
)); }
// Hooking up our function to theme setup
add_action( 'init', 'create_post_type_opportunities');
/* Custom Post Type for our Add opportunities*/
here is my code kindly check my code how can i create separate category for my new custom post type
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Movies', 'twentythirteen' ),
'parent_item_colon' => __( 'Parent Movie', 'twentythirteen' ),
'all_items' => __( 'All Movies', 'twentythirteen' ),
'view_item' => __( 'View Movie', 'twentythirteen' ),
'add_new_item' => __( 'Add New Movie', 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'movies', 'twentythirteen' ),
'description' => __( 'Movie news and reviews', 'twentythirteen' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => true,
// This is where we add taxonomies to our CPT
'taxonomies' => array( 'category' ),
);
// Registering your Custom Post Type
register_post_type( 'movies', $args );
}
add_action( 'init', 'custom_post_type', 0 );
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'custom_post_type', 0 );
To display your custom post types on the same category page as your default posts, you need to add this code into your theme’s functions.php or a site-specific plugin.
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if( is_category() ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('nav_menu_item', 'post', 'movies'); // don't forget nav_menu_item `enter code here`to allow menus to work!
$query->set('post_type',$post_type);
return $query;
}
}
i don't know how to create a new custom post type in WordPress so can you please show me how to create it.
In below function you can create new custom post type in WordPress.
function my_first_custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Movies', 'twentythirteen' ),
'parent_item_colon' => __( 'Parent Movie', 'twentythirteen' ),
'all_items' => __( 'All Movies', 'twentythirteen' ),
'view_item' => __( 'View Movie', 'twentythirteen' ),
'add_new_item' => __( 'Add New Movie', 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
'edit_item' => __( 'Edit Movie', 'twentythirteen' ),
'update_item' => __( 'Update Movie', 'twentythirteen' ),
'search_items' => __( 'Search Movie', 'twentythirteen' ),
'not_found' => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'movies', 'twentythirteen' ),
'description' => __( 'Movie news and reviews', 'twentythirteen' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
// Registering your Custom Post Type
register_post_type( 'movies', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'my_first_custom_post_type', 0 );
I hope this answer is useful for you to create a new custom post type in WordPress.
I use the custom post type "events". I make a custom page template page-events.php.
I make a page "Events" (slug events) as archive page.
Select "Events" as page template.
Nothing will show, but when I switch over to default page template insteed of "Events" everythings works fine.
The WP body class shows events-template-default single single-events
So, I don't really why?
My settings:
page-events.php
<?php
/*
Template Name: Events
*/
?>
...
<?php
$args = array(
'post_type' => array( 'events' ),
'posts_per_page' => '-1',
'order' => 'ASC'
);
$news_query = new WP_Query( $args );
if ( $news_query->have_posts() ) : ?>
<?php while ( $news_query->have_posts() ) : ?>
<?php $news_query->the_post(); ?>
...
<?php endwhile; endif; wp_reset_postdata(); ?>
functions.php
<?php
// Register Custom Post Type "Events"
function cpt_events() {
$labels = array(
'name' => _x( 'Events', 'Post Type General Name', 'theme_mmm' ),
'singular_name' => _x( 'Events', 'Post Type Singular Name', 'theme_mmm' ),
'menu_name' => __( 'Events', 'theme_mmm' ),
'name_admin_bar' => __( 'Events', 'theme_mmm' ),
'parent_item_colon' => __( 'Events:', 'theme_mmm' ),
'all_items' => __( 'Alle Events', 'theme_mmm' ),
'add_new_item' => __( 'Event hinzufügen', 'theme_mmm' ),
'add_new' => __( 'Event hinzufügen', 'theme_mmm' ),
'new_item' => __( 'Event hinzufügen', 'theme_mmm' ),
'edit_item' => __( 'Event bearbeiten', 'theme_mmm' ),
'update_item' => __( 'Aktualisieren', 'theme_mmm' ),
'view_item' => __( 'Events ansehen', 'theme_mmm' ),
'search_items' => __( 'Suchen', 'theme_mmm' ),
'not_found' => __( 'Keine Treffer', 'theme_mmm' ),
'not_found_in_trash' => __( 'Keine Treffer', 'theme_mmm' ),
);
$args = array(
'label' => __( 'Events', 'theme_mmm' ),
'description' => __( 'Beschreibung', 'theme_mmm' ),
'labels' => $labels,
'supports' => array( 'title', 'editor' ),
'taxonomies' => array(''),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-calendar-alt',
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => true,
'capability_type' => 'page',
);
register_post_type( 'events', $args );
}
add_action( 'init', 'cpt_events', 0 );
?>
If you're trying to make an archive page for your post type events, copy stock archive.php of your theme, rename it to archive-events.php and modify it from there to suit your needs.
If you're trying to make a template for a single page of events post type, copy stock single of your theme, rename it to single-events.php and modify it from there.
P.S. You've name your post type events. It's usually a good practice to use singular form instead of plural one, so event in your case
Refer to this image to see how Wordpress selects correct template and this page for hierarchy related information.
I tried to ask this question in the Wordpress Devlopment network with no success, I'm trying to display only 3 specific categories in permalinks for custom-post-type categories.
Right now the permalinks structure (that is the structure given by the theme i'm using and that i'm modifying through a child theme)is as follows:
www.myfoodblog.com/recipes/the-post-title/
^ ^ ^
root custom-post-type title
some post are under 3 categories that i would like to display in permalinks which are restaurant, users and admin to get something like this
www.myfoodblog.com/recipes/restaurant/the-post-title/
www.myfoodblog.com/recipes/users/the-post-title/
www.myfoodblog.com/recipes/admin/the-post-title/
leaving the original structure for posts that are not in those categories.
I tried using this plugin but it will display the custom post type category for all posts.
I also tried to follow the instructions provided in this question but it's not working, no changes are made in the permalinks structure.
Any advice is much appreciated.
You have to work with both post_type_link and add a rewrite rule
function recipes_post_link( $post_link, $id = 0 ){
$post = get_post( $id );
if ( is_object( $post ) ){
$terms = get_the_terms( $post->ID, 'recipe-category' );
foreach($terms as $term) {
if( $term->slug == 'restaurants' || $term->slug == 'users'){
return str_replace( site_url()."/recipes" , site_url()."/recipes/".$term->slug , $post_link );
}
}
}
return $post_link;
}
add_filter( 'post_type_link', 'recipes_post_link', 1, 3 );
function custom_rewrite_basic() {
add_rewrite_rule('^recipes/(.+)/(.+)', 'index.php?recipe=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_basic');
Create Post Type
add_action( 'init', 'codex_recipes_init' );
/**
* Register a recipes post type.
*
* #link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function codex_recipes_init() {
$labels = array(
'name' => _x( 'Recipes', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Recipe', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Recipes', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Recipe', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'recipe', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Recipe', 'your-plugin-textdomain' ),
'new_item' => __( 'New Recipe', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Recipe', 'your-plugin-textdomain' ),
'view_item' => __( 'View Recipe', 'your-plugin-textdomain' ),
'all_items' => __( 'All Recipes', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Recipes', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Recipes:', 'your-plugin-textdomain' ),
'not_found' => __( 'No recipes found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No recipes found in Trash.', 'your-plugin-textdomain' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'your-plugin-textdomain' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'recipes/%type%' ),
'capability_type' => 'post',
'has_archive' => 'recipes',
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'recipes', $args );
}
Important: look on rewrite rule: 'rewrite'=> array( 'slug' => 'recipes/%type%' ),
Create Custom Taxonomy
// hook into the init action and call create_recipes_taxonomies when it fires
add_action( 'init', 'create_recipes_taxonomies', 0 );
function create_recipes_taxonomies() {
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Type', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Type', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Types', 'textdomain' ),
'popular_items' => __( 'Popular Types', 'textdomain' ),
'all_items' => __( 'All Types', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Type', 'textdomain' ),
'update_item' => __( 'Update Type', 'textdomain' ),
'add_new_item' => __( 'Add New Type', 'textdomain' ),
'new_item_name' => __( 'New Type Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate types with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove types', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used types', 'textdomain' ),
'not_found' => __( 'No types found.', 'textdomain' ),
'menu_name' => __( 'Types', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'type' ),
);
register_taxonomy( 'type', 'recipes', $args );
}
Add Filter Post Type Link
function recipes_post_link( $post_link, $id = 0 ){
$post = get_post( $id );
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'type' );
if( $terms ){
return str_replace( '%type%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'recipes_post_link', 1, 3 );
Important: After all go to permalink options and reset it.
I am developing a plugin which registers a custom post type and custom taxonomy for that post type. Now, what to do once any one deactivates the plugin ? Should I de-register my custom post or taxonomy or any thing else ? I am totally new to plugin development kindly let me know what should i do on plugin deactivation ? Here is the code for my custom post type.
function portolfio_post_type() {
$labels = array(
'name' => __( 'Portfolio Post Type', 'Post Type General Name' ),
'singular_name' => __( 'Portfolio Post Type', 'Post Type Singular Name' ),
'menu_name' => __( 'Portfolio'),
'name_admin_bar' => __( 'Post Type'),
'archives' => __( 'Portfolio Archives' ),
'parent_item_colon' => __( 'Parent Item:' ),
'all_items' => __( 'All Portfolios'),
'add_new_item' => __( 'Add New Portfolio'),
'add_new' => __( 'Add New'),
'new_item' => __( 'New Portfolio' ),
'edit_item' => __( 'Edit Portfolio' ),
'update_item' => __( 'Update Portfolio' ),
'view_item' => __( 'View Portfolio' ),
'search_items' => __( 'Search Portfolio' ),
'not_found' => __( 'Not found'),
'not_found_in_trash' => __( 'Not found in Trash' ),
'featured_image' => __( 'Featured Image'),
'set_featured_image' => __( 'Set featured image' ),
'remove_featured_image' => __( 'Remove featured image' ),
'use_featured_image' => __( 'Use as featured image'),
'insert_into_item' => __( 'Insert into Portfolio'),
'uploaded_to_this_item' => __( 'Uploaded to this Portfolio'),
'items_list' => __( 'Portfolios list' ),
'items_list_navigation' => __( 'Portfolios list navigation'),
'filter_items_list' => __( 'Filter Portfolios list'),
);
$args = array(
'label' => __( 'Portfolio' ),
'description' => __( 'Portfolio Post type to add portfolio of your work' ),
'labels' => $labels,
'supports' => array('title', 'editor', 'thumbnail' ),
'taxonomies' => array( 'portfolio_category'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'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' => 'post',
);
register_post_type( 'portfolio', $args );
}
add_action( 'init', 'portolfio_post_type', 0 );
I am just pasting this code inside my plugin file and on activation it creates custom post type fine, but I don't know what to do for this post type on deactivation of the plugin ... ? Any one to help me please ...
Also any good tips in plugin development would really be appreciated :)
Yes, you should delete all your code on plugin uninstall. For that you can create your uninstall.php file and place that file inside your main plugin directory. The code of your uninstall.php will look like shown below:
Note: This is my plugin's uninstall.php file.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) )
exit();
function go_delete_now() {
global $wpdb;
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => 'films',
'post_status' => 'any' ) );
foreach ( $posts as $post ){
wp_delete_post( $post->ID, true );
}
}
go_delete_now();
// Set global
global $wpdb;
// Delete terms
$wpdb->query( "
DELETE FROM
{$wpdb->terms}
WHERE term_id IN
( SELECT * FROM (
SELECT {$wpdb->terms}.term_id
FROM {$wpdb->terms}
JOIN {$wpdb->term_taxonomy}
ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id
WHERE taxonomy = 'films_category'
) as T
);
");
// Delete taxonomies
$wpdb->query( "DELETE FROM {$wpdb->term_taxonomy} WHERE taxonomy = 'films_category'" );
Note: In my case my post type name is films and category for films is films_category.
You can simply change those post and taxonomy names as per your own needs and it will work fine for you. If any thing goes wrong let me know.
You can eventually convert your cpt posts as draft posts, on the uninstall action and add an admin notice.