How can I display a message only on the homepage of joomla?
I am interested in displaying it on site.com and not site.com/index.php/page or anything else then site.com.
I have tested the following:
<?php $app = JFactory::getApplication();
$menu = $app->getMenu();
$lang = JFactory::getLanguage();
if ($menu->getActive() == $menu->getDefault($lang->getTag())) : ?>this is the homepage
<?php endif; ?>
and this
<?php $menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo "this is the homepage";
}
?>
The problem is that I can still see the message "this is the homepage" on pages like http://site.com/index.php/category/id/78-article which clearly isn't the homepage. It seems that whenever there is index.php in the link, the above code thinks it belongs to the homepage.
This has nothing to do with the 'index.php' in the link. Instead it is related to the fact that the link at http://site.com/index.php/category/id/78-article does not have a menu item associated with it. To do exactly what you are wanting, you will probably need to get a little trickier with the code and check to make sure that the actual page's information matches the homepage information:
$jinput = JFactory::getApplication()->input;
$menu = & JSite::getMenu();
$active = $menu->getActive();
$default = $menu->getDefault();
if (
$active == $default &&
$jinput->get('option') == $default->query['option'] &&
$jinput->get('view') == $default->query['view'] &&
$jinput->get('id') == $default->query['id']
) {
echo "This is the homepage";
}
I am checking the default menu items option (which component) and view and id values against those set in the input.
http://site.com/index.php/category/id/78-article This link will set the id to 78 and likely change the view and option from what it is defined as in the menu for the homepage, so the trigger will not occur.
OK, just an idea but try:
if (preg_match('/index\.php$/',$_SERVER['PHP_SELF'])) {
echo "this is the homepage";
}
You should try this.
1. Check the menu in which has been assigned "Home"(This is the homepage) from the administrator section. Copy the link shown over there.
It would be something like
index.php?option=com_somecomponent&view=someview
Now Goto your index.php in template.
Write a condition there
if(JRequest::getVar('option')=='com_something' && JRequest::getVar('view')=='someview'){ //show message }
This should do the trick for you !! But keep in mind. If you change the menu of homepage, this will have to be changed accordingly.
Related
I am looking to show some content on a page based on the parameter in a link.
If a link is given to a user https://www.examplesite.com/example-page/?client_feeback=1
then they will see the content of the page, if not using the link, then users will not see the content.
Additionally, I need the users of the link to be able to look on other pages and return to the page where the content is hidden/shown and still see the content.
I have set a cookie in functions.php, that will expire in 30days.
code added into functions.php
add_action('init', 'set_feedback_cookie');
function set_feedback_cookie () {
$name = 'client_feedback';
$value= 1;
setcookie($name, $value, strtotime( '+30 days' ), "/example-page/", "examplesite.com", "true" );
}
I have then added the following into the example-page.php template file
<?php
if (!isset($_GET['client_feedback'])) { ?>
<style type="text/css">#form__feedback {display:none!important}</style>
<?php } else { ?>
<style type="text/css">#form__feedback {display:block!important}</style>
<?php } ?>
The cookie is loaded on to the site and the content is hidden/shown when using/ not-using the url link.
What is not working, is the ability to browse other pages on the site and come back to the page with the hidden content and still see it!
Since you confirmed in the comment section that you are not leaving your own domain, You could put the GET variable into a SESSION variable and do your checks based on that. That way, you will always have that available to you until the user leaves the site or you manually kill the session somewhere.
Example:
<?php
session_start();
$_SESSION["client_feedback"] = $_GET['client_feedback'];
?>
As long as declare your session_start(); at the very beginning of your page(s) (literally before ANYTHING else), you can access your session variable anywhere you want.
Now you can perform checks on your session variable with the logic you're trying to achieve in order to display your desired content.
Full Example:
<?php
session_start();
if($_GET['client_feedback'] != "") {
$_SESSION["client_feedback"] = $_GET['client_feedback'];
}
if( isset( $_SESSION["client_feedback"] ) != "" ) {
?> <style type="text/css">#form__feedback {display:block!important}</style> <?php
} else {
?> <style type="text/css">#form__feedback {display:none!important}</style> <?php
}
?>
You can read more about PHP sessions here.
I've got a website running and to go to a different page I'd like to import a list with all different page names (each one in its own line) so I can compare each row with the given page value.
First I look which page it is (index.php):
$get_page = '';
if(isset($_GET['page'])){
$get_page = $_GET['page'];
}
else{
$get_page = 'home';
}
setPage($get_page);
Then I import my 'pages.txt' and set the page (This works in a function) I included before (functions.php):
function setPage($page){
$all_pages = file('pages.txt');
foreach ($all_pages as $line_num) {
if($line_num == $page){
echo $line_num;
}
}
}
I know this won't help me to set the page but I that's not the problem.
pages.txt looks like following:
borrowed
edit
home
new
since I start on my index.php with nothing set $get_page should be home. I tested it and it is. I also echoed out all $line_num's. It works, too. $get_page is 'home' and one entry in pages.txt is 'home'. But if I compare them as shown it doesn't echo 'home' as it should.
With this snippet of code, I'm attempting to show a clickable link (if "admin" is logged in), which will redirect me to adminarea.php
Right now it just prints out "Admin" in text. Nothing to click on. Just simple text.
Am I missing anything? Surely I got it wrong but I cannot see what's missing.
Here is the code:
<?php if (getUser("user") == "admin") { ?>
<option value="adminarea.php">Admin</option>
<?php } ?>
You're printing an option, which is part of the select form input. You're probably looking for an anchor?
Admin
Possibly a better way to do this would be to declare two options for a variable in your PHP first. Something like:
<?php
if(getUser("user") == "admin") {
$adminlink = 'Admin';
} else {
$adminlink = NULL;
}
?>
And in the html:
<?php echo $adminlink; ?>
This would show the href link if the PHP condition was true, and would display nothing if not. Hope this helps!
Well based on your title am assuming you want a link. By the way you can use PHP friend html syntax instead of making the code look "dirty".
<?php if(getUser("user") == "admin"): ?>
Admin
<?php endif; ?>
I have all my navigation stored in an include file. But an issue I've run into is how to set the navigation links to 'active' from the include file?
So that when I'm on the home page, the Home link will be activated. And when on the about page, the About link will be activated, etc., etc. so on for the other pages.
What would be the best way to find out what page is active, and set the link in the include file to reflect what page is active?
Hopefully that makes sense...
You can use an if statement against the URI, and include required files depending on the current page.
if( $_SERVER['REQUEST_URI'] == '/home.php' ) {
include 'homenav.php';
}
else
{
include 'nav.php';
}
Or in the navigation include file
if( $_SERVER['REQUEST_URI'] == '/home.php' ) {
echo '<li class="active">home</li>' ;
}
else
{
echo '<li class="inactive">home</li>' ;
}
I am new in php. I want to show the current page name as my page title. So I used...
<?php echo ucfirst(pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME)); ?>
Now I want to show the page title as "Home" if the "ucfirst" code returns "index" i.e. my index.php. So I tried to write a function.
function pagename() {
$pname = ucfirst(pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME));
if($pname == "Index") {
$pname = "Home";
}
return $pname;
}
But the problem is, it is not showing anything. I know my code is wrong, but I failed to understand where is wrong. Please help.
I tried your code, it's giving me correct output. when i run it, I get "Index" as return value. maybe you should see that you are printing $pname correctly or not in the application :) Good luck