How to set background on active page on included menu? - php

I have a menu but I'm including it on every page because I don't have to copy and paste the whole code on every page. So I'm including it by this way
require "menu.php";
This is what menu.php contains
Home
.....
News
And I want when I'm on Index page the background of the link in the menubar to be gray for example and not just for the index.php but for every other page. I know how to do it when I'm not including it, just add class and I stylize the class. But now I can't think how it could happen.
I would be appreciated if someone help me.
Thank you in advance

The way I would do it would be to add a class to your main <body> (or other parent container) tag that indicates the current page:
<body class='page-home'>
Then add classes to every single menu item:
Home
.....
News
Now you can write CSS styles that target your menu links differently depending on the current page:
.page-home .nav-home {
/** styles for the home link when you are on the homepage **/
}
Now you're handling all your styling in pure CSS, and it doesn't matter that your menu is an include. It's just up to you now to decide how much styling you want to do based on the current page and the specific menu links.

You can use this tricks:
get your file by file_get_contents() and replace your class name according to your file name.
In your menu.php file:
return
'<a class=[+index] href="index.php">Home</a>
.....
<a class=[+news] href="news.php">News</a>';
In your index.php
$strMenu = file_get_contents("menu.php");
$strMenu = str_replace('[+index]','is-active',$strMenu);
echo $strMenu;
In your news.php
$strMenu = file_get_contents("menu.php");
$strMenu = str_replace('[+news]','is-active',$strMenu);
echo $strMenu;

Related

Hide component div from home page in joomla 3

I have a RocketTheme Affinity template that when it hides the component the wrapper stays untouched and i want it removed also.
I only want to disable it from the home page (id=101).
In the "index.php" the bellow code calls this:
<?php
echo $section_rows->render();
?>
The div name is "section-row3" in a file called "rt_sectionrows.php"
There are also css files called style6 and tempate.css files.
Conclusion:
How do I hide certain div and class from homepage?
Try this,
<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault()) {
?>
<script type="text/javascript">jQuery(document).ready(function(){
jQuery('.section-row3').hide()
});</script>
<?php
}
?>
Add this code at the bottom of your home page. It hide that div.
Hope it helps.
You should be able to just do this with some CSS.
Go into the Joomla! Administrator, Menus, Select the menu which contains your homepage menu item and then edit it.
In the menu item, select 'Page Display'. At the bottom you will see: Page Class.
Add a space and type: homep
Then add:
.homep .section-row3 {display: none !important;}
To your Rocket Theme Custom CSS or LESS file.
Then that will just hide it with CSS, we have specified !important, so it takes priority.

Dynamic title depending on page (DRUPAL 7)

Before I made the switch to Drupal (previously just static HTML), I had a div title in my header that changed depending on what page the user was on using the following code:
ABOUT PAGE
<?php
$title = "ABOUT US</span>";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path); ?>
The reason this worked, however, was because I was using static HTML... which means I had every page set to use its own Index.PHP, located at (for the About page, for example) public_HTML/about/index.php. Therefore, I could edit each page individually to my liking by editing its respective Index file...
Now, with Drupal.. instead of having so many Index.PHP files like I did before (which was messy IMO...), I want to have every page use the same page.tpl.php (with the exception of the front page, which uses page--front.tpl.php).
Can I somehow modify the above PHP code to display a different title using IF statements depending on what page the user is on?
For example... (I have no idea how to PHP code so this is just to give you experts an idea of what I would want..)
Ifpage=about, $title=About Us;
Ifpage=contact, $title=Contact Us;
Could this code all be in the same page.tpl.php?
Thank you so much!
Drupal 7 has a title field for each content type. Why don't you use that?
It shows by default and you can change it for every node.
It is this code in your template file.
<?php print render($title_prefix); ?>
<?php if ($title): ?>
<h1 class="title" id="page-title">
<?php print $title; ?>
</h1>
<?php endif; ?>
<?php print render($title_suffix); ?>
$title_prefix (array): An array containing additional output populated by modules, intended to be displayed in front of the main title tag that appears in the template.
$title: The page title, for use in the actual HTML content.
$title_suffix (array): An array containing additional output populated by modules, intended to be displayed after the main title tag that appears in the template.
See https://api.drupal.org/api/drupal/modules!system!page.tpl.php/7 for all the variables available in your page.tpl template file.
If you want to style one part different from the other you could use an extra field for it.
Or if you need to do something with the value (I wouldn't suggest this!!!).
switch ($title) {
case "About Us":
// Do something with the title
break;
case "Contact Us":
// Do something with the title
break;
case 2:
// Do something with the title
break;
}
Here's a simple approach:
Add a new field for the content type
Call it title2
Render the two fields
Use css to inline align them and add styling
Here was my solution since I am unable to position a Drupal-based title block right next to a non-Drupal based logo DIV..
I created a div called .headertitle and positioned/styled it how I want.
My header.php is an PHP include file which is called by each page and the headertitle has a PHP condition..
<?php if(empty($headertitle)) $headertitle = "ASK REAL QUESTIONS, <br><span class='white'>GET FREE ANSWERS.</span>";
echo $headertitle; ?>
That way, if no text is declared for the .headertitle, it takes the default form of "ASK REAL QUESTIONS, GET FREE ANSWERS."
...Now, I had to figure out a way to actually declare text for the .headertitle depending on what page I was on...
I did this by using a custom template for each page (node) by using this file.. "page--node--nodeid.tpl.php".
So, for my ABOUT US page, I created a tpl.php file called "Page--node--7.tpl.php".. for this page, the 7 is the nodeid which is easy to find.
Then, I added the following PHP code to declare text for the .headertitle specifically on the ABOUT US page which looks like this...
<?php $headertitle = "ABOUT<span class='white'>.US</span>"; include('includes/header.php'); ?>
DONE AND DONE.
I'll do that for each page. :)

Setting current page menu as active

I downloaded menu from:
http://www.stunicholls.com/menu/pro_dropline_2.html
And used in my application.
But when I am clicking on a menu it is not highlighting as active menu.
That means i want give an active class to a menu which i clicked .
How can i do this by using this menu code.
My application is in php.
You could accomplish this using the dirname function or FILE global variable to get the file name/path. Thereby, being able to find which tab should be highlighted.
Imagine this directory tree
html_root/
video/
file.php
music/
anotherfile.php
...
Say that each tab is organized into its own directory.
Inside your header file
<?php
if (dirname(__FILE__) == "video") {
// Set css to video tab
} // etc.
?>

One file, displaying code on one page, but excluding on another?

Can anyone advise how I'd alter a template which is called on every page load to display a particular section on just the one page?
I hope I explain this well enough...
index.php includes;
require_once('./cache/templates/sidebar.php');
Every subsequent page is built uses what's defined in this index.php file, meaning the sidebar.php is a must.
I'm wanting to edit sidebar.php to contain an advert which displays solely on the index page.
At the moment, when I edit sidebar.php, so for instance, to display the letter "B", it will display on the homepage, and every other page like so;
Index Page: http://img.photobucket.com/albums/v684/nilsatis/1stack.jpg
Every other page: http://img.photobucket.com/albums/v684/nilsatis/2stack.jpg
How can I dictate one area of an included file to display on one page but exclude showing on others?
Edit: Thanks very much for your time. It's really appreciated.
I inherited/purchased the website and I'm finding the index.php file very temperamental.
I'm attempting to put this code within the sidebar (or below it, above the footer) but any amend on index.php breaks it.
}
if (isset($url[1])) require_once('./cache/html/'.$url[0].'/'.$url[1].'.php');
else require_once('./cache/html/'.$url[0].'.php');
}
}
require_once('./cache/templates/sidebar.php');
}
require_once('./cache/templates/footer.php');
A quick and dirty hack would be inserting a custom CSS stylesheet on the index.php page that makes some div that's part of "sidebar.php" visible.
Example:
sidebar.php:
<?php
echo '<div id="theLetterB" style="display:none">B</div>';
?>
index.php:
<?php
if ($_SERVER['PHP_SELF'] == 'index.php') // Replace this with something to differentiate between pages
{
echo '<style type="text/css"> #theLetterB { display:block; } </style>';
}
?>

Joomla : How can we assign a different layouts to different menu IDs?

I am running Joomla and seeking you help for the following issue.
Lets say I have 3 layouts in my template and the layout files are named as...
index.php
index2.php
index3.php
I have 5 menu links say....
Link 1
Link 2
Link 3
Link 4
Link 5
What I am looking for is......
For Link 1, Link 4 and Link 5, I want Joomla to load the regular index.php but for Link 2 I want Joomla to load index2.php and similarly for Link 3 I want it to load index3.php.
What I mean is... How can we assign a different layouts to different menu IDs?
I know there is an inbuilt option to choose a different Template based on menu ID but I do not want to duplicate the template files just for this one function. Everything in my templates is the same just the change is in the layout depending on the Menu ID.
Kindly Help.
Are you using a commercial template or something custom? You should be able to code your index.php so that the layout is determined by the modules loaded on the page. You then control what modules show up by the menu assignments in the module parameters. You can control the layout being displayed through CSS, Page Class Suffix, and the code on index.php.
Every module position in your template should be collapsible - meaning that if no modules are loaded to the position, it does not get added to the HTML. Use something like this:
<?php if ($this->countModules('left')) : ?>
<jdoc:include type="modules" name="left" style="xhtml" />
<?php endif; ?>
You can also use a combination of the Page Class Suffix that you can set on the System Parameters of a menu item and CSS to control the layout of the page. I add the Page Class Suffix to the BODY tag of my templates so I can control every page individually.
First, you need to figure out which menu item you are on:
<?php
$menu = &JSite::getMenu();
$active = $menu->getActive();
if (is_object( $active )) :
$params = new JParameter( $active->params );
$pageclass = $params->get( 'pageclass_sfx' );
endif;
?>
Then you need to add that to the BODY tag as an ID:
<body id="<?php echo $pageclass ? $pageclass : 'default'; ?>">
Now you can use module positions and CSS to control every page. You can achieve vastly different layouts without having to go back in and touch code.
I always use include_once or for security purpose require_once, from my point of view it is a better way of programming in the template process.
- What do you think ?
- Example I would do this way :
(isset($_GET['Itemid']))?$itemID=$_GET['Itemid']:"";
OU POUR LES PURISTES DE JOOMLA :
$itemID=JRequest::getInt('Itemid',0);
if($itemID == '57')
{
require_once ("index1.php");
}
if($itemID == '58')
{
require_once ("index2.php");
}
else
{
// template code of index.php
}
On the basis of your menu ID (ItemID) you can include a different index<x>.php in your main index.php, like so:
$itemID = $_GET['ItemID'];
if($itemID == '57')
{
include index1.php
}
if($itemID == '58')
{
include index2.php
}
else
{
// template code of index.php
}

Categories