Custom templates with custom url structure in wordpress

88 Views Asked by At

I'm working on a website that includes a section for Barbers, and I want to achieve the following URL structure:

Barbers {domain.com/barbers This page will have 2 section, 1 section will list Barber Organizations and another section will have list of states which have barbers}/
├── Individual Organization Page {domain.com/<<organization-name>> this page will display overview of the organization, contact details and have further 3 parts}/
│   ├── Branches {domain.com/<<organization-name>>/branches this page will again display list of states related to the selected organization}/
│   │   └── Individual State  {domain.com/<<organization-name>>/branches/<<state>> this page will again display list of cities, in the states selected previously, related to the selected organization}/
│   │       └── Individual City {domain.com/<<organization-name>>/branches/<<state>>/<<city>> this page will again display all branches, in the city selected previously, related to the selected organization}
│   ├── Barbers related to the selected organization {domain.com/<<company-name>>/barbers} this will again list all the states which have barbers related to the selected organization/
│   │   └── Individual State  {domain.com/<<organization-name>>/barbers/<<state>> this page will again display list of cities, in the state selected previously, related to the selected organization}/
│   │       └── Individual City {domain.com/<<organization-name>>/barbers/<<state>>/<<city>> this page will again display all barbers, in the city selected previously, related to the selected organization}
│   └── Super Barbers related to the selected company {domain.com/<<organization-name>>/super-barbers this will display list of Super Barbers for the selected organization}
└── Individual State  {domain.com/barbers/<<state>> this will display list of cities which have barbers, irrespective of any organization}/
    └── Individual City {domain.com/barbers/<<state>>/<<city>> this will display list of barbers in the selected City, irrespective of any organization}

Please refer this image link for a flow chart for better understand and visualization.

I plan to create custom post types for the elements mentioned above and link them using ACF Relationship fields or the P2P plugin.

  1. Barbers
  2. Organizations
  3. States
  4. Cities
  5. Super Barbers
  6. Branches

Here are my questions:

  1. What templates do I need to create to achieve this URL structure?
  2. Is there a way to register a layout type based on the URL structure?
  3. How efficient and SEO-friendly can this approach be?

Please note that, apart from organizations, nothing else will have a single.php template; everything else will be in archive or listing format related to the parameters mentioned.

Thank you in advance.

1

There are 1 best solutions below

1
On

What templates do I need to create to achieve this URL structure?

Response: See URL Structure and Templates below.

Is there a way to register a layout type based on the URL structure?

Response: See Layout based on URL structure below.

How efficient and SEO-friendly can this approach be?

Response: see my Comments

URL Structure and Templates

If I've understood the hierarchy correctly, I see the need for only two templates:

  1. a page template for Branches, and
  2. a page template for Barbers

These templates would act like search results pages.

This is based on these assumptions:

1. The Branch entity has these attributes (among others):

  • Organization
  • State
  • City

2. The Barber entity has these attributes (among others):

  • Organization
  • State
  • City
  • Super-Barber Status

3. Super-Barber is just a type of Barber and does not require its own search results page.

Further explanations are below.

WP_Rewrite

You will likely have to get familiar with WP_Rewrite to use your proposed URL structure. Examples below demonstrate when it would be used.

query_vars filter hook

The query_vars filter hook is needed to add custom URL query variables like organization, company, state, and city to WP_Query for use in page templates that act like search result pages.

URL Structure for Branches

Individual Organization Page

The WordPress Post name permalink structure parses URLs starting with the first part of the URL Path and checks to see if it matches a Page, Category, or Post (in that order).

The Individual Organization Page URL structure (domain.com/<<organization-name>>) fits perfectly with WordPress default behavior. If Organizations are represented as Pages in wp_posts, the post_name (slug) would be the <<organization-name>>. Rewriting the URL with WP_Rewrite is unnecessary in this case.

Branches

Looking at the URL for Branches (domain.com/<<organization-name>>/branches), it suggests a target page that shows Branches. However, your URL structure indicates that this URL targets a search results page. This page is a list of States where each State is associated with one or more Branches. It is as if a search page were used to filter a collection of Branches by Organization and only display a list of unique States.

Given the URL structure (domain.com/<<organization-name>>/branches), WP_Rewrite could be used to convert:

domain.com/<<organization-name>>/branches

into something like:

domain.com/?pagename=branch-search&organization=<<organiztion-name>>

The branch-search page would be an actual WordPress page with branch-search as the post_name. It would use a Custom Page Template (the page template for Branches) to recognize that since the only search key is the custom URL query variable organization, the results page should display a list of States.

Individual State

Each State in the list on page domain.com/<<organization-name>>/branches would use a URL with the Individual State structure like this: domain.com/<<organization-name>>/branches/<<state>>

WP_Rewrite would convert:

domain.com/<<organization-name>>/branches/<<state>>

into something like:

domain.com/?pagename=branch-search&organization=<<organiztion-name>>&state=<<state>>

The branch-search page would recognize that since two search keys are used (organization and state), the results page should display a list of Cities.

Individual City

WP_Rewrite would convert:

domain.com/<<organization-name>>/branches/<<state>>/<<city>>

into something like:

domain.com/?pagename=branch-search&organization=<<organiztion-name>>&state=<<state>>&city=<<city>>

The branch-search page would recognize that since three search keys are used (organization, state, and city), the results page should list the related Branches.

URL Structure for Barbers

Following the examples for URL Structure for Branches above, the URL structure for Barbers would use WP_Rewrite to convert URLs into those that target a Barbers page that uses the Barbers custom page template.

Individual State

Each State in the list would use a URL with the Individual State structure like this: domain.com/barbers/<<state>>

WP_Rewrite would convert:

domain.com/barbers/<<state>>

into something like:

domain.com/?pagename=barber-search&state=<<state>>

The barber-search page would recognize that since one search key is used (state), the results page should display a list of Cities.

Individual City

WP_Rewrite would convert:

domain.com/barbers/<<state>>/<<city>>

into something like:

domain.com/?pagename=barber-search&state=<<state>>&city=<<city>>

The barber-search page would recognize that since two search keys are used (state, and city), the results page should list the related Barbers.

Other URL Structures

The remaining URL structures would be handled just as the examples above for URL Structure for Branches.

For example, Barbers related to the selected organization {domain.com/<<company-name>>/barbers} would have the URL converted into something like: domain.com/?pagename=barber-search&company=<<company-name>>


Layout based on URL structure

If you are asking if you can add custom WordPress Post Formats, the answer is no. According to WordPress:

New formats cannot be introduced by themes or even plugins.

However, if two page templates (one for Branches, one for Barbers) were used, multiple layouts could be coded within each template.

For example, the custom page template used for the branch-search page could use a different layout based on the number and type of search keys:

If ( organization AND state AND city ) Then
    Use branch layout
Elseif ( organization AND state ) Then
    Use branch city layout
Elseif ( organization ) Then
    Use branch state layout
Else
    Use some other layout
EndIf

And for the custom page template used for the barber-search page:

If ( ( organization OR company ) AND state AND city ) Then
    Use barber layout
Elseif ( ( organization OR company ) AND state ) Then
    Use barber city layout
Elseif ( organization OR company ) Then
    Use barber state layout
Else
    Use some other layout
EndIf

If some layouts for Super-Barbers and Barbers are the same and some are different, Switch-Statements rather than If-Statements could be used to group the use of similar layouts. Otherwise, perhaps a strategy design pattern or "Layout" class could be used.