WordPress 自定义帖子类型、自定义分类法、URL 和主题

发布于 2024-11-17 12:24:14 字数 565 浏览 4 评论 0原文

我正在尝试建立一个 WordPress 3 网站,在其中我通过一个插件创建了一个名为“产品”的自定义帖子类型。除了这个自定义帖子类型之外,我还创建了一个自定义分类法,以便可以将产品放置在一个类别中,而不会干扰博客类别。这一切通过管理系统都可以很好地工作。到目前为止,一切都很好。

当我想要访问产品和类别时,问题就出现了。理想情况下,我有以下 URL 结构:

/products - 显示包含所有类别的页面。

/products/category - 显示指定给定“类别”的所有产品。

/products/product - 显示单个产品。

在过去的几天里,我一直在阅读各种来源,我正在寻找的信息似乎非常分散,这让我对这个问题非常困惑,并且不知道我想要的东西在上下文中是否真的可能。 WordPress。

接下来我非常困惑的是上面示例中所示的 URL 如何与主题引擎结合起来。我不完全理解应该创建哪些模板或者应该绑定哪些挂钩才能完成这项工作。

所以,基本问题是;如何使我的自定义帖子类型和自定义分类法映射到正确的主题模板?

I'm trying to set up a Wordpress 3 website in which I have, through a plugin, created a custom post type called a "product". Along with this custom post type I've created a custom taxonomy so the products can be placed in a category without interfering with the blog categories. This is all working great through the Admin system. So far so good.

The problem arises when I want to access the products and the categories. Ideally I have the following URL structure:

/products - Shows a page with all the categories.

/products/category - Shows all the products assigned with the given 'category`.

/products/product - Shows a single product.

I've been reading through various sources over the past few days and the information I'm looking for seems to be very fragmented making me very much confused about the problem and not knowing if what I want is actually possible or not within the context of Wordpress.

The next thing I'm very confused about is how the URL's illustrated in the example above tie in with the theme engine. I do not fully comprehend which templates I should create or which hooks I should tie into to make this work.

So, the basic question is; How can I make my custom post types and custom taxonomies map to the correct theme template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2024-11-24 12:24:14

我知道这是一个老问题,但这可能对其他人有帮助......

要完成您所追求的目标,您需要使用 register_post_typeregister_taxonomy WordPress 功能。

您的functions.php 文件中需要类似的内容

// Register Custom Post Type
function products() {
    $labels = array(
        'name'                => 'Products',
        'singular_name'       => 'Product',
        'menu_name'           => 'Product',
        'parent_item_colon'   => 'Parent Product:',
        'all_items'           => 'All Products',
        'view_item'           => 'View Product',
        'add_new_item'        => 'Add New Product',
        'add_new'             => 'New Product',
        'edit_item'           => 'Edit Product',
        'update_item'         => 'Update Product',
        'search_items'        => 'Search products',
        'not_found'           => 'No products found',
        'not_found_in_trash'  => 'No products found in Trash'
    );
    $args = array(
        'label'               => 'product',
        'description'         => 'Product information pages',
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', ),
        'taxonomies'          => array( 'category' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'           => '',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post'
    );
    register_post_type( 'product', $args );
}
// Hook into the 'init' action
add_action( 'init', 'products', 0 );

// Register Custom Taxonomy
function product-category()  {
    $labels = array(
        'name'                       => 'Product Category',
        'singular_name'              => 'Product Categories',
        'menu_name'                  => 'Product Category',
        'all_items'                  => 'All Product Categories',
        'parent_item'                => 'Parent Product Category',
        'parent_item_colon'          => 'Parent Product Category:',
        'new_item_name'              => 'New Product Category Name',
        'add_new_item'               => 'Add New Product Category',
        'edit_item'                  => 'Edit Product Category',
        'update_item'                => 'Update Product Category',
        'separate_items_with_commas' => 'Separate product categories with commas',
        'search_items'               => 'Search product categories',
        'add_or_remove_items'        => 'Add or remove product categories',
        'choose_from_most_used'      => 'Choose from the most used product categories',
    );
    $rewrite = array(
        'slug'                       => 'product-category',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'query_var'                  => 'product-category',
        'rewrite'                    => $rewrite,
    );
    register_taxonomy( 'product-category', 'product', $args );

}
// Hook into the 'init' action
add_action( 'init', 'product-category', 0 );

您可以使用自定义帖子生成器,例如 http:// generatewp.com/post-type/ 让您在完成所有选项时变得更轻松。

注册自定义帖子类型和分类法后,添加此代码或对 URL 结构进行任何修改后刷新 DNS 规则非常重要。通过访问 WordPress 管理设置 -> 来执行此操作永久链接页面,它会自动刷新 DNS 规则。

编辑主题文件夹中的以下文件以控制显示的模板:

  • archive-product.phptaxonomy
  • -product-category.php
  • single-product.php

I know this is an old question but this might help someone else…

To accomplish what you're after you'll need to use the register_post_type and register_taxonomy WordPress functions.

You'll need something similar to this in your functions.php file

// Register Custom Post Type
function products() {
    $labels = array(
        'name'                => 'Products',
        'singular_name'       => 'Product',
        'menu_name'           => 'Product',
        'parent_item_colon'   => 'Parent Product:',
        'all_items'           => 'All Products',
        'view_item'           => 'View Product',
        'add_new_item'        => 'Add New Product',
        'add_new'             => 'New Product',
        'edit_item'           => 'Edit Product',
        'update_item'         => 'Update Product',
        'search_items'        => 'Search products',
        'not_found'           => 'No products found',
        'not_found_in_trash'  => 'No products found in Trash'
    );
    $args = array(
        'label'               => 'product',
        'description'         => 'Product information pages',
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', ),
        'taxonomies'          => array( 'category' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'           => '',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post'
    );
    register_post_type( 'product', $args );
}
// Hook into the 'init' action
add_action( 'init', 'products', 0 );

// Register Custom Taxonomy
function product-category()  {
    $labels = array(
        'name'                       => 'Product Category',
        'singular_name'              => 'Product Categories',
        'menu_name'                  => 'Product Category',
        'all_items'                  => 'All Product Categories',
        'parent_item'                => 'Parent Product Category',
        'parent_item_colon'          => 'Parent Product Category:',
        'new_item_name'              => 'New Product Category Name',
        'add_new_item'               => 'Add New Product Category',
        'edit_item'                  => 'Edit Product Category',
        'update_item'                => 'Update Product Category',
        'separate_items_with_commas' => 'Separate product categories with commas',
        'search_items'               => 'Search product categories',
        'add_or_remove_items'        => 'Add or remove product categories',
        'choose_from_most_used'      => 'Choose from the most used product categories',
    );
    $rewrite = array(
        'slug'                       => 'product-category',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'query_var'                  => 'product-category',
        'rewrite'                    => $rewrite,
    );
    register_taxonomy( 'product-category', 'product', $args );

}
// Hook into the 'init' action
add_action( 'init', 'product-category', 0 );

You can use a custom post generator like http://generatewp.com/post-type/ to make your life easier when working through all the options.

It's important once you register your custom post types and taxonomies to flush the DNS rules once you add this code or make any amendments to the URL structure. Do this by visiting the WordPress admin Settings -> Permalinks page and it'll automatically flush the DNS rules.

Edit the following files in your theme folder to control the template displayed:

  • archive-product.php
  • taxonomy-product-category.php
  • single-product.php
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文