0

I am building a business directory and will give some context and then have 2 questions at the end.

The development URL is: http://svcta.lainternet.biz/

The website I am rebuilding is: https://www.visitsimivalley.com/

The current website requires a unique business profile for each Taxonomy type. For example, if you are a Hotel and you also have Meeting Space and Wedding Venue space, you would end up with 3 listings, one for the hotel, one for meetings, and one for weddings.

I want to have 1 master profile that houses the info for EVERY data field we would display regardless of the Taxonomy type.

To minimize my coding, I am using Advanced Custom Fields and CPT UI.

I have one Custom Field “Directory Listing” with Slug “directory”. It currently has some basic fields for Address and Photo Gallery.

I created a new Taxonomy “Directory Categories” with Slug “directory_category” and created the following Terms under WordPress Admin > Directory > Categories:

Hotel
Meeting Space
Restaurant
Sports Venue
Things to Do
Wedding Venue

I created the following Templates

archive-directory.php
single-directory.php   
taxonomy-directory_category-hotels.php   
taxonomy-directory_category-meetings.php   
taxonomy-directory_category-restaurants.php   
taxonomy-directory_category-sports.php   
taxonomy-directory_category-things-to-do.php   
taxonomy-directory_category-weddings.php

I can see my directory listings and control their Archive/List View & Single views.

I can also control the Archive/List View for each Taxonomy.

Question 1 – Is this a sound approach? Did I make any mistakes that should be corrected now?

Question 2 – Now that I can control the lists, how do I setup template files that display the actual listing beyond WordPress using “single-directory.php”.

For example: I have directory listing for a hotel that contains all information needed for the Hotel page, Meetings page, and Weddings page. I want to control which template the user sees based upon the listings page they came from. The following 3 links all contain the “Best Western” profile.

http://svcta.lainternet.biz/directory_category/hotels/ http://svcta.lainternet.biz/directory_category/meetings/ http://svcta.lainternet.biz/directory_category/weddings/

How do I make it so that when your on the /hotels/ page and click on Best Western you are sent to the Hotel Template. And when you are on the /meetings/ or /weddings/ page and click on Best Western it loads the respective Meetings or Weddings template?

UPDATE 5-9-18

With Milo's help, I was able to put together the remaining pieces of code and templates needed to accomplish my original task.

I now have the following code in my functions.php file.

// Register Endpoints
// =============================================================================

function wpd_add_my_endpoints(){
    add_rewrite_endpoint( 'hotels', EP_PERMALINK );
    add_rewrite_endpoint( 'meetings', EP_PERMALINK );
    add_rewrite_endpoint( 'restaurants', EP_PERMALINK );
    add_rewrite_endpoint( 'sports', EP_PERMALINK );
    add_rewrite_endpoint( 'things-to-do', EP_PERMALINK );
    add_rewrite_endpoint( 'weddings', EP_PERMALINK );
}
add_action( 'init', 'wpd_add_my_endpoints' );



// Register Endpoint Templates
// =============================================================================

function wpd_endpoint_templates( $template ){
    global $wp_query;
    if( isset( $wp_query->query_vars['hotels'] ) ){
        $template = locate_template( 'single-directory-hotels.php' );
    }   
        elseif( isset( $wp_query->query_vars['meetings'] ) ){
        $template = locate_template( 'single-directory-meetings.php' );
    } 
        elseif( isset( $wp_query->query_vars['restaurants'] ) ){
        $template = locate_template( 'single-directory-restaurants.php' );
    } 
        elseif( isset( $wp_query->query_vars['sports'] ) ){
        $template = locate_template( 'single-directory-sports.php' );
    } 
        elseif( isset( $wp_query->query_vars['things-to-do'] ) ){
        $template = locate_template( 'single-directory-things-to-do.php' );
    } 
        elseif ( isset( $wp_query->query_vars['weddings'] ) ){
        $template = locate_template( 'single-directory-weddings.php' );
    }
    return $template;
}
add_filter( 'single_template', 'wpd_endpoint_templates' ); 

And I have the following new Template files.

single-directory-hotels.php
single-directory-meetings.php
single-directory-restaurants.php
single-directory-sports.php
single-directory-things-to-do.php
single-directory-weddings.php

Everything works on the front end as I imagined and my client is going to be very pleased with the product I get to deliver.

0

Q1 - It seems reasonable, if it checks all of your boxes and gives you a maintenance workflow that works for you.

Q2 - There are at least a few ways you could handle this, I think using rewrite endpoints is a fairly simple and workable solution.

We'll start with just a single endpoint- for example, say the URL for Best Western is at:

http://example.com/directory/best-western/

After we add a hotels endpoint, we'll also have the URL:

http://example.com/directory/best-western/hotels/

The code to do that:

function wpd_add_my_endpoints(){
    add_rewrite_endpoint( 'hotels', EP_PERMALINK );
}
add_action( 'init', 'wpd_add_my_endpoints' );

You can add that to your theme's functions.php, or better, put it in your own plugin. You'll also have to flush rewrite rules whenever you add or remove an endpoint, or otherwise change it. A quick way to do this during testing is to visit the Settings > Permalinks page on the admin side.

Ok, so now you can visit that new URL, but it currently shows the same thing as the original.

To change that, we can check for the presence of our new hotels query var to detect if that URL is being viewed, and load a different template in that case. We do this via the single_template filter, which lets us change the template WordPress has chosen for this type of query:

function wpd_endpoint_templates( $template ){
    global $wp_query;
    if( isset( $wp_query->query_vars['hotels'] ) ){
        $template = locate_template( 'single-directory-hotels.php' );
    }
    return $template;
}
add_filter( 'single_template', 'wpd_endpoint_templates' ); 

Now you can create a single-directory-hotels.php file and customize it for your needs. You'll also now have to modify your related directory_category template file to link to these new hotels/ URLs.

bonus you also have the ability to add (and read) any arbitrary value after the endpoint, for example:

http://example.com/directory/best-western/hotels/gallery/

get_query_var( 'hotels' ) will return whatever that contains.

In Conclusion

That's cool and all, you say, but those URLs would be better if they were like

http://example.com/directory/hotels/best-western/

That's possible! It's just much more complicated than this.

  • Thanks Milo for your input! I will update this post with my progress. – Micah K May 8 '18 at 18:17
  • Quick update, I implemented the code and it worked as you described. Going back to work to update the links on the listings pages and add more custom templates. – Micah K May 9 '18 at 17:11
  • Thanks again Milo. I will explore packaging this into a plugin now that the foundational functionality is working. – Micah K May 9 '18 at 21:54
  • The URL structure ended up working fine for our needs as well since I can granularly control the naming convention. – Micah K May 9 '18 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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