开发者

Displaying custom post types that have taxonomies

开发者 https://www.devze.com 2023-03-05 07:07 出处:网络
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 wit

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开发者_StackOverflow社区 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 );


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

0

精彩评论

暂无评论...
验证码 换一张
取 消