WordPress for personalized mail archive - <post type> .php does not work

New to WordPress, I tried to create an archive page for a personalized post type, and when I click on the link to localhost/websitename/wordpress/archive-event.php , I get 404. Here is my code for registering a post type:

 add_action('init', 'event_register'); function event_register() { $labels = array( 'name' => _x('Events', 'post type general name'), 'singular_name' => _x('Event', 'post type singular name'), 'add_new' => _x('Add New', 'event'), 'add_new_item' => __('Add New Event'), 'edit_item' => __('Edit Event'), 'new_item' => __('New Event'), 'view_item' => __('View Event'), 'search_items' => __('Search Events'), 'not_found' => __('Nothing found'), 'not_found_in_trash' => __('Nothing found in Trash'), 'parent_item_colon' => '' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => array('slug' => 'event'), 'has_archive' => 'event', 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title','editor','thumbnail'), ); register_post_type( 'event' , $args ); } 

I tried having rewrite => true , has_archive => true , dropping the rewrite rules and even registering a taxonomy for the message type. single-event.php working fine. Now what?

+6
source share
3 answers

Well, I had a similar problem with yours, and now I believe that I have decided.

I'm not sure my code will accurately reflect what you need to do, but it should be easy enough to adapt. Note the rewrite rules for the archive URL.

functions.php

 function my_custom_posts() { $labels = array( 'name' => _x( 'Events', 'post type general name' ), 'singular_name' => _x( 'Event', 'post type singular name' ), 'add_new' => _x( 'Add New', 'event' ), 'add_new_item' => __( 'Add New Event' ), 'edit_item' => __( 'Edit Event' ), 'new_item' => __( 'New Event' ), 'all_items' => __( 'All Events' ), 'view_item' => __( 'View Event' ), 'search_items' => __( 'Search Events' ), 'not_found' => __( 'No events found' ), 'not_found_in_trash' => __( 'No events found in the Trash' ), 'parent_item_colon' => '', 'menu_name' => 'Events' ); $args = array( 'labels' => $labels, 'description' => 'Holds our events and event specific data', 'public' => true, 'menu_position' => 5, 'supports' => array( 'title' ), 'has_archive' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => array('slug' => 'events', 'with_front' => true), 'capability_type' => 'post', 'hierarchical' => false, ); register_post_type( 'event', $args ); //flush_rewrite_rules(); } add_action( 'init', 'my_custom_posts' ); add_action('init', 'event_archive_rewrite'); function event_archive_rewrite(){ add_rewrite_rule('^events/([0-9]{4})/([0-9]{2})/?','index.php?post_type=event&year=$matches[1]&monthnum=$matches[2]','top'); add_rewrite_rule('^events/([0-9]{4})/?','index.php?post_type=event&year=$matches[1]','top'); } 

Now it allows me to access the following events:

example.com/events/2013/07/01

Remember to have a proper archive template, for example

 archive-event.php 

In addition, if you simply register your message type, you need to reset the permalink cache. Do this by going to your WordPress admin and save the permalink structure again.

Hope this helps someone!

Thanks, Mikey.

+7
source

It looks like in the example of your url you are trying to get to the template file name itself.

But if you determined that slug will be event , you should just visit localhost / websitename / wordpress / event

0
source

Not sure if this will answer your question, but have you reset your permalink structure?

If you just added a custom message type, you need to go to the Permalinks page and click "Save."

Hope this helps!

0
source

Source: https://habr.com/ru/post/946712/


All Articles