简体   繁体   中英

Custom post type - archive

I have custom post type for wordpress.

http://localhost/wordpress/photos/
http://localhost/wordpress/photos/my-photo

Why these url return 404 not found ?

How can I make a archive file for this ? like :
archive-photos.php ( I have this in directory, but ...)

function register_gallery(){
    register_post_type( 'photos', array(
        'public'            => true,
        'menu_position'     => 10,
        'menu_icon'     => 'dashicons-format-gallery',
        'supports'      => array( 'title', 'editor', 'thumbnail'),
        'has_archive'       => true,

    ));
}

add_action('init','register_gallery');

You have to use,

//Need to call when custom post type is being used
flush_rewrite_rules();

also try, settings->permalinks save all settings.

more info visit, https://codex.wordpress.org/Function_Reference/register_post_type

I found another solution, it can be done on 404 page when navigating through archives on custom post type(CPT):

/**
 * My cpt is storytitle and slug for cpt is story-title
 * 
**/
function generate_cpt_rewrite_rules( $wp_rewrite ) {

$event_rules = array(
'story-title/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$' => 'index.php?post_type=storyTitle&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
'story-title/([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?post_type=storyTitle&year=$matches[1]&monthnum=$matches[2]',
'story-title/([0-9]{4})/?$' => 'index.php?post_type=storyTitle&year=$matches[1]'
);

$wp_rewrite->rules = $event_rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'generate_cpt_rewrite_rules' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM