List a custom taxonomy's terms, with links, on the taxonomy page (11 posts)

  1. codeview
    Posted 2 years ago #

    I have registered a custom post type in my functions.php file for "Wines", with this code:

    function customposttype_wines() {
    $labels = array(
        'name'                => 'Wines',
        'singular_name'       => 'Wine',
        'menu_name'           => 'Wines',
        'parent_item_colon'   => 'Parent Wine',
        'all_items'           => 'All Wines',
        'view_item'           => 'View Wine',
        'add_new_item'        => 'Add New Wine',
        'add_new'             => '+',
        'edit_item'           => 'Edit Wine',
        'update_item'         => 'Update Wine',
        'search_items'        => 'Search Wines',
        'not_found'           => 'No Wines found',
        'not_found_in_trash'  => 'No Wines found in Trash',
    $args = array(
        'label'               => 'wines',
        'description'         => 'Wine Reviews',
        'labels'              => $labels,
        'supports'            => array( 'title', ),
        'unset'            => array ('date'),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
    add_action( 'init', 'customposttype_wines', 0 );

    And registered 9 custom taxonomies (brand, region, grape, etc.) with this code (repeated 9 times with only the <XXX> changing for each):

    function customtaxonomy_<XXX> ()  {
    $labels = array(
        'name'                       => '<XXX>',
        'singular_name'              => '<XXX>',
        'menu_name'                  => '<XXX>s',
        'all_items'                  => 'All <XXX>s',
        'parent_item'                => 'Parent <XXX>',
        'parent_item_colon'          => 'Parent <XXX>:',
        'new_item_name'              => 'New <XXX> Name',
        'add_new_item'               => 'Add New <XXX>',
        'edit_item'                  => 'Edit <XXX>',
        'update_item'                => 'Update <XXX>',
        'separate_items_with_commas' => 'Separate <XXX>s w/commas',
        'search_items'               => 'Search <XXX>s',
        'add_or_remove_items'        => 'Add or Remove <XXX>s',
        'choose_from_most_used'      => 'Choose from the most used <XXX>s',
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'query_var'                  => true,
    add_action( 'init', 'customtaxonomy_<XXX>', 0 );

    Now I am trying to display the lists of custom taxonomy terms on the parent page of each, so when visiting http://www.mywebsite.com/wines/<XXX>; there is a list, with links, to all the terms that have been entered.

    Right now when I visit http://www.mywebsite.com/wines/<XXX>; I just see the 404.php error page. But if I visit a taxonomy term page I see the posts within there fine, ie: http://www.mywebsite.com/wines/<XXX>/term

    I tried using a taxonomy template hierarchy page, taxonomy-<XXX>.php but it will not load, still just shows the 404.php.

    Pulling hair here, any help is very appreciated.

  2. keesiemeijer
    Posted 2 years ago #

    How did you get urls like this:
    Custom taxonomies have urls like this:

    Did you register the taxonomy with something like this:

    'rewrite' => array(
    	'slug' => 'wines/<XXX>',
    	'with_front' => true,
    	'hierarchical' => true ,
    ) ,

    Or are you using some other rewrite rules?

    Did you re-save your permalink structure in wp-admin > Settings >Permalinks.

  3. codeview
    Posted 2 years ago #

    @keesiemeijer - oh, yes, I am using this plugin to have the /wines in the links because I couldn't figure out how to do it otherwise. I'll try disabling the plugin and adding the rewrites as you have listed to see if that helps. I will report back. Thank you for your help.

  4. codeview
    Posted 2 years ago #

    No luck :( The new rewrite argument is making a 404 appear now at http://www.mywebsite.com/wines/<XXX>/term instead of the posts that were loading before.

  5. codeview
    Posted 2 years ago #

    Oh, and yes I did flush the rewrite after the change by going to Permalinks > Settings and clicking save.

  6. keesiemeijer
    Posted 2 years ago #

    Try it without the plugin (deactivate and refresh permalinks) and register the custom taxonomies with the rewrite rules as shown above:

    'rewrite' => array(
    	'slug' => 'wines/<XXX>',
    	'with_front' => true,
    	'hierarchical' => true ,
    ) ,

    Now your terms should be at

    Just refresh the permalink structure and see where WordPress thinks the term archive pages are when visiting the custom terms in the wp-admin.

    Create a 'wine' custom post type post called "<XXX>"
    this post has a url

    Do this for all your taxonomies.

    Create a template for the single custom post type display:
    single-{post_type}.php (single-wines.php)

    In this template check the ID of the post and show the terms.
    Very basic example:

    $taxonomy = '';
    // $post ID is available on single post pages
    if($post->ID == 22)
    	$taxonomy = 'grapes';
    if($post->ID == 44)
    	$taxonomy = 'brand';
    // etc
    // check if it's a taxonomy post
    if('' != $taxonomy) {
    	// show all terms from the taxonomy
    	 wp_list_categories( 'taxonomy=' . $taxonomy ); 
    } else {
    	// normal custom post type wine post
    	// single loop here

    Always re-save your permalink structure after every step. I hope this all makes sense. I have this setup working on a couple of sites of mine.

  7. codeview
    Posted 2 years ago #

    Thank you again, keesiemeijer. The problem I see there is that my custom post type is "wines" and I would like the posts to appear under it as-is: http://www.mywebsite.com/wines/postname

    Just to clarify the goal:

    • @ mywebsite.com/ = all posts
    • @ mywebsite.com/wines = all posts under the custom post type "wines"
    • @ mywebsite.com/wines/brands = a list of all the terms that have been created under the custom taxonomy "brands", which is a taxonomy linked to the custom post type "wines".
    • @ mywebsite.com/wines/brands/acme = all the posts that have the term "acme" applied to them, which is a term belonging to the custom taxonomy "brands".
  8. codeview
    Posted 2 years ago #

    The only one not working right now is the 3rd bullet, @ mywebsite.com/wines/brands is where I get a 404. Shouldn't that page load the taxonomy.php or if it doesn't exist load the index.php?

  9. keesiemeijer
    Posted 2 years ago #

    Did you create a wines custom post type post 'brands'?
    What theme are you using?

  10. codeview
    Posted 2 years ago #

    No, "brands" is one of the custom taxonomies.

    I'm building the theme from scratch, so it's very simple and only using index.php and single.php for displaying the posts. I'm obviously doing something wrong or thinking I can do something that I cannot, perhaps.

  11. codeview
    Posted 2 years ago #

    The custom taxonomies are hierarchical, so they show in the admin like categories do for regular posts (with check boxes beside each). So, for the BRANDS taxonomy, "acme" is the category checked/selected under it.

Topic Closed

This topic has been closed to new replies.

About this Topic