How To Select Template Based On Language Switch? - php

I have a Joomla 2.5 multilanguage website and I want to have a different banner on the English version and on the French version. I know that I can duplicate the template and assign them to the specific language but I am doing a lot of modification in the CSS and I don't want to double the changes!
I did add this condition in my index.php but only the first banner will display on the EN and the FR website.
<?php if ($this->language = 'en-gb') : ?>
<div style="background-image: url(/templates/mega_calibra/images/bannierEN.png);"></div>
<?php elseif ($this->language = 'fr-fr') : ?>
<div style="background-image: url(/templates/mega_calibra/images/bannierFR.png);"></div>
<?php endif; ?>`
What am I doing wrong?

Basic PHP error
if ($this->language == 'en-gb')
I think $this->language will return en-GB and fr-FR (note the uppercase) so check with a var_dump() on $this->language.

Related

How to set a title in Cell/Header directory in cakePHP ?

I am using CakePHP 3.0 and I am using inside the Cell/Header directory a display for the header.
<div id="header">
<div class="header-title">
<h2><?php echo (isset($pageTitle)) ? $pageTitle : 'Test'?></h2>
</div>
</div>
This code snippet runs perfectly and on every page.
I am trying to set the $pageTitle dynamically. This means that I am trying to set it for every page differently.
I have set inside the UsersController.php
$this->set('pageTitle', 'Nebojsa');
In index method.
But when I go to the url /users/index , this page title stays 'test'.
I have also tried to assign the value inside the Template/Users/index.ctp
<?= $this->assign('pageTitle', __('Users')); ?>
But it won't work.
What am I doing wrong ? Could you maybe point me in the right direction, where should I look ?
Between view templates and layouts you can use:
<?= $this->assign('pageTitle', __('Users')); ?>
And get the value back in any other template or layout using:
<?= $this->fetch('pageTitle') ?>
As far as setting the title from the controller your code is correct.
$this->set('pageTitle', 'Nebojsa');
<?php echo (isset($pageTitle)) ? $pageTitle : 'Test'?>

PHP content does not switch to other language anymore (Polylang)

Hello I m using Polylang and this php code to switch my slider (I did with Smart Slider) to english-french. I don't know anything about PHP but I got it on a forum. It was working perfectly until yesterday.
http://www.chooseyourtelescope.com/
<?php
$currentlang = get_bloginfo('language');
if($currentlang=="fr_FR"):
?>
<div>
<?php
echo do_shortcode('[smartslider3 slider=6]');
?>
</div>
<?php else: ?>
<div>
<?php
echo do_shortcode('[smartslider3 slider=4]');
?>
</div>
<?php endif; ?>
Now it stays in english. I dont think the problem is coming from Smart Slider. I tried to deactivate the other plugins and to restore the previous theme and Polylang versions but nothing changed.
BTW my site is responsive and the slider is not displaying under 800px width. So you can't see the problem on mobile for example.
Instead of doing it with php I resolved the problem with css: I put both english and french sliders in my header template with different classes and "display:none" the slider I don't want for each frontpage:
.page-id-94 .smartslider-en {
display:none;
}
.page-id-7386 .smartslider-fr {
display:none;
}
Please answer if you find a solution with php only.
Here s the solution :
<?php if(get_locale() == 'fr_FR') : ?>
Actually the "get_bloginfo('language')" function stopped working since I upgraded to WordPress 4.6 and to Polylang 2.0.3.

Display DIV only on specific subpage

I'm trying to display a Joomla module or a div, but only on specific subpages.
Example: trying to display this
<div><?php echo $this['widgets']->render('module'); ?></div>
Only on www.example.com/products/item1 and other subpages of /products
But NOT on:
www.example.com/
or
www.example.com/products/
I can't find a PHP or JS script to do that, nor is it possible to do it with the native Joomla features.
How to do that?
You can use preg_match on the $_SERVER['REQUEST_URI']:
<?php if (preg_match("/products\/.+/", $_SERVER['REQUEST_URI'])): ?>
<div><?php echo $this['widgets']->render('module'); ?></div>
<?php endif; ?>
The expression /products\/.+/ sees if products is in the url and a slash and some content follows it.

Can a Joomla module “know” what position it's in? (2)

This is my very first question at StackOverflow.
I was trying to post comment in the post Can a Joomla module "know" what position it's in?, but could find no way to do that so I have to post another question here. Any tips regarding how to do this properly is greatly apprieciated.
Anyway, here is my questions:
I tried the code mentioned in the above post but it seemed didn't work. I'm not only quite new to PHP, but also coding, so I'm not sure if it's me or the code. here's what I did to the module's default.php file:
1)to ensure I insert the code at the right place, I insert
<?php echo(print_r($module)); ?>
and it output 1 at the right position;
2)the position-name that I need to determine is "showcase-a", so I insert code
<?php if ($module->position == 'showcase-a'):?>test<?php endif;?>
to the above place, but this time it does't show anything;
3)then I tried this code:
<?php if ($module):?><span>test</span><?php endif; ?>
But still it does't display "test" at the position as I expected.
4)I tried
<?php if (1):?><span>test</span><?php endif; ?>
and the test "test" displays as good. So I didn't code the IF statement wrong.
Now I'm totally lost. Since print_r($module) outputs 1, $module must be positive, why PHP ignore test in 3)? This is just a side question for the sake of PHP learning. What I still need to solve is let the module determine which position itself is in.
Please help. Thank you!
I'm not sure about using it in the template (although it doesn't seem wrong altogether) but my modules oftentimes access the position like this:
$module->position
in the module (so mod_something.php) so try to put it there, if it's available just set a variable and it will be available in the view too.
If $module->position didn't work for you. $module->position may only work for joomla 1.7, 2.5 and + but i'm not sure. $module->position works in joomla 2.5.
Otherwise you need to make a module.php function
Check out the file
/templates/you-template/html/modules.php
For example you make your position the following in your template (for example index.php)
<jdoc:include type="modules" name="header" style="includeposition" />
inside of
/templates/you-template/html/modules.php
You make a function like this :
<?php
function modChrome_includeposition($module, &$params, &$attribs){
//Set a value to $module->position
$module->position='WHATEVER YOUWANT';
?>
<div class="moduletable <?php echo $params->get('moduleclass_sfx').' '.$module->name; ?>">
<?php
if($module->showtitle){
?>
<div class="modtitle">
<div class="fleche">
<span>
<?php
echo $module->title;
?>
</span>
</div>
</div>
<?php
}
?>
<div class="modcontent">
<?php echo $module->content; ?>
<div class="clear">
</div>
</div>
</div>
<?php
}
?>
make different functions names that sets different values if needed.
the function name will match the syle you set:
modChrome_includeposition($module, &$params, &$attribs)
because
style="includeposition"
inside of your jdoc.

Load different div at home template in wordpress

I am working on a custom wordpress homepage.
I want that it is loading in a different div in the header.
The header now loads the #content div for every page. But i want to let it load #contenthome div if the homepage loads.
I'm using this php-code:
<?php if (is_page_template('template-home.php')) { ?>
<div id="contenthome">
<?php } else { ?>
<div id="content">
<? } ?>
I'm fairly new to php. so i hope you guys can help me :)
Thanks a lot.
Or this:
if(is_home())
{
<div id="contenthome">
<?php } else { ?>
<div id="content">
<? } ?>
Personally, I prefer writing things in shorthand when it comes to cases like these. Consider also using this to shorten your code:
<div id="<?php echo is_home() ? 'contenthome' : 'content'; ?>">
ADDITIONAL NOTES: If your <body> tag has the Wordpress body_class() added to it, you can pretty much target individual pages with CSS, even when they're using the same template. The homepage, like any other page, will have a unique body class applied that will then allow you to target that particular page.
So if your issue is simply a matter of being able to target any given page regardless of template with CSS, you won't necessarily NEED to apply any unique divs to your page. For example:
<style type="text/css">
#content{display:block;}
.home #content{display:none;}
</style>
This will hide the content div only on the homepage. It's probably not what you want it to do, but you can see how the page itself is being targeted to override the default behavior.
UPDATE: As per your latest question, if you need another particular page to use that conditional, use is_page() like so:
LONG CODE:
if(is_home() || is_page('posts_page_title'))
{
<div id="contenthome">
<?php } else { ?>
<div id="content">
<? } ?>
SHORTHAND:
<div id="<?php echo is_home() || is_page('posts_page_title') ? 'contenthome' : 'content'; ?>">
If you prefer, you may also use is_page('posts_page_ID') or is_page('posts_page_slug'). Play around with it and see what works best for you. More information here: http://codex.wordpress.org/Function_Reference/is_page
With reference to this post Try this one....
$pagename = get_query_var('pagename');
if ($pagename = 'template-home.php') {
<div id="contenthome">
<?php } else { ?>
<div id="content">
<? } ?>
it seems like you already created a file in your theme just for the homepage and you called it template-home.php
from this point you have two options:
1) you can just define a constant (or variable) and decide what div to use only when it is defined; if you will use a variable remember that in header.php you are in fact inside a function (get_header) and you will need to use global to have access to it
2) if you foresee any other differences between the structure of the header between the main page and the rest of your wordpress you could use another file for the header. to do this, in your template-home.php file give a parameter to the get_header function, like get_header('home')
if you do this header-home.php will be loaded instead of header.php

Categories