自定义文章类型 - 档案

自定义文章类型 - 档案

问题描述:

我有自定义文章类型的WordPress。自定义文章类型 - 档案

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

为什么这些url返回404找不到?

如何为此创建档案文件? 像:
归档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'); 
+0

我建议,采取从参考woocommerce,产品是自定义posts.I已完成它的自定义产品类型。 –

+1

我没有使用任何插件! –

+1

如何在设置>阅读中设置固定链接? – vard

你必须使用,

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

也试试,设置 - >永久链接保存所有设置。

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

+0

访问永久链接页面是相同的使用'flush_rewrite_rules()' – vard

+0

谢谢我改变固定链接 –

+0

@JayDeepNimavat为什么呢? 'init'钩子对于寄存器帖子类型来说非常好。从[WP Codex](https://codex.wordpress.org/Function_Reference/register_post_type):“register_post_type只能通过'init'操作调用” – vard

我发现了另一个解决方案,它可以在404页通过自定义职位类型(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');