WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying taxonomy as category page? (4 posts)

  1. Elliot Toman
    Member
    Posted 1 year ago #

    I'd like to display every post tagged with a certain taxonomy on a page. Here's what I've got so far...

    In functions.php, I've created a custom post type and custom taxonomy:

    // IMAGE GALLERY
    	register_post_type(
    		  'gallery', array(
    			  'labels' => array(
    				  'name' => 'Image Gallery',
    				  'singular_name' => 'Gallery Image',
    				  'add_new' => 'Add new gallery image',
    				  'add_new_item' => 'Add gallery image',
    				  'new_item' => 'New gallery image',
    				  'view_item' => 'View gallery images',
    				  'edit_item' => 'Edit gallery image',
    				  'not_found' =>  __('No gallery images found'),
    				  'not_found_in_trash' => __('No gallery images found in Trash')
    			  ),
    			  'public' => true,
    			  'publicly_queryable' => true,
    			  'show_ui' => true,
    			  'query_var' => true,
    			  'rewrite' => true,
    			  'capability_type' => 'post',
    			  'exclude_from_search' => true,
    			  'hierarchical' => false,
    			  'menu_position' => null,
    			  'supports' => array('title', 'thumbnail'),
    			  'taxonomies' => array('image_category')
    		 )
    	  );
    // Custom Taxonomies
    function asw_register_taxonomies() {
    	register_taxonomy("image_category", array("gallery"),
    	array(
    		"hierarchical" => true,
    		"label" => __('Image Categories', 'gallery'),
    		"singular_label" => "Image Category",
    		"query_var" => true,
    		"rewrite" => true));
    }

    The taxonomies show up perfectly in the post type and are successfully saved with the post.

    Then I have a template for the gallery which contains this code for the taxonomy list:

    <?php // LIST IMAGE CATEGORIES
    $categories = array(
    	'show_option_all'    => '',
    	'orderby'            => 'name',
    	'order'              => 'ASC',
    	'style'              => 'list',
    	'show_count'         => 0,
    	'hide_empty'         => 1,
    	'use_desc_for_title' => 1,
    	'child_of'           => 0,
    	'feed'               => '',
    	'feed_type'          => '',
    	'feed_image'         => '',
    	'exclude'            => '',
    	'exclude_tree'       => '',
    	'include'            => '',
    	'hierarchical'       => true,
    	'title_li'           => __( '' ),
    	'show_option_none'   => __('No categories'),
    	'number'             => null,
    	'echo'               => 1,
    	'depth'              => 0,
    	'current_category'   => 0,
    	'pad_counts'         => 0,
    	'taxonomy'           => 'image_category',
    	'walker'             => null
    ); ?>
    
    <?php wp_list_categories( $categories ); ?>

    ...which works like a charm, displaying all the categories of the taxonomy. But when I click on one of the links in the list, it sends me to [url]/image_category/sample-images/ -- which uses index.php and contains a "page not found" error.

    I have a template named taxonomy-image_category.php which contains a standard loop, but WP isn't using it; it loads the category from index.php instead. I even tried creating a generic taxonomy.php page, but no cigar. Does anyone know what I'm missing?

  2. Does it work if you use image-category instead of image_category? Some of the template related functions strip out underscores; it's a known issue, but probably easier to just work around it in the mean time.

  3. Elliot Toman
    Member
    Posted 1 year ago #

    That didn't seem to work, Amy. I renamed the taxonomy "image-category" and updated the template to taxonomy-image-category.php, but WP is still defaulting to index.php.

  4. Elliot Toman
    Member
    Posted 1 year ago #

    I figured it out. In the process of trying random variations in the code, I set exclude_from_search to false and presto. Taxonomy page works.

    Follow-up research revealed that this technicality is in fact part of the documentation and I overlooked it.

Topic Closed

This topic has been closed to new replies.

About this Topic