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

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>';
}
?>

Related

Get_Header not working on Home Page Wordpress - Tried Almost Everything

At my website: www.TattiniBoots.com, I would like to add a larger header than what exists on all other pages. Below is the code that I am using
<?php
if (is_front_page() ) {
get_header( 'home' );
} else {
wp_head();
}
?>
When removing the else function, all other pages lose the header except the home page; and therefore I am sure that the call script is working appropriately
My new header file that I am calling is: "header-home.php" which includes the col-md-12 instead of col-md-3. However, it does not display
Even when removing all code inside "header-home.php" the original old header will still display on the home page
What am I doing wrong?
I simply need one line of code to be altered on this index page (col-md-12 instead of col-md-3)
You can do this programmatically without having to create a different header-home.php. Following is the code.
if (is_front_page()){
$header_home_class = "col-md-12";
} else {
$header_home_class = "col-md-3";
}
//whereever you want to add the class name
<div class="<?php echo esc_attr($header_home_class);?>">

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. :)

Conditionally display menu for page type in Wordpress

I am relatively unfamiliar with Wordpress and I am creating a custom theme for a client. I would like to either display or remove the main menu depending on the page type. I have researched several options like removing the navigation from header.php and referencing it separately and also making the menu conditional which is preferable.
I have a custom page type in my theme called 'landing page' on which I would like the menu to be never be displayed, though it will be on every other page. Ultimately there will be a lot of these and I would rather I didn't have to intervene.
I would rather not duplicate my header.php file but I can only find reference to displaying the menu conditionally like below by page name or ID which seems ridiculous.
<?php
if (is_page('contact')){
<?php include(TEMPLATEPATH.'/headerA.php'); ?>
}
elseif (is_page('gallery')){
<?php include(TEMPLATEPATH.'/headerB.php'); ?>
}
else {
<?php include(TEMPLATEPATH.'/headerA.php'); ?>
}
?>
Rather than including files as above, I will put the whole thing into my header and just make the navigation conditional. Does anyone know how I should approach this using my custom page type landing page rather than by page name so every page created with that type will never have a menu?
Thanks
Are you talking about a Custom Post Type (CPT) or a page called landing-page?
They are completely different. See http://codex.wordpress.org/Post_Types
In any event, this will work for a custom post type or a page:
if ( !is_singular( 'custom-post-type-name-or-page-slug-here' ) ) {
get_template_part('menu');
}
It says: "If this page is not a single page or a CPT, load the file menu.php from the theme folder."
See also http://codex.wordpress.org/Include_Tags:
The get_template_part() tag includes the file {slug}.php or
{slug}-{name}.php from your current theme's directory, a custom
Include Tags other than header, sidebar, footer.

How to set background on active page on included menu?

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;

How can i check if there is any content in them main page of joomla?

i am using joomla 3 for a web site, i don't use any content in the main page but the system output is on because i need to output contents on other pages of the site. the problem is i have a space in the middle of the page where normally the content comes. i know where this comes from but i have to disable it if there is no content on that selected page. i tried to check the
$this['template']->render('content')
which is actually used to render the specific content. but i was not able to use it in a conditional if. so i need to check if the loaded page has a content output. has anybody a idea how i could do that?
PS: i dont want do this with css.
You may check if you are showing the main page and render conditionally:
<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault()) {
$this['template']->render('content');
}
?>
For multilingual site check this link for detecting if you are on the default page:
http://docs.joomla.org/How_to_determine_if_the_user_is_viewing_the_front_page
Just curious... why don't use the <jdoc:type="component/>?

Categories