Link to home
Start Free TrialLog in
Avatar of LZ1
LZ1Flag for United States of America

asked on

How to get Wordpress to use a specific page template depending on URL params?

Hey Experts!!

I have a Wordpress site that's using data from a Salesforce DB.  

I have a plugin in WP that looks at the URL and runs a query based on what it finds.

What I need help with is to make WP use a specific page template depending on the URL.

For example:
If the URL contains artist:
              Redirect to page-artist.php

Any help is much appreciated.
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

In the php when you discover the string and know where you want to go add:

Header( "Location: http://pages-artist.php" );

You need to send the header before anything else is sent back to the client.


Cd&
Avatar of LZ1

ASKER

I'm already doing a redirection once though. I need the page template to be transparent to the user, as it is in normal instances of Wordpress
I don't follow what you mean 'transparent to the user'... are you trying to hide the address of the page?


Cd&
BTW, I don't work with wordpress so if that was a stupid comment; let me know.

Cd&
Avatar of LZ1

ASKER

I think I've found what I'm looking for. Wordpress has the template_redirect hook, which I'm reading up on right now.  Looks like it maybe what I need.

I don't want to change the URL, in fact I can't. I need the URL to remain normal.  What I need to do is use a different page template depending on a variable in the URL.
Okay I gotcha.  Sorry I didn't help much.

Cd&
Avatar of LZ1

ASKER

No worries. Thanks for your help.
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LZ1

ASKER

Hey Jason,
So my URL looks like this:
http://www.mydomain.com/index.php/artists/500/?artID=123

But artists isn't a category or a slug. It's not even in WP. It's coming from the Salesforce DB. So I have some PHP that grabs that and I can condition on that which is great. But I need to condition a page template when the URL looks like that.
Right, so your base page.php file doesn't actually display any content.  Instead, it does a series of checks to see what URL params are set (if any) and then calls the correct get_template_part() with parameters based on the tests.

So the flow would be something like:

if no parameters set then show normal-page.php template

if artID is set then test value of artID against SalesForce, return slug then set get_template_part()
Avatar of LZ1

ASKER

That sounds right.

So how would that look in PHP with the slugs?

get_template_part( $artist, "page-artist.php" );
???
Avatar of LZ1

ASKER

Thoughts??