WordPress.org

Ready to get started?Download WordPress

Forums

creating categories for products (9 posts)

  1. vince23
    Member
    Posted 1 year ago #

    hey there,

    I ve created products in WP with this code

    add_action('init', 'catalog_init');
    function catalog_init()
    {
      $labels = array(
        'name' => _x('Products', 'post type general name'),
        'singular_name' => _x('Product', 'post type singular name'),
        'add_new' => _x('Add New', 'product'),
        'add_new_item' => __('Add New Product'),
        'edit_item' => __('Edit Product'),
        'new_item' => __('New Product'),
        'view_item' => __('View Product'),
        'search_items' => __('Search Products'),
        'not_found' =>  __('No products found'),
        'not_found_in_trash' => __('No products found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => 'Products'
    
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'menu_icon' => get_bloginfo('template_url') . '/images/producticon.png',
        'supports' => array('title','editor','thumbnail','excerpt')
      );
      register_post_type('product',$args);
    }

    Now I d like to add categories.

    Do you guys know how ?

  2. kmessinger
    Volunteer Moderator
    Posted 1 year ago #

    Can't you create the categories as explained at
    http://codex.wordpress.org/Posts_Categories_SubPanel

  3. vince23
    Member
    Posted 1 year ago #

    I didnt mean categories in posts.

    The categories must be associated with the products (product categories).

  4. kmessinger
    Volunteer Moderator
    Posted 1 year ago #

    I was thinking you wanted the categories to appear in the menu with a link to the products in that category.

    There are others looking also. Google product categories in wordpress - I didn't read any of them but maybe there is help there.

  5. vince23
    Member
    Posted 1 year ago #

    no, not really.

    I just wanted to list categorized lists of products.

  6. vince23
    Member
    Posted 1 year ago #

    I m kinda impressed by myself. I figured it out !!!

    so here is the solution:

    <?php register_taxonomy_for_object_type( 'category', 'object type' ); ?>

  7. catacaustic
    Member
    Posted 1 year ago #

    You can do this two ways.

    First, create a new taxonomy for the categories using register_taxonomy(). I'd recommend that as it's easy to do and can be set to the costuom post type that you wish very easily.

    Second, you can create another cusotm post type for catgegories and craete your own custom functions to look after the product-catgory associations.

  8. vince23
    Member
    Posted 1 year ago #

    this is what I did. I created a custom template which lists the products.

    <?php
    /**
     * Template Name: Sidebar Template
     * Description: A Page Template that adds a sidebar to pages
     *
     * @package WordPress
     * @subpackage Twenty_Eleven
     * @since Twenty Eleven 1.0
     */
    
    get_header(); ?>
    
    		<div id="primary">
    			<div id="content" role="main">
    
    				<?php while ( have_posts() ) : the_post(); ?>
    
    					<?php get_template_part( 'content', 'page' ); ?>
    
    					<?php comments_template( '', true ); ?>
    
    				<?php endwhile; // end of the loop. ?>
    
    			</div><!-- #content -->
    		</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    How can make it look after the categories ??

  9. vince23
    Member
    Posted 1 year ago #

    oops sorry, that was the wrong code, here is what I used

    <?php
    /*
    Template Name: Products
    */
    ?>
    <?php get_header(); ?>
    	<?php query_posts(array('post_type' => 'product')); ?>
    	<?php while(have_posts()): the_post(); ?>
    	<div class="product-thumb">
    	            <?php the_post_thumbnail(); ?></div>
    	<h1 class="product-title"><?php the_title(); ?></h1>
    
    	<div class="product-description">
    	            <?php the_content() ?></div>
    
    	<?php endwhile; ?>
    	<?php get_footer(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic