Display page content using multiple templates - WordPress - php

Is it possible to have a page like: www.site.com/page/
and show different templated versions using, say:
www.site.com/page/?template=default
www.site.com/page/?template=archive
...?
So it retrieves the same page content but displays it differently.
Is this possible with WordPress? Is it standard or would need some tomhackery to do this?
Thank you

I answered a similar question a moment ago.
Manually set template using PHP in WordPress
The answer above should work, but the use of TEMPLATEPATH, I think is not ideal, it also seems to not take advantage of what WordPress is already doing to select a template.
function filter_page_template($template){
/* Lets see if 'template is set' */
if( isset($_GET['template']) ) {
/* If so, lets try to find the custom template passed as in the query string. */
$custom_template = locate_template( $_GET['template'] . '.php');
/* If the custom template was not found, keep the original template. */
$template = ( !empty($custom_template) ) ? $custom_template : $template;
}
return $template;
}
add_filter('page_template', 'filter_page_template');
Doing it this way, you don't need to add a new line for every template you want to be able to specify. Also, you take advantage of the existing template hierarchy, and account for the possibility that a non-existant template was entered.
I would point out that you should do some validation against the $_GET['template'] value before using it, but also that you might want to keep a running list to check against, so that they cant simply use any old template.

Create a 'master' template and assign it to your page. The master template doesn't contain any layout information—just a set of conditional include statements that selects the 'real' template based on the GET variable. The master template might look something like this:
<?php
switch ($_GET["template"]) {
case "foo":
include(TEMPLATEPATH . "/foo.php");
break;
case "bar":
include(TEMPLATEPATH . "/bar.php");
break;
case "baz":
include(TEMPLATEPATH . "/baz.php");
break;
default:
include(TEMPLATEPATH . "/default_template.php");
break;
}
?>

Related

Trying to add functionality to my WP theme for switching logos based on current language--

I am using the Polylang plugin for this. The theme is only bilingual, and I've managed to create a panel in the admin section for uploading the logos separately, registered to logo_sr and logo_es.
I now want to add a variable to the options table which will contain the URL to the logo which should be displayed, based on the current language. Here is what I've done so far:
I registered a new setting, logo_spanski_metar, for that purpose, and called it with admin_init:
register_setting('spanski-settings-group', 'logo_spanski_metar');
Then I used the following code to set its value based on the current language (HERE LIES THE PROBLEM, SOMEWHERE):
add_filter('logo_spanski_metar','change_logo');
function change_logo($logo) {
$lang = pll_current_language('locale');
switch ($lang) {
case 'sr_RS':
$logo = get_option( 'logo_sr') ;
break;
case 'es_ES':
$logo = get_option( 'logo_es') ;
break;
}
return $logo;
}
I know that the logo_sr and logo_es settings are saved properly because they work properly when I do this:
<img src="<?php echo get_option('logo_sr') ?>">
But the change_logo() function is messed up, the logo_spanski_metar variable remains empty.
Thank you for your time.
EDIT: I also know that the switch should be working correctly because I've echoed the pll_current_language('locale') function and it returns either sr_RS or es_ES.
EDIT 2: I solved the problem by forgetting about the third variable and just putting the switch directly in my header HTML. I am leaving this open in case someone can point out what I was doing wrong.

Get part of PHP file from URL

Is it possible to take a specific part of code from one php file. For a project I have 12 page templates. So in WordPress when I make a new page and want to select a page template, a long list will appear with 12 months.
Here an example of couple months.
Januari, Fabruari, Maart, April etc..
So the question will be, is it possible to place all php code in one page template. And when you go to a specific url it takes a look to the end of the domain and based on that one part of php code..
Example:
www.domain.com/januari
Takes the part from the url /januari
This is Januari
www.domain.com/februari
Takes the part from the url /februari
This is Februari
In future there will be a lot of page templates so it will look really messy. It should be cleaner if there will be one page template "Months" with all content in it. Hope you understand the problem!
Thanks!
You can use the global $_SERVER['REQUEST_URI'] to identify which path was used to get to your PHP script. Then you can print the specific parts based upon if conditions or a switch case:
For the url www.domain.com/januari $_SERVER['REQUEST_URI'] will be /januari, for www.domain.com/februari ir will be /februari, etc...
switch($_SERVER['REQUEST_URI']) {
case '/januari':
//January code here
break;
case '/februari':
//February code her
break;
//...
}
But note, that for the URL www.domain.com/januari?some=get&params $_SERVER['REQUEST_URI'] will be /januari?some=get&params.
You don't need to use any PHP methods for that.
Just use the concept of Page Slug in Wordpress.
here is the code which gives you the slug of current opened page.
<?php
global $post;
$post_slug = $post->post_name;
switch ($post_slug) {
case 'january':
// page for January
get_template_part('template-parts/january');
break;
case 'februari':
// page for februari
get_template_part('template-parts/februari');
break;
... and so on
}
By getting slug into variable '$post_slug' you can apply your conditional statements to compare the value with your paremeters.
OR also you can prefer is_page() method of wp to compare current page with your params.
There are a number of approaches to this problem. My approach will let you keep separate PHP files for each month but only one Page Template will be used.
Let's say page-template-month.php is your template file. And your month specific files have the following structure
theme-root /
- template-parts /
- - january.php
- - february.php
...
Now, the approach to get your specific query term and build logic around it.
Using get_the_ID()
The above function returns the post/page ID and you can build your logic around that ID. Now, your page-template-month.php file will have the following snippet
switch( get_the_ID() ) {
case 5:
// let's assume it's the page for January
get_template_part( 'template-parts/january' );
break;
case 18:
// let's assume it's the page for February
get_template_part( 'template-parts/february' );
break;
}
You can with get the actual link with and cut it
$actualLink = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$whatYouWant = substr($actualLink, strpos($actualLink, "/")[1]);
But why not use $_GET ?

giving custom argument vlue in 'q' variable

**also posted on druapl.stackexchange
https://drupal.stackexchange.com/questions/150500/giving-custom-argument-vlue-in-q-variable
**
apologies if question is ambiguous . the scenario is as follows:
in drupal 7 , we want to use a custom template page when a specific value is given for the q variable in url .
for example if we give http://localhost/drupal/?q=xyz/123 , we want to use a custom template page say page-xyz.tpl.php ..
have a hunch that hooks and template.php file may be the key components here but not sure what to exactly do..
any help appreciated.
you could implement theme_preproccess_page() (or node, or html) to control this in your template.php
function YOURTHEME_preproccess_page(&$vars) {
if (isset($_GET['q']) && $_GET['q'] == 'xyz/123') {
$vars['theme_hook_suggestions'][] = 'page_xyz';
}
}
that should work, but I would like to recommend not use the '?q=xyz' solution, but do an preproccess that should work to all your pages, like this.
function YOURTHEME_preproccess_page(&$vars) {
$title = strreplace(' ','_', $vars['node']->title);
//if you use the transliteration module, instead of strreplace
//use transliteration_get($vars['node']->title);
$vars['theme_hook_suggestions'][] = 'page_'.$title;
}
now that should work for every page that you want to make a custom template. Just add the file and clear the chaches. If you don't have the page template to the specific page, it's ok, drupal will use the default.

Drupal 7 - Page template suggestion based on specific alias

I'm poor php developer and i need code for my template.php to set specific template for some pages:
Example:
mypage/blog/about-me
mypage/blog/about-you
mypage/blog/about-us
Uses page--blog-about-first.tpl.php.
mypage/blog/about-him
mypage/blog/about-her
mypage/blog/about-them
Uses page--blog-about-second.tpl.php.
I can't find it anywhere so i'm asking here.
Check the naming conventions here:
https://drupal.org/node/1089656
If you need to do something more complex you can use the instructions here/:
https://drupal.org/node/223440#custom-suggestions
// Page template suggestions based off URL alias
$alias=drupal_get_path_alias($_GET['q']);
$args=explode('/', $alias);
if ($args[0]=='blog/about-me') {
$vars['theme_hook_suggestions'][] = 'page__simple_blog';
}
elseif ($args[0]=='artist') {
$vars['theme_hook_suggestions'][] = 'page__simple_blog';
}
This is the code. I got link /blog/about-me and link /artist. Suggestion works for artist page, but not for blog/about-me. Solution?

Drupal 7 - What is the variable in template.php that dictates which page template is used?

Ok, here's the deal: I am constructing a Drupal website that has several different sections. Each section is a view that displays a content type. (Each section has it's own content type) For example, I have a view that points to ?q=blog which displays content type blog.
All the sections look a little different than each other. Not like 'website-within-a-website' different but different enough that they can't all use the same template file and each be modified with CSS. Each section needs it's own page.tpl.php.
Unfortunately, AFAIK Drupal theme's .info files can only either assign one page.tpl.php for the entire theme or assign a page-node-####.tpl.php for each node. There is going to be lots of content on this website so setting Drupal to make a new identical page-node-####.tpl.php for every created node would get unmanagable very fast.
To solve this problem, I am going to use pathauto to create an alias for each content type. For example, all nodes of content type blog are given an alias ?q=blog/[post title]. Modify template.php to use page-blog.tpl.php for any page who's alias starts with the word 'blog'.
Other people have tried doing this sort of thing and have created functions such as the one described. Unfortunately, all the ones I have seen are for Drupal 6 or below. I have tried modifying existing ones with no success. So far, though, I think this is on the right track:
function basic_preprocess_page(&$vars, $hook) {
...
if( module_exists('path') ) {
$alias = drupal_get_path_alias( $_GET['q'] );
$site_section = "blog";
if( strpos( $alias, $site_section ) === 0 ) {
$VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE = "/path/to/page-blog.php";
}
}
}
I cannot find $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE does anyone know what it is?
Maybe my site is structured badly. If anyone knows how to restructure my site so I can more easily make a theme with seperate sections please share how!
Thanks a million! (c:
EDIT: Perhaps I need to use template suggestions instead. Does anyone know the function or variable to use to set this?
They changed the name of this array key in D7 and I haven't seen it documented anywhere. I finally figured this out after a good bit of debugging. You can override the theme template in template.php with a hook_preprocess_page() like so:
function myTheme_preprocess_page(&$vars) {
global $node;
if ($node->type == 'blog') {
$vars['theme_hook_suggestions'] = array('my__blog_template'); // use my--blog-template.tpl.php, note '-' = '_'
}
elseif ($node->type == 'articles') {
$vars['theme_hook_suggestions'] = array('article__node_template'); // use article--node-template.tpl.php
}
}
Oh and don't forget to flush the Drupal caches after making changes to your template.php.
Ok, I found it:
http://drupal.org/node/223440#comment-991840
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$variables['template_files'][] = $template_filename;
}
}
Credit to this function goes to user mfb.
I had a lot of trouble with this so I will explain it here in case anyone finds it useful.
This function goes in your template.php. It needs to be part of the <theme name>_preprocess_page function. What it does is it takes the alias and then explodes it into a bunch of different components. For example if you are on a page with the alias ?q=blog/blog-post-title it would be exploded into blog and blog-post-title. It then turns each component into a name for a template suggestion. It puts each template suggestion into the template_files[] array (inside the $variables[] array) so that the page now has two new template suggestions:
page-blog, and page-blog-blog-post-title
Template suggestions are alternate template files. In this case they are for pages, but they don't necessarily have to be. You can have template suggestions for anything you can think of including blocks, nodes and the like. Don't let the name 'template suggestion' fool you. Template suggestions will be used over default templates as long as they exist. I don't know why it was named like that. I think it should be renamed.
What you do, then, now that you've set up Drupal to look for a template suggestion that points to your alias, is create a new template file where all the rest are in your theme. In this case, let's say I want to theme my entire blog section. In the templates folder I should create a file named page--blog.tpl.php (note the --double hyphens--) with the layout I want.
Drupal will use the most specific template suggestion it can find so if you wanted you could make one blog post to look completely different than the rest of the site long as you make a template for it named page--blog--blog-post-title and put it in your theme's templates directory. (again, note the double hyphens.)

Categories