显示具有分类法的自定义帖子类型
我的 WordPress 项目中有一个自定义帖子类型的产品。 CPT 有几个自定义字段和两个可以附加到它的分类法。我可以在 WordPress 管理中添加/编辑/删除所有这些,没有任何问题。
我现在遇到的问题是在前端显示这些产品自定义帖子类型。如果我进入“编辑产品”管理部分,我可以看到每个产品的永久链接(我什至可以看到每个分类的链接),但单击它们会弹出 404。
我希望能够直接链接到产品(例如示例 http://localhost:8888/project/product/productname/) 就像它的显示一样在编辑中CPT 页面。如果他们只选择这些产品的分类法,我还想列出产品。希望这是有道理的。
有没有一种方法可以做到这一点,而不必为每个分类和产品制作页面?这是我第一次展示 CPT(任何与此相关的帖子),所以我很难理解它。我需要为这些制作页面吗?
谢谢你!
这是我的自定义帖子类型:
/* CUSTOM POST TYPE AREA FOR PRODUCTS */
add_action( 'init', 'create_product_post_type' );
function create_product_post_type()
{
$labels = array(
'name' => _x('Products', 'post type general name'),
'singular_name' => _x('Product', 'post type singular name'),
'add_new' => _x('Add New', 'products'),
'add_new_item' => __('Add New Product'),
'edit_item' => __('Edit Product'),
'new_item' => __('New Product'),
'view_item' => __('View Product'),
'search_item' => __('Search Products'),
'not_found' => __('No products found'),
'not_found_in_trash' => __('No products found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Products'
);
$args = array(
'label' => __('Products'),
'labels' => $labels,
'public' => true,
'can_export' => true,
'show_ui' => true,
'_builtin' => false,
'capability_type' => 'post',
'menu_icon' => get_bloginfo('template_url').'/functions/images/product.png',
'hierarchical' => false,
//'rewrite' => array( "slug" => "product" ),
'supports' => array('title'), //MAYBE add thumbnail later!
'show_in_nav_menus' => true
);
register_post_type( 'products', $args);
}
//*********************************************************************//
//**************Custom Taxonomy for Products***************************//
//*********************************************************************//
/* CUSTOM TAXONOMY FOR PRODUCTS POST */
function create_productcategory_taxonomy() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' =>_x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Product Category' ),
'update_item' => __( 'Update Product Category' ),
'add_new_item' => __( 'Add New Product Category' ),
'new_item_name' => __( 'New Product Category' ),
'separate_items_with_commas' => __( 'Separate categories with commas' ),
'add_or_remove_items' => __( 'Add or remove product categories' ),
'choose_from_most_used' => __( 'Choose from the most used categories' )
);
register_taxonomy('productcategory', 'products', array (
'label' => __('Product Category'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'product-category'),
));
}
function create_product_type_taxonomy() {
$labels = array(
'name' => _x( 'Types', 'taxonomy general name' ),
'singular_name' =>_x( 'Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Types' ),
'popular_items' => __( 'Popular Types' ),
'all_items' => __( 'All Types' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Product Type' ),
'update_item' => __( 'Update Product Type' ),
'add_new_item' => __( 'Add New Product Type' ),
'new_item_name' => __( 'New Product Type' ),
'menu_name' => __( 'Types' )
);
register_taxonomy('producttype', 'products', array (
'label' => __('Product Type'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'product-type'),
));
}
add_action( 'init', 'create_productcategory_taxonomy', 0 );
add_action( 'init', 'create_product_type_taxonomy', 0 );
I have a custom post type of Products in my WordPress project. The CPT has several custom fields and two taxonomies that can be attached to it. I can add/edit/delete all these in the wordpress admin without any problems.
My issue I'm struggling with now is DISPLAYING these Products custom post types on the front end. If I go into the EDIT PRODUCT admin section I can see the Permalink for each individual product ( I can even see links for each taxonomy) but clicking them brings up a 404.
I'd like to be able to link directly to products (for example http://localhost:8888/project/product/productname/) like its show in in Edit CPT page. I'd also like to list Products if they pick just the taxonomies for those products. Hope that makes sense.
Is there a way of doing this without having to make pages for each taxonomy and product? This is my first time displaying CPTs (any posts for that matter) so I'm having trouble wrapping my head around it. Do I need to make Pages for these?
Thank you!
Here is my custom post type:
/* CUSTOM POST TYPE AREA FOR PRODUCTS */
add_action( 'init', 'create_product_post_type' );
function create_product_post_type()
{
$labels = array(
'name' => _x('Products', 'post type general name'),
'singular_name' => _x('Product', 'post type singular name'),
'add_new' => _x('Add New', 'products'),
'add_new_item' => __('Add New Product'),
'edit_item' => __('Edit Product'),
'new_item' => __('New Product'),
'view_item' => __('View Product'),
'search_item' => __('Search Products'),
'not_found' => __('No products found'),
'not_found_in_trash' => __('No products found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Products'
);
$args = array(
'label' => __('Products'),
'labels' => $labels,
'public' => true,
'can_export' => true,
'show_ui' => true,
'_builtin' => false,
'capability_type' => 'post',
'menu_icon' => get_bloginfo('template_url').'/functions/images/product.png',
'hierarchical' => false,
//'rewrite' => array( "slug" => "product" ),
'supports' => array('title'), //MAYBE add thumbnail later!
'show_in_nav_menus' => true
);
register_post_type( 'products', $args);
}
//*********************************************************************//
//**************Custom Taxonomy for Products***************************//
//*********************************************************************//
/* CUSTOM TAXONOMY FOR PRODUCTS POST */
function create_productcategory_taxonomy() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' =>_x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Product Category' ),
'update_item' => __( 'Update Product Category' ),
'add_new_item' => __( 'Add New Product Category' ),
'new_item_name' => __( 'New Product Category' ),
'separate_items_with_commas' => __( 'Separate categories with commas' ),
'add_or_remove_items' => __( 'Add or remove product categories' ),
'choose_from_most_used' => __( 'Choose from the most used categories' )
);
register_taxonomy('productcategory', 'products', array (
'label' => __('Product Category'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'product-category'),
));
}
function create_product_type_taxonomy() {
$labels = array(
'name' => _x( 'Types', 'taxonomy general name' ),
'singular_name' =>_x( 'Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Types' ),
'popular_items' => __( 'Popular Types' ),
'all_items' => __( 'All Types' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Product Type' ),
'update_item' => __( 'Update Product Type' ),
'add_new_item' => __( 'Add New Product Type' ),
'new_item_name' => __( 'New Product Type' ),
'menu_name' => __( 'Types' )
);
register_taxonomy('producttype', 'products', array (
'label' => __('Product Type'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'product-type'),
));
}
add_action( 'init', 'create_productcategory_taxonomy', 0 );
add_action( 'init', 'create_product_type_taxonomy', 0 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上周遇到了这个问题,我找到的解决方案:
1- 创建一个循环浏览一个(或多个)自定义帖子类型的所有帖子的页面:
http://codex.wordpress.org/Pages#A_Page_of_Posts_for_a_Custom_Post_Type这是我在wordpress文档中找到的一个例子。因此,创建该页面模板后,您只需在管理员中创建一个页面并选择刚刚创建的模板即可。
2-如果您想在网站的主循环中显示自定义帖子,则此代码效果很好:
http://justintadlock .com/archives/2010/02/02/showing-custom-post-types-on-your-home-blog-page
3-最后在 WordPress 3.1+ 中,您可以创建 single-{posttype}.php (例如:single-acme_product.php)以更改显示自定义帖子类型时的显示方式。
希望可以帮助你
Had this problem last week, the solutions i found:
1- create a page that loops through all posts of a (or several) custom post type:
http://codex.wordpress.org/Pages#A_Page_of_Posts_for_a_Custom_Post_Type that's an exemple i found in wordpress documentation. So after creating that page template, you would just have to create a page in the admin and chose the template you just created.
2- if you want to display the custom posts in the site's main loop this code works great:
http://justintadlock.com/archives/2010/02/02/showing-custom-post-types-on-your-home-blog-page
3- finally in wordpress 3.1+ you can create single-{posttype}.php (ex: single-acme_product.php) to change the way the custom post type are displayed when in showing one.
hope that could help you