• Joey

    (@joeythesquid)


    I’m trying to integrate a “products” custom post type into my custom bare-bones theme. The post type shows up in the admin interface properly and I can add posts and even save the additional metadata. So all of those functions appear to be working okay. What I’m having trouble with is displaying that data on the front end.

    First off, my goal is to create:
    – A product page that lists all the product custom post types, sorted by category (eg. a catalog page).
    – Individual product category pages (pages that list all products for a specific category)

    Product Catalog Page
    I’ve got the catalog page started. Right now it lists all files but does not sort them by category. Quite frankly, I’m a designer not a developer so I have no idea how to approach that just yet. 😉 I am, of course, open to any feedback. My bigger concern is that the meta information (such as price, etc.) is not showing on my catalog page or my individual product pages.

    Individual Product Pages
    The pages are there but they’re obviously using the wrong template. The single-product.php template applies to my catalog page for formatting, however I have no idea where the template is for these individual pages. It’s not single.php, right?

    Functions.php
    Here’s my functions.php file (excuse the table for the admin. it’s a quick fix):

    <?php
    register_sidebar( array('name'=>'sidebar1') );
    register_sidebar( array('name'=>'sidebar2') );
    
    if (function_exists('register_nav_menu')) { // Menu functionality
        register_nav_menu('masthead', __('Menu','mainmenu'));
    }
    function check_thumbnail_support() {   //Turns on thumbnails only for custom post types
     if (!current_theme_supports('post-thumbnails')) {
       add_theme_support( 'post-thumbnails' );
       add_action('init','remove_posttype_thumbnail_support');
     }
    }
    add_action('after_setup_theme','check_thumbnail_support',99);  
    
    function remove_posttype_thumbnail_support() {
     remove_post_type_support('post','thumbnail');
     remove_post_type_support('page','thumbnail');
    }  
    
    add_action('init', 'product_register'); //Register Products
    function product_register() {
    	$args = array(
        	'labels' => array(
    		'name'				=> __('Products'),
    		'singular_name'			=> __('Product'),
    		'add_new'				=> __('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'			=> __('Product not found'),
    		'not_found_in_trash'	=> __('Product not found in trash')),
    		'public'				=> true,
    		'show_ui'				=> true,
    		'capability_type'		=> 'post',
    		'hierarchical'			=> false,
    		'rewrite'				=> true,
    		'supports'				=> array('title', 'editor', 'thumbnail', 'revisions')
        );
    	register_post_type( 'product' , $args );
    }
    
    add_action("admin_init", "admin_init"); //Add admin Labels
    function admin_init(){
    		add_meta_box("prodInfo-meta", "Product Options", "meta_options", "product", 'normal', 'high');
    	}
    
    	function meta_options(){ //Product Options
    		global $post;
    		$custom_fields = get_post_custom($post->ID);
    		$sku = $custom_fields["sku"][0];
    		$hours = $custom_fields["hours"][0];
    		$price = $custom_fields["price"][0];
    ?>
      <table class="" width="300px" border="0" cellspacing="0" cellpadding="0">
    	  <tr>
    		  <td><label>Sku:</label></td>
    		  <td><input name="sku" value="<?php echo $sku; ?>" /></td>
    	  </tr>
    	  <tr>
    		  <td><label>Hours:</label></td>
    		  <td><input name="hours" value="<?php echo $hours; ?>" /></td>
    	  </tr>
    	  <tr>
    		  <td><label>Price:</label></td>
    		  <td><input name="price" value="<?php echo $price; ?>" /></td>
    	  </tr>
      </table>
    <?php
    	}
    
    add_action('save_post', 'save_product_details'); //Save product options
    function save_product_details(){
    	global $post;
    	update_post_meta($post->ID, "sku", $_POST["sku"]);
    	update_post_meta($post->ID, "hours", $_POST["hours"]);
    	update_post_meta($post->ID, "price", $_POST["price"]);
    }
    
    register_taxonomy("catalog", array("product"), array("hierarchical" => true, "label" => "Catalog", "singular_label" => "Catalog", "rewrite" => true));
    
    ?>

    single-product.php

    <?php
    	$custom_fields = get_post_custom($post->ID);
    	$sku = $custom["sku"][0];
    	$hours = $custom["hours"][0];
    	$price = $custom["price"][0];
    get_header(); ?>
    
    <div class="product">
    	<?php the_post(); ?>
    	<?php the_post_thumbnail(thumbnail); ?>
    	<div class="productbody">
    		<h2><?php the_title(); ?></h2>
    		<p><strong>SKU:</strong> <?php echo $sku; ?></p>
    		<p><strong>Hours:</strong> <?php echo $hours; ?></p>
    		<p><strong>Price:</strong> <?php echo $price; ?></p>
    		<h3>Details:</h3> <?php the_content(); ?></p>
    	</div>
    </div>
    
    <?php get_footer(); ?>

    tpl.catalog.php

    <?php
    /*
    Template Name: Product Catalog
    */
    ?>
    
    <?php get_header(); ?>
    
    <h1><?php the_title(); ?></h1>
    
    <!-- Custom Query
    ******************************************************** -->
    <?php query_posts(array('post_type'=>'product')); ?>
    
    <!-- WordPress Loop
    ******************************************************** -->
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
     <?php the_content(__('Read more'));?>
    <?php endwhile; else: ?>
      <p><strong>There has been a glitch in the Matrix.</strong><br />
      There is nothing to see here.</p>
      <p>Please try somewhere else.</p>
    <?php endif; ?>
    
    <?php get_footer(); ?>

Viewing 1 replies (of 1 total)
  • Thread Starter Joey

    (@joeythesquid)

    Bump. Can anyone provide any insight on this? I’m really quite stuck. 🙁

Viewing 1 replies (of 1 total)
  • The topic ‘Need help displaying custom post types’ is closed to new replies.