自定义帖子类型 - 存档

时间:2015-10-06 12:21:47

标签: php wordpress

我有wordpress的自定义帖子类型。

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

为什么这些网址没有找到404?

如何为此制作存档文件? 喜欢 :
archive-photos.php(我在目录中有这个,但是......)

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');

2 个答案:

答案 0 :(得分:1)

你必须使用,

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

还可以尝试,设置 - >永久链接保存所有设置。

更多信息访问,https://codex.wordpress.org/Function_Reference/register_post_type

答案 1 :(得分:0)

我找到了另一种解决方案,可以在自定义帖子类型(CPT)上浏览档案时在404页面上完成:

/**
 * 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' );