Php中的这段CPT代码如何添加隐藏的“Products”类别?

时间:2020-05-12 作者:randy

这是我修改的代码https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/

/*
* Creating a function to create our CPT
* just drop this in functions.php inside your theme file basically...
* You replaced all \'twentytwenty\' with \'twentyseventeen\'
*/

function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        \'name\'                => _x( \'Products\', \'Post Type General Name\', \'twentyseventeen\' ),
        \'singular_name\'       => _x( \'Product\', \'Post Type Singular Name\', \'twentyseventeen\' ),
        \'menu_name\'           => __( \'Products\', \'twentyseventeen\' ),
        \'parent_item_colon\'   => __( \'Parent Product\', \'twentyseventeen\' ),
        \'all_items\'           => __( \'All Products\', \'twentyseventeen\' ),
        \'view_item\'           => __( \'View Product\', \'twentyseventeen\' ),
        \'add_new_item\'        => __( \'Add New Product\', \'twentyseventeen\' ),
        \'add_new\'             => __( \'Add New\', \'twentyseventeen\' ),
        \'edit_item\'           => __( \'Edit Product\', \'twentyseventeen\' ),
        \'update_item\'         => __( \'Update Product\', \'twentyseventeen\' ),
        \'search_items\'        => __( \'Search Product\', \'twentyseventeen\' ),
        \'not_found\'           => __( \'Not Found\', \'twentyseventeen\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'twentyseventeen\' ),
    );

// Set other options for Custom Post Type

    $args = array(
        //\'label\'               => __( \'products\', \'twentyseventeen\' ),
        \'description\'         => __( \'Products and reviews\', \'twentyseventeen\' ),
        \'labels\'              => $labels,
        // Features this CPT supports in Post Editor
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        \'taxonomies\'          => array( \'category\', \'tag\' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        \'hierarchical\'        => false,
        \'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\',
        \'show_in_rest\' => true,

    );

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

}

/* Hook into the \'init\' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( \'init\', \'custom_post_type\', 0 );

我创建了一个名为“shop”的类别,我希望这些CPT“产品”显示在我的“shop”类别中,以便您访问该域时。你会看到他们的列表。

我在分类法中添加了“类别”功能,并且我可以在创建新产品时选择商店类别,但永久链接始终是唯一的域。com/产品/产品名称

每当我创建新的产品自定义帖子时,我都会填写描述、添加自定义字段、标题、选择店铺类别。。。在我按publish之前一切都很好。。。然后编辑器中会出现一个“permalink”下拉部分,我被告知可以在域中看到该产品。com/产品/产品名称。。。当我从未选择产品类别时。。。而且产品类别甚至不存在/从未存在!!!

有什么好处?

如果我访问域。商店里什么也看不见。

如果我访问域。com/shop/product name我被重定向到域。com/products/product name(顺便说一下,它显示得很好)。

你知道这个被称为“产品”的幽灵类别是从哪里来的吗?

为什么编辑不尊重我将产品列入“商店”类别的愿望?而是将其放在自己的“产品”类别中(在wordpress管理仪表板中甚至不存在/从未创建)。

我对任何php代码所做的唯一编辑就是在我的主题函数中添加单个函数。php文件。

我甚至更新/刷新了permalinks设置,方法是在仪表板的自定义permalinks部分重新保存。

非常感谢。

编辑:我想我可能错过了“重写”参数?如下所述:https://developer.wordpress.org/reference/functions/register_post_type/

\'rewrite\'
(bool|array) Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be passed with any of these keys:

1 个回复
SO网友:Jacob Peattie

这是自定义帖子类型的正常行为。除页面和帖子以外的任何帖子类型的URL中都有帖子类型名称。因此,对于此URL:

https://example.com/products/product-name/
“产品”是岗位类型。不是类别。同样,注册帖子类型时,如果has_archive 设置为true, 所有产品将在以下位置自动查看:

https://example.com/products/
Therewritehas_archive 属性可用于更改这些的每个段塞所使用的段塞。例如,如果设置:

\'has_archive\' => \'shop\',
\'rewrite\'.    => [
    \'slug\'       => \'product\',
    \'with_front\' => false,
],
然后,产品存档URL将为:

https://example.com/shop/
单个产品的URL为:

https://example.com/product/product-name/
在这两种情况下,您都不需要“产品”或“商店”类别。

相关推荐