WordPress.org

Forums

[resolved] How to assign a Page Template to certain URL Patterns? (7 posts)

  1. Call_Me_Matthew
    Member
    Posted 1 year ago #

    Is there a way to set up a page template that assigns itself to pages of a particular url structure?

    For example, say you have these urls:

    http://www.mysite.com/beachhouse/snippet
    http://www.mysite.com/hospital/snippet
    http://www.mysite.com/farm/snippet
    http://www.mysite.com/mansion/snippet

    I don't want to have to create pages manually for every "snippet' subpage on my site. Instead, I want to create a page template that will load automatically when a url with a pattern of "http://mysite.com/x/snippet" is visited.

    I've already created a template file that loads content dynamically depending on the parent page(it takes the parent's slug from the url and serves up content based on that). I just need help figuring out how to assign it to a specific url pattern, as opposed to a page.

    Thanks for any help!

  2. bcworkz
    Member
    Posted 1 year ago #

    Sorry for the slow reply.

    Use add_rewrite_rule() so that when "snippet" is matched in the URL, your template is requested. You will want to pass the prior path element as an URL parameter so your template has the parent slug to use.

  3. Call_Me_Matthew
    Member
    Posted 1 year ago #

    I had already found an alternative but that may do the trick. I'll look into it. For now I'll mark this as closed.

  4. matt adams
    Member
    Posted 1 month ago #

    He Call_Me_Matthew, I'm interested in what your alternative was for this.
    Care to share?

  5. Call_Me_Matthew
    Member
    Posted 1 month ago #

    Hey Matt. I ended up using a plugin called Pods Framework with an extension for it called Pods Pages.

    Basically, You use Pods to create custom post types, and Pods Pages to create template files for those custom post types. Pods Pages will let you specify the url pattern to attach your template to.

    Then in my template code, I used $post->post_name to get the slug of the current page, and created a loop that outputs the custom post type with that matching slug.

    You can find plenty of tutorials/documentation on how it works on their site or by searching Google. I finished this project a year ago so I couldn't tell you exactly what I did, but hope this helps!

  6. matt adams
    Member
    Posted 1 month ago #

    Got it. Thanks matt.
    Im doing something similar to your original request but dont want to have to create all the duplicate pages down the line. I think i have it sorted out to wildcard match the permalink and the parent page will drive which template header to grab with an action hook.

  7. ryanmclaughlin1
    Member
    Posted 1 month ago #

Topic Closed

This topic has been closed to new replies.

About this Topic