1

Hello ladies and gents!

I'm hoping you can help me with this one.. if it's each possible. I (ideally) need a general template for the categories of a custom post type.

Custom post type: Listings (type-listings) Listing's categories(cat-listings): Lease (lease) & Sale (sale)

So when I visit http://www.domain.com/listings/cat-listings/lease I get a 404 (does this mean that this page is not possible to have? or because I am missing a template. I suspect the former as I know wordpress has default ones to fallback on.) when I'd like to have a page that I can alter... In this page I want to show all of the posts in the lease category, and have the user be able to navigate to the similar sale page with the sale posts.

Now I know I could create a page for each and have them have their custom template pages but the issue I have with this is if my client ever wants to add another category a page and page template has to be created.

Another solution is to have an toggle/tab system where I just create a listings page and have a toggle/tab between the categories. My problem with this is I don't know how many listings there will be and therefore pagination should work. I don't think I could have a toggle/tab and pagination.

Thanks for your help!

I've tried the following and had no luck; cat-listings.php type-listings.php archive-type-listings.php archive-type-listings-cat-listings.php category.php category-type-listings.php category-cat-listings.php category-type-listings-cat-listings.php taxonomy-type-listings.php taxonomy-cat-listings.php taxonomy-type-listings-cat-listings.php

I found this post which sounds like what I need but as you can see from the above list I've tried it without luck. Same template for all the terms of a taxonomy

Here is how I register my custom post type:

    register_post_type('type-listings', array(
    'labels' => array(
    'name' => _x('Our Listings', 'Titles', theme_domain()),
    'singular_name' => _x('Listing', 'Titles', theme_domain()),
    'menu_name' => _x('Our Listings', 'Titles', theme_domain()),
    ),
    'public' => false,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => array('slug' => _x('listings', 'URL Slug', theme_domain()), 'with_front' => false, 'page' => false),
    'capability_type' => 'page',
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => 21,
    'menu_icon' => 'dashicons-admin-home',
    'supports' => array('title', 'custom-fields', 'revisions'),
    ));
    $catProjectsArgs = array(
    'label' => __('Availability', theme_domain()),
    'sort' => true,
    'show_ui' => true,
    'show_admin_column' => true,
    'hierarchical' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'args' => array('orderby' => 'term_order'),
    );
    register_taxonomy('cat-listings', array('type-listings'), $catProjectsArgs);

1 Answer 1

0

The name you are using for taxonomy isn't allowed. register_post_type() $taxonomy parameter says

$taxonomy

(string) (required) The name of the taxonomy. Name should only contain lowercase letters and the underscore character, and not be more than 32 characters long (database structure restriction).

Default: None

So, you need to change the name cat-listings to something else( I will assume cat_listings for the rest of the answer)

Now let's visit your questions one by one.

  1. So when I visit http://www.domain.com/listings/cat_listings/lease I get a 404 (does this mean that this page is not possible to have? or because I am missing a template.

It's possible to have Permalink structure like that. If you create a term named lease under taxonomy cat_listings, the default url structure will be

http://www.domain.com/cat_listings/lease

If you want to add a segment named listings before that, you have to pass rewrite arguments like this

$catProjectsArgs = array(
    'label' => __('Availability', theme_domain()),
    'sort' => true,
    'show_ui' => true,
    'show_admin_column' => true,
    'hierarchical' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'listings' )
);

By the way, there is no parameter named args, I've removed that.

  1. Now I know I could create a page for each and have them have their custom template pages but the issue I have with this is if my client ever wants to add another category a page and page template has to be created.

It should be pretty clear which template file you have to use, if you look at the template hierarchy image. Here's a cropped version just for custom taxonomy

template hierarchy for custom taxonomy

If you want to apply a single template for all terms under cat_listings, just create template file taxonomy-cat_listings.php. If you want to create separate template file for one of the terms that you add later, say lease, you need to create template file taxonomy-cat_listings-lease.php and so on.

2
  • Thanks your answer has been very helpful. My first problem was not understanding that my taxonomy (cta-listings) is not within my custom post type (type-listings) but rather separate. Thanks again for your help! May 21, 2015 at 20:14
  • @robinwkurtz glad I could help.
    – sakibmoon
    May 22, 2015 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.