I am wanting to create a custom post type called testimonials, for this I am wanting to allow the administrator that chance to add a company name/username and what testimonial they have given, I understand I can do this by declaring a custom post-type in my functions.php file, however it does not seem to work and all I get is the normal post fields, can someone tell me where I am going wrong, or how I would do this please?
function testimonials_register() {
$args = array(
'label' => __('Testimonials'),
'singular_label' => __('Testimonial'),
'public' => true,
'show_ui' => true,
'capability_type' => false,
'hierarchical' => false,
'rewirte' => true,
'supports' => array('title', 'editor')
);
register_post_type('testimonial', $args);
}
You spelled rewrite wrong, for starters.
You are missing add_action('init', 'testimonials_regiser'); afer the function.
A more thorough code that's a bit more customized can be as follows:
function testimonials_register() {
$labels = array(
'name' => _x( 'Testimonials', 'post type general name' ),
'singular_name' => _x( 'Testimonial', 'post type singular name' ),
'add_new' => _x( 'Add New', 'testimonial' ),
'add_new_item' => __( 'Add New Testimonial' ),
'edit_item' => __( 'Edit Testimonial' ),
'new_item' => __( 'New Testimonial' ),
'all_items' => __( 'All Testimonials' ),
'view_item' => __( 'View Testimonial' ),
'search_items' => __( 'Search Testimonials' ),
'not_found' => __( 'No testimonials found' ),
'not_found_in_trash' => __( 'No testimonials found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Testimonial'
);
$args = array(
'labels' => $labels,
'description' => '',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor'),
'has_archive' => true,
);
register_post_type( 'testimonial', $args );
}
add_action( 'init', 'testimonials_register' );
Here is a good guide.
Related
I created custom post type "testimonials" with custom taxomies
it works well except of sinbgle page, when I click on permalink it shows "not found".
Here is my code for custom post types/taxonomies:
function testimonials_custom_post_type()
{
$labels = array(
'name' => _x('Testimonials', 'Post type general name'),
'singular_name' => _x('Testimonial', 'Post type singular name'),
'add_new' => _x('Add new Testimonial', 'Grower'),
'add_new_item' => __('Add new Testimonial'),
'edit_item' => __('Edit Testimonial'),
'new_item' => __('New Testimonial'),
'all_items' => __('All Testimonials'),
'view_item' => __('View Testimonial'),
'search_items' => __('Search Testimonials'),
'not_found' => __('No testimonials found'),
'not_found_in_trash' => __('No testimonials found in Trash'),
'parent_item_colon' => '',
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => false,
'query_var' => false,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'exclude_from_search' => false,
'supports' => array('title', 'editor', 'thumbnail' ),
'menu_position' => 6,
'menu_icon' => 'dashicons-id',
'taxonomies' => array( 'subjects' ),
);
register_post_type('testimonials', $args);
}
add_action('init', 'testimonials_custom_post_type');
/*register custom taxonomies for testimonials*/
add_action( 'init', 'create_testimonials_taxonomies', 0 );
function create_testimonials_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Subjects', 'subjects', 'textdomain' ),
'singular_name' => _x( 'Subject', 'subject', 'textdomain' ),
'search_items' => __( 'Search Subject', 'textdomain' ),
'all_items' => __( 'All Subjects', 'textdomain' ),
'edit_item' => __( 'Edit Subject', 'textdomain' ),
'update_item' => __( 'Update Subject', 'textdomain' ),
'add_new_item' => __( 'Add new Subject', 'textdomain' ),
'new_item_name' => __( 'New Category Subject', 'textdomain' ),
'menu_name' => __( 'Subject', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => false,
'query_var' => true,
'rewrite' => array( 'slug' => 'subjects' ),
);
register_taxonomy( 'subjects', array( 'testimonials' ), $args );
}
/*register custom taxonomies for testimonials*/
I tried to use flush_rewrite_rules( false ); and tried to update permalinks in wp admin panel it's still not working
For custom post type "testimonials" I created single-testimonials.php in my child theme
Thank you very much for any help!
To fix my bug I had to set 'publicly_queryable' to 'true'
and it works now ))
You could try to create taxonomy-subjects.php and refresh your permalinks after you try each time.
i created a custom taxonomy named cancers and a template file for this taxonomy named taxonomy-cancers.php but when i call this url:
http://localhost/cancers even http://localhost/?tax=cancers
wordpress shows page not found and doesn't use my taxonomy template.
this is my code for defining custom taxonomy:
add_action( 'init', 'create_cancers_taxonomy', 0 );
function create_cancers_taxonomy() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'سرطانها', 'taxonomy general name' ),
'singular_name' => _x( 'سرطان', 'taxonomy singular name' ),
'search_items' => __( 'جستجو در میان سرطانها' ),
'all_items' => __( 'تمامی سرطانها' ),
'parent_item' => __( 'دسته اصلی' ),
'parent_item_colon' => __( 'دسته اصلی:' ),
'edit_item' => __( 'ویرایش سرطان' ),
'update_item' => __( 'بروزرسانی سرطان' ),
'add_new_item' => __( 'افزودن سرطان' ),
'new_item_name' => __( 'عنوان سرطان' ),
'menu_name' => __( 'سرطانها' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
);
register_taxonomy( 'cancers', array('cancer', 'post'), $args );
}
I think you forgot to add rewrite parameter in arguments,
Use this arguments:--
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'cancers' ),
);
Note:- After changing this, please update your Permalinks from Settings --> Permalinks once...
here is my registerin taxonomy function to my posts.
add_action( 'init', 'marka_taxonomies', 0 );
function marka_taxonomies()
{
$labels = array(
'name' => _x( 'Markalar', 'taxonomy general name' ),
'singular_name' => _x( 'Marka', 'taxonomy singular name' ),
'search_items' => __( 'Markalarda Ara' ),
'all_items' => __( 'Tüm Markalar' ),
'parent_item' => __( 'Alt Marka Kategorisi' ),
'parent_item_colon' => __( 'Üst Marka Kategorisi:' ),
'edit_item' => __( 'Markayı Düzenle' ),
'update_item' => __( 'Markayı Güncelle' ),
'add_new_item' => __( 'Yeni Marka Ekle' ),
'new_item_name' => __( 'Yeni Marka Adı' ),
'menu_name' => __( 'Marka' )
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'marka' )
);
register_taxonomy( 'marka', array( 'post' ), $args );
}
Thnik everthing is normal but the taxonomy-marka.php doesnt work. I still get 404 Not found on title. And there is nothing on my taxonomy archive page. What should i do also where i am wrong ?
When I updated my permalinks option the taxonomy-marka.php page listed my posts.
Is there a way to create a custom post (e.g. movies) with custom fields (e.g. title, director, 0 or more actors, etc.) in WP and make it part of a theme? I'm guessing that something needs to be added in functions.php, but I'm not sure how to define a custom post type and assign it custom fields.
If this is too hard to do as part of a theme, I'm happy to just create it from the admin section. Suggestions?
I am using More Fields Plugin to add custom fields to my posts (Default post type or custom post type).
To create a custom post type (and custom taxonomy of course), I am adding in my functions.php a code that works ok and looks like this:
function post_type_aya_bi_aya() {
$labels = array(
'name' => _x( 'التفاسير', 'post type general name' ),
'singular_name' => _x( '', 'post type singular name' ),
'add_new' => _x( 'أضف تفسير جديد', 'book' ),
'add_new_item' => __( 'أضف تفسير جديد' ),
'edit_item' => __( 'تحرير التفسير' ),
'new_item' => __( 'تفسير جديد' ),
'all_items' => __( 'كافة التفاسير' ),
'view_item' => __( 'مشاهدة التفسير' ),
'search_items' => __( 'بحث في التفاسير' ),
'not_found' => __( 'لا يوجد أي تفسير' ),
'not_found_in_trash' => __( 'لا يوجد أي تفسير في سلة المهملات' ),
'parent_item_colon' => '',
'menu_name' => 'التفاسير'
);
$args = array(
'labels' => $labels,
'description' => 'يحنوي على معطيات "آية بآية و التفاسير المرفوعة"',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
'rewrite' => array('slug' => 'aya-bi-aya'),
);
register_post_type( 'aya-bi-aya', $args );
}
add_action( 'init', 'post_type_aya_bi_aya' );
function taxonomy_aya_bi_aya() {
$labels = array(
'name' => _x( 'السور', 'taxonomy general name' ),
'singular_name' => _x( 'السورة', 'taxonomy singular name' ),
'search_items' => __( 'بحث في السور' ),
'all_items' => __( 'كافة السور' ),
'parent_item' => __( '' ),
'parent_item_colon' => __( '' ),
'edit_item' => __( 'تحرير السورة' ),
'update_item' => __( 'تحديث السورة' ),
'add_new_item' => __( 'أضف سورة' ),
'new_item_name' => __( 'سورة جديدة' ),
'menu_name' => __( 'السور' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => TRUE,
'show_ui' => TRUE,
'query_var' => 'sowar',
);
register_taxonomy( 'sowar', 'aya-bi-aya', $args );
}
add_action( 'init', 'taxonomy_aya_bi_aya', 0 );
I am here to explain more. Good luck.
You could also use the excellent Advanced Custom Fields plugin
This provides a quick admin interface for creating fields per post type/page template/page type etc and has a very simple API for template use
<?php the_field('my_custom_field'); ?>
But the part I think you'll be interested in is you can export your field groups as php and then include them as a part of your theme
I have created a custom post type called 'Case Studies' ... What i want to be able to do is display the box with the categories of normal posts inside the Case Studies 'add case study page', so i can choose posts that are associated with the case study im making.
All this done in the admin section.
Any suggestions would be great.
Cheers,
In your register_post_type function's arguments array parameter, add the following key and array value to add the regular categories in the custom post type add/edit page:
'taxonomies' => array("category")
For example:
register_post_type( 'slider',
array(
'labels' => array(
'name' => _x( 'Slider', 'post type general name' ),
'singular_name' => _x( 'Slider', 'post type singular name' ),
'add_new' => _x( 'Add New', 'Slider' ),
'add_new_item' => __( 'Add New Slider' ),
'edit_item' => __( 'Edit Slider' ),
'new_item' => __( 'New Slider' ),
'view_item' => __( 'View Slider' ),
'search_items' => __( 'Search Slider' ),
'not_found' => __( 'No Slider Items found' ),
'not_found_in_trash' => __( 'No Slider Items found in Trash' ),
'parent_item_colon' => ''
),
'public' => true,
'exclude_from_search' => true,
'show_in_nav_menus' => false,
'supports' => array('title','thumbnail'),
'taxonomies' => array("category"), /* This is what you are looking for and the value has to be an array. */
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
)
);
For details see the Wordpress register_post_type function documentation.
try this or check this tutorial
function Book() {
$labels = array(
'name' => 'Books',
'singular_name' => 'Book',
'add_new' => 'Add New',
'add_new_item' => 'Add New Book',
'edit_item' => 'Edit Book',
'new_item' => 'New Book',
'all_items' => 'All Books',
'view_item' => 'View Book',
'search_items' => 'Search Books',
'not_found' => 'No books found',
'not_found_in_trash' => 'No books found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Books'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
'taxonomies' => array('category', 'post_tag')
);
register_post_type( 'book', $args );
}
add_action( 'init', 'book' );
Ohh, interesting. Not that i have done this before but i just thinking of it makes it exciting. I do things a little dirty so forgive me.
I would a special tag to my custom post. Then add a second loop to my custom post that would query posts based on that tag.