【问题标题】:wordpress custom post type slugs and rewritewordpress 自定义帖子类型 slugs 和重写
【发布时间】:2021-03-05 17:52:42
【问题描述】:

我的 wordpress 网站中有 2 种自定义帖子类型,

 // Our custom post type function
function create_posttype() {
 
    register_post_type( 'properties',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Properties' ),
                'singular_name' => __( 'Property' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'properties'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

 // Our custom post type function
function create_team_posttype() {
 
    register_post_type( 'team',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Team Members' ),
                'singular_name' => __( 'Team Member' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'our-team'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_team_posttype' );

当我转到mydomain.com/properties 时,我看到了我的archive-properties.php 页面模板,我看到了正确的模板和内容。我有一个名为archive-team.php 的团队存档teamplate,但是当我转到它的URl mydomain.com/our-team 我看到archive-properties.php 模板我做错了什么吗?

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    您需要为存档 slug 传递一个参数。目前,您只为单个自定义帖子传递 slug。因此,您的自定义帖子现在位于 /our-team/some-team-slug

    如果你想修改你的存档,你需要使用:

    $args = array(
      'has_archive' => 'our-team',
    );
    

    在法典中它被解释为follows

    'has_archive' (bool|string) 是否应该有 post 类型的存档,或者如果是字符串,则使用存档 slug。如果启用了 $rewrite,将生成正确的重写规则。默认为假。

    您可能需要重写您的永久链接或取消注册并重新注册您的自定义帖子类型,以确保更改被采纳。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2013-08-03
      • 2018-07-03
      • 1970-01-01
      相关资源
      最近更新 更多