• jamiemc25

    (@jamiemc25)


    Hi there,

    I recently created a custom post type called ‘Stock’ and added a custom taxonomy to this post type called ‘stock_category’. The code I added to the functions.php file is as follows:

    add_action( 'init', 'create_stock_post_type' );
    add_action('init', 'register_stock_taxonomy');
    
    function register_stock_taxonomy() {
      register_taxonomy('stock_category',
                        'stock',
                         array (
                               'labels' => array (
                                                  'name' => 'Stock Categories',
                                                  'singular_name' => 'Stock Categories',
                                                  'search_items' => 'Search Stock Categories',
                                                  'popular_items' => 'Popular Stock Categories',
                                                  'all_items' => 'All Stock Categories',
                                                  'parent_item' => 'Parent Stock Category',
                                                  'parent_item_colon' => 'Parent Stock Category:',
                                                  'edit_item' => 'Edit Stock Category',
                                                  'update_item' => 'Update Stock Category',
                                                  'add_new_item' => 'Add New Stock Category',
                                                  'new_item_name' => 'New Stock Category',
                                                ),
                                'hierarchical' =>false,
                                'show_ui' => true,
                                'show_tagcloud' => true,
                                'rewrite' => true,
                                'public'=>true
                                )
                         );
    }
    
    function create_stock_post_type() {
    	register_post_type( 'stock',
    		array(
    			'labels' => array(
    				'name' => __( 'Stock' ),
    				'singular_name' => __( 'Item' )
    			),
    			'supports' => array(
    				'title',
    				'editor',
    				'thumbnail',
    			),
    			'public' => true,
    			'has_archive' => true,
    		)
    
    	);
    
    }

    I have the post types being displayed correctly on a custom page template called ‘Stock List’. The page can be found here.

    As you can see from the link I also have a list of relevant categories (terms) from the taxonomy to the left. To show the terms I used the following code:

    $customPostTaxonomies = get_object_taxonomies('stock');
    					if(count($customPostTaxonomies) > 0)
    					{
    						 foreach($customPostTaxonomies as $tax)
    						 {
    							 $args = array(
    								  'orderby' => 'name',
    								  'show_count' => 0,
    								  'pad_counts' => 0,
    								  'hierarchical' => 1,
    								  'taxonomy' => $tax,
    								  'title_li' => ''
    								);
    
    							 wp_list_categories( $args );
    						 }
    					}

    However, when I try clicking on one of the links that gets generated, it always just goes to the index.php and no other page. I’ve tried adding ‘archive.php’, ‘taxonomy.php’, category.php.. you name it I’ve added it. I just can’t seem to work out which page I should be adding and what I can do to get these links to point to a different page other than index.php.

    Any help would be fantastic. Thanks in advance.

  • The topic ‘Show all custom posts with a certain term’ is closed to new replies.