作为CPT子项发布的重写规则

时间:2018-02-23 作者:Emilien Schneider

我有一些自定义的帖子类型,URL就是这样显示的,并且工作正常:

http://www.mywebsite.com/my-cpt-name


自定义职位类型的注册码为:

$rewrite = array(\'slug\' => \'my-cpt-type\', \'with_front\' => false);
$download_args = array(
    \'labels\'            => $labels,
    \'public\'            => true,
    \'publicly_queryable\'=> true,
    \'show_ui\'           => true,
    \'show_in_menu\'      => true,
    \'query_var\'         => true,
    \'rewrite\'           => $rewrite,
    \'capability_type\'   => \'product\',
    \'map_meta_cap\'      => true,
    \'has_archive\'       => $archives,
    \'hierarchical\'      => false,
    \'supports\'          => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'revisions\', \'author\' ),
);
register_post_type( \'my-cpt-type\', $download_args );
我使用post\\u type\\u link进行过滤,使其显示为之前写入的链接。

function custom_type_links( $post_link, $post = 0 ) {
    if ( $post->post_type === \'my-cpt-type\' ) {
        return home_url( $post->post_name . \'/\' );
    }
    return $post_link;
}
对于每个CPT,我都有当前使用此类URL访问的新闻:

1 个回复
SO网友:WebElaine

除设置外has_archive 您需要设置rewrite 定义CPT时。我替换了$archives 因为您的代码段没有定义它。

$rewrite = array(\'slug\' => \'my-cpt-type\', \'with_front\' => false);
$download_args = array(
    \'labels\'            => $labels,
    \'public\'            => true,
    \'publicly_queryable\'=> true,
    \'show_ui\'           => true,
    \'show_in_menu\'      => true,
    \'query_var\'         => true,
    \'rewrite\'           => $rewrite,
    \'capability_type\'   => \'product\',
    \'map_meta_cap\'      => true,
    // Set has_archive to the prefix you want, so the archive is in the right place
    \'has_archive\'       => \'my-cpt-name/news\',
    // Also set rewrite to the prefix you want, which will affect individual posts.
    \'rewrite\'      => \'my-cpt-name/news\',
    \'hierarchical\'      => false,
    \'supports\'          => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'revisions\', \'author\' ),
);
register_post_type( \'my-cpt-type\', $download_args );
您需要:

取消注册post类型(“my-cpt-type”);取消注册CPT,否则WP将不接受您的更改。取消注册时不会丢失任何内容,它只会清除永久链接。

使用上述设置重新注册,然后访问设置>永久链接页面刷新重写规则。然后,您应该拥有所需的结构-在尝试访问新URL之前,请确保清除浏览器缓存。

结束

相关推荐