自定义帖子类型-如何使其成为另一个页面的子url?

时间:2020-07-13 作者:ScottM

我已经创建了一个自定义帖子类型;推荐信工作正常,但我希望url为“;领域com/结果/证明”;而不是;领域com/证明”;

我是否只需要更改创建自定义帖子类型的函数中的某些内容?或者我还需要做些什么?

谢谢Scott

2 个回复
最合适的回答,由SO网友:WebElaine 整理而成

注册CPT时,请使用rewrite 参数添加前缀。

例如,如果您的CPT当前是这样注册的:

<?php
function wpse_create_cpt_testimonial() {
    $args = array(
        \'public\' => true,
        \'label\'  => __( \'Testimonials\', \'textdomain\' ),
        // ... you probably have other arguments as well
    );
    register_post_type( \'testimonial\', $args );
}
add_action( \'init\', \'wpse_create_cpt_testimonial\' );
?>
您可以添加rewrite 像这样:

<?php
function wpse_create_cpt_testimonial() {
    $args = array(
        \'public\' => true,
        \'label\'  => __( \'Testimonials\', \'textdomain\' ),
        // Here\'s the part where you\'re changing the URL
        \'rewrite\' => array(\'slug\' => \'results/testimonials\'),
        // If you want that URL to be an archive, add this line too
        \'has_archive\' => \'results/testimonials\',
        // ... keep your other arguments as well
    );
    register_post_type( \'testimonial\', $args );
}
add_action( \'init\', \'wpse_create_cpt_testimonial\' );
?>
如果这不能立即改变情况,您可能需要运行unregister_post_type(\'testimonial\') 在重新注册之前,和/或访问Permalinks页面刷新重写规则。

SO网友:ScottM

再花点时间四处搜索,我找到了答案。。。给你,以防对其他人有帮助。

在创建自定义帖子类型的函数中,我添加了以下内容:

   $rewrite = array(
    \'slug\'                  => \'results/testimonials\',
    \'with_front\'            => true,
    \'pages\'                 => true,
    \'feeds\'                 => true,
);
因此,完整的自定义帖子类型代码现在是:

    /*
* Creating a function to create our CPT
*/
 
static function testimonial_post_type() {
 
// Set UI labels for Custom Post Type
$labels = array(
    \'name\'                => _x( \'Testimonials\', \'Post Type General Name\'),
    \'singular_name\'       => _x( \'Testimonial\', \'Post Type Singular Name\'),
    \'menu_name\'           => __( \'Testimonials\'),
    \'parent_item_colon\'   => __( \'Parent Testimonial\'),
    \'all_items\'           => __( \'All Testimonials\'),
    \'view_item\'           => __( \'View Testimonial\'),
    \'add_new_item\'        => __( \'Add New Testimonial\'),
    \'add_new\'             => __( \'Add New\'),
    \'edit_item\'           => __( \'Edit Testimonial\'),
    \'update_item\'         => __( \'Update Testimonial\'),
    \'search_items\'        => __( \'Search Testimonial\'),
    \'not_found\'           => __( \'Not Found\'),
    \'not_found_in_trash\'  => __( \'Not found in Trash\'),
);

$rewrite = array(
    \'slug\'                  => \'results/testimonials\',
    \'with_front\'            => true,
    \'pages\'                 => true,
    \'feeds\'                 => true,
);

// Set other options for Custom Post Type
$args = array(
    \'label\'               => __( \'testimonials\'),
    \'description\'         => __( \'Client testimonials\'),
    \'labels\'              => $labels,
    // Features this CPT supports in Post Editor
    \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\',\'page-attributes\' ),
    // You can associate this CPT with a taxonomy or custom taxonomy. 
    \'taxonomies\'          => array( \'feature_on\',\'service\',\'industry\' ),
    /* A hierarchical CPT is like Pages and can have
    * Parent and child items. A non-hierarchical CPT
    * is like Posts.
    */ 
    \'hierarchical\'        => true,
    \'public\'              => true,
    \'show_ui\'             => true,
    \'show_in_menu\'        => true,
    \'show_in_nav_menus\'   => true,
    \'show_in_admin_bar\'   => true,
    \'menu_position\'       => 5,
    \'can_export\'          => true,
    \'has_archive\'         => true,
    \'exclude_from_search\' => false,
    \'publicly_queryable\'  => true,
    \'capability_type\'     => \'post\',
    \'menu_icon\'         => "dashicons-thumbs-up",
    \'rewrite\'   => $rewrite,
    "rest_base" => "Testimonial",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    \'show_in_rest\' => true,
    "query_var" => true,

);
 
// Registering your Custom Post Type
register_post_type( \'testimonials\', $args );

}
我希望这能帮助别人!

相关推荐

Problem with permalinks

我已经更改了类别的基本名称,现在它是“博客”,工作正常。但当我通过/blog/%category%/%postname%/更改结构时。显示404。如果我删除结构中的blog,它会再次工作,但我想使用blog word。问题出在哪里?非常感谢。