[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-09-11
12
Medium Priority
?
611 Views
Last Modified: 2012-09-12
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.
0
Comment
Question by:LZ1
  • 6
  • 4
  • 2
12 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 38387615
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&
0
 
LVL 30

Author Comment

by:LZ1
ID: 38387763
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
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 38387948
I don't follow what you mean 'transparent to the user'... are you trying to hide the address of the page?


Cd&
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 38387954
BTW, I don't work with wordpress so if that was a stupid comment; let me know.

Cd&
0
 
LVL 30

Author Comment

by:LZ1
ID: 38387963
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.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 38387996
Okay I gotcha.  Sorry I didn't help much.

Cd&
0
 
LVL 30

Author Comment

by:LZ1
ID: 38388010
No worries. Thanks for your help.
0
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 2000 total points
ID: 38388333
Pretty sure you are going to need something like this:

http://codex.wordpress.org/Function_Reference/get_template_part

 <?php get_template_part( $slug, $name ); ?>

And then just write some simple PHP that gets the slug from the URL and writes the appropriate get_template_part() function for pages.  So if "artist" is present in the URL, the page template uses:

$slug = "artist";
get_template_part( $slug, "page" );

for artist-page.php
0
 
LVL 30

Author Comment

by:LZ1
ID: 38388766
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.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 38388816
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()
0
 
LVL 30

Author Comment

by:LZ1
ID: 38388832
That sounds right.

So how would that look in PHP with the slugs?

get_template_part( $artist, "page-artist.php" );
???
0
 
LVL 30

Author Comment

by:LZ1
ID: 38391284
Thoughts??
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question