WordPress Custom Post Type Hierarchy - 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.

Related

Custom post template makes my wordpress crash

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.

not found message for wordpress custom taxonomy

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...

Custom post type title of each author in his own post

The users registered in wordpress have one post only, and i created custom_post_type named "ofertas". The users can create one oferta only. With this code show the oferta title on index.php, the problem is show the oferta title in ALL POSTS. I want only show his own oferta title (if they have).
<?php
// First loop
while ( have_posts() ): the_post();
author_id = get_query_var('author');
$ofertas = array();
// Second Loop
$i = 0;
$args = array(
'author' => $author_id,
'post_type' => 'ofertas'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); // check if it has offers, if it has, loop over the offers
$ofertas[$i]['title'] = get_the_title(); // or anything you need
$i++;
endwhile; // close the loop
else: // if it has no post
continue; // we don't display the post
endif;
wp_reset_postdata();
?>
<div class="post">
<?php
the_title(); // or anything you need
foreach ($ofertas as $oferta):
echo $oferta['title']; // we display the title of the offer
endforeach;
?>
</div>
<?php endwhile; ?>
I created the custom_post_type with this code
add_action('init', 'crear_tipo_ofertas', 0);
function crear_tipo_ofertas() {
$labels = array(
'name' => _x( 'Ofertas', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Oferta', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Ofertas', 'text_domain' ),
'parent_item_colon' => __( 'Oferta Padre', 'text_domain' ),
'all_items' => __( 'Ofertas', 'text_domain' ),
'view_item' => __( 'Ver Oferta', 'text_domain' ),
'add_new_item' => __( 'Añadir Oferta Nueva', 'text_domain' ),
'add_new' => __( 'Añadir', 'text_domain' ),
'edit_item' => __( 'Editar Oferta', 'text_domain' ),
'update_item' => __( 'Actualizar', 'text_domain' ),
'search_items' => __( 'Buscar Ofertas', 'text_domain' ),
'not_found' => __( 'Ofertas no encontradas', 'text_domain' ),
'not_found_in_trash' => __( 'Ofertas no encontradas en Papelera', 'text_domain' ),
);
$rewrite = array(
'slug' => 'oferta',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'oferta', 'text_domain' ),
'description' => __( 'Ofertas Spas y Balnearios', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields'),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => site_url().'/wp-content/plugins/my_plugin/images/logo.png',
'can_export' => true,
'has_archive' => 'ofertas',
'exclude_from_search' => false,
'query_var' => 'ofertas',
'rewrite' => $rewrite,
'capability_type' => 'post',
);
register_post_type('ofertas', $args);
}
add_action('init', 'crear_tipo_ofertas', 0);
If I understand you correctly, you want to show the current logged in users posts in the ofertas CPT? Try changing this line:
author_id = get_query_var('author');
To this:
$author_id = get_current_user_id();
get_query_var('author) is used to retrieve a public query variable which is most likely not set on the home page.

Wordpress pagination for custom post type not working

I have created custom post type with custom taxanomy and displaying them on theme's index.php. when i use wordpress pagination, and it goes on page 2, nothing showing there and it goes on Page Not Found!.
following are codes
add_action( 'init', 'register_success_stories' );
function register_success_stories(){
register_post_type( 'sajid-photos', array(
'label' => 'My Photos',
'singular_label' => 'My Photo',
'description' => 'Manage phots.',
'public' => TRUE,
'publicly_queryable' => TRUE,
'show_ui' => TRUE,
'query_var' => TRUE,
'rewrite' => TRUE,
'capability_type' => 'post',
'hierarchical' => FALSE,
'menu_position' => NULL,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'revisions'),
'menu_position' => 5,
'rewrite' => array(
'slug' => 'sajid-photo-work',
'with_front' => FALSE,
),
'labels' => array(
'name' => __( 'My Photos' ),
'singular_name' => __( 'My Photos' ),
'add_new' => __( 'Add New Photo' ),
'add_new_item' => __( 'Add New Photo' ),
'edit' => __( 'Edit Photo' ),
'edit_item' => __( 'Edit Photo' ),
'new_item' => __( 'New Photo' ),
'view' => __( 'View Photo' ),
'view_item' => __( 'View Photo' ),
'search_items' => __( 'Search Photos' ),
'not_found' => __( 'No Photo Found' ),
'not_found_in_trash' => __( 'No Photo found in Trash' ),
'parent' => __( 'Parent Photo' ),
)
));
flush_rewrite_rules( false );
}
register_taxonomy('sajid-album', 'sajid-photos', array(
'label' => 'Albums',
'singular_label' => 'Album',
'public' => TRUE,
'show_tagcloud' => FALSE,
'hierarchical' => TRUE,
'query_var' => TRUE,
'rewrite' => array('slug' => 'sajid-album' )
));
Following is code to show specific texanomy's post on home page.
<?php
wp_reset_query();
global $post;
query_posts( array( 'post_type' => 'sajid-photos', 'sajid-album' => 'home-page-photos','paged' => $paged, 'posts_per_page' => 1 , 'paged' => get_query_var('paged')) );
if ( have_posts() ){
while ( have_posts() ) : the_post();
unset($thumb_big);
$thumb_big = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
$thumb_big_url = $thumb_big['0'];
?>
<img src="<?php echo $cfs->get('upload_thumbnail'); ?>" alt="<?php the_title(); ?>">
<?php
endwhile;
} // end of if ( have_posts() )
theme_paginate();
wp_reset_query();
?>
Moreover its Terms urls also not working, when i click on term, it goes on page not found.
please help me to solve this problem.
thank you very much.
website URL is http://sajidz.w3made.com/
For starters, ideally you wouldn't paginate your homepage in a wordpress site. But per the documentation you would need to be looking for the get_query_var('page') variable instead of "paged" if you want to get the current page on a static front page. You are using theme_paginate(); it appears for pagination linking and I am not sure how it is building the links, so that might be the first place you start.

custom post types wordpress

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.

Resources