【问题标题】:Custom post type - archive自定义帖子类型 - 存档
【发布时间】:2015-10-06 12:21:46
【问题描述】:

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

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

为什么这些 url 返回 404 not found ?

如何为此制作存档文件? 喜欢 :
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');

【问题讨论】:

  • 我建议,参考woocommerce,产品是自定义帖子。我已经为自定义产品类型做过。
  • 我没有使用任何插件!
  • 如何在设置 > 阅读中设置永久链接?
  • 我没有让你使用插件,我只是让你参考,你可以在你的主题文件夹中使用模板。即使你知道,你可以在你的主题文件夹中使用 woocommerce 模板,就像为您的帖子类型制作自定义模板并在您的主题文件夹中使用它。
  • 永久链接已解决

标签: php wordpress


【解决方案1】:

你必须使用,

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

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

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

【讨论】:

  • 访问永久链接页面与使用flush_rewrite_rules()相同
  • @JayDeepNimavat 为什么这样? init 钩子非常适合注册帖子类型。来自WP Codex:“register_post_type 只能通过 'init' 操作调用”
【解决方案2】:

我找到了另一种解决方案,可以在浏览自定义帖子类型(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' );

【讨论】:

    猜你喜欢
    • 2014-02-19
    • 1970-01-01
    • 2014-06-16
    • 2017-11-21
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    相关资源
    最近更新 更多