To start out: Not Wordpress! Just plain old PHP. Here's what I'm trying to do:
I've got a horizontal navigation bar at the top of my page with the links 'Home, About, Info, Contact'
Most of the pages also have a vertical navigation bar, the sidebar.
If I'm on the Home page, no sidebar needs to be shown.
If I'm on the About page there has to be a sidebar with various other subjects. The Contact page needs to show a sidebar with a Route Description and Contact Form link etc.
I was thinking about achieving this with $GLOBAL variables and put something like $GLOBAL['sidebar] = 'home' $GLOBAL['sidebar'] = 'contact' etc... on top of every page. In the PHP file that would render my sidebar I would use an if structure to see what sidebar needs to be rendered. But using global variables is something I've always been taught is wrong and shouldn't be used. After that, my mind drifted to $SESSION variables, but that would actually be exactly the same but with some extra concerns like session_start() etc.
I'm self-taught in PHP so I don't know what could be best used to solve this particular (and I presume very common) "issue". Any insights about this matter would be greatly appreciated. Thanks.
The solution will depend a lot on how you've set up your code structure already, but in more general terms the way I usually do it is to:
Include the sidebar as part of your template included on every page.
Set up your sidebar along the lines of this:
<?php
if (sizeof($sidebarModules)>0) {
?>
<div id="sidebar">
<?php
if (in_array('contact',$sidebarModules)) {
// display contact form
}
if (in_array('route',$sidebarModules)) {
// display route description
}
if (in_array('login',$sidebarModules)) {
// display login
}
?>
<\div>
<?php
}
?>
Then at the top of each page make sure you define the array $sidebarModules. Something like:
<?php
$sidebarModules = array(
'contact',
'login',
'description'
);
?>
I'm not sure if this is the best solution, but it's worked well for me in the past.
Edit:
or do it more efficiently if you're templates are named in a standard convention, e.g.:
<?php
if (sizeof($sidebarModules)>0) {
?>
<div id="sidebar">
<?php
foreach ($sidebarModules as $module) { // loop round all modules
include($module.'_tpl.php'); // include module template
}
?>
<\div>
<?php
}
?>
Related
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. :)
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.
Goal - have a section of my sidebar that is conditional depending on what page I'm on. All child pages of the selected page should also show the conditional section.
There are at least two ways to do this - 1) have all the conditional hooks and resulting code in one sidebar file and, 2) have multiple sidebar.php documents and call them based off what pages are being visited.
I've pretty much failed at both ways so far... :( I'm working on the second method right now though, because I think it might be more flexible long term. Anyway, in my page.php I replaced:
<?php get_sidebar() ?>
With
<?php
if (is_page(1997) || $post->post_parent) {
get_sidebar('sidebar-test-may12.php')
}
else { get_sidebar()
} ?>
With the hope of testing the method. What I want to happen is that if the page is page id 1997 or any of its child pages please show sidebar-test-may12.php. Otherwise show the regular sidebar. With the new code the whole page goes blank. Any suggestions or solutions for me? Seems like a common problem, but I haven't been able to figure it out yet. Thanks!
You have a few problems with your code example:
1) $post->post_parent is not being checked against anything else, so it will only return true if it is a child page (not necessarily the child of the page you want to target)
2) get_sidebar() is being called incorrectly. If you want to get 'sidebar-test-may12.php' from your theme folder, you need to call get_sidebar('test-may12')
3) You're missing semicolons after your function calls
So your code should look like this:
<?php
if(is_page(1997) || $post->post_parent == 1997) {
get_sidebar('test-may12'); //get sidebar-test-may12.php
}
else{
get_sidebar(); //get sidebar.php
}
?>
Let me know if that helps.
UPDATE: Bear in mind, $post->post_parent does NOT get the top-most ancestor ID of a child page. If you want to grab the top-level ID regardless of depth, consider doing this:
<?php
$ancestors = get_ancestors(get_the_ID(), 'page');
if(is_page(1997) || end($ancestors) == 1997)
get_sidebar('test-may12'); //get sidebar-test-may12.php
else
get_sidebar(); //get sidebar.php
?>
POSSIBLE SOLUTION: Building off your example and my proposed ancestry check, one thing you can do is have your template check to see if a special sidebar exists in your theme based on the parent page's slug. This way, if you decide a particular page needs a special sidebar for it and all of its children/grandchildren/great-grandchildren/etc. you just need to add it into your theme with a name of 'sidebar-{parent_slug}.php'. So:
<?php
$id = get_the_ID();
$ancestors = get_ancestors($id, 'page');
$top_page = $ancestors ? get_page(end($ancestors)) : get_page($id);
if(locate_template('sidebar-'.$top_page->post_name.'.php'))
get_sidebar($top_page->post_name);
else
get_sidebar();
?>
This way, you don't need a ton of conditionals to decide which Sidebar file to load on a general Page template.
Use some error trapping to show what's going on with your php, either toggling an error log in .htaccess of a simple widget: http://wordpress.org/extend/plugins/error-log-dashboard-widget/
I've hardcoded logic for sidebars, but I've also used WordPress › Widget Logic « WordPress Plugins for my own and client sites. It's easy to use with a simplified version of WP conditionals, like is_category and is_page ('101')
For someone else tackling this same problem -- there is a new plugin out that solves this problem very well. It even makes creating the sidebars easy. You can check it out here:
http://wordpress.org/extend/plugins/custom-sidebars/
I've inserted the following code to the template loop (in the correct place), but it is not outputting any comments. Why?
<?php
$withcomments = true; // force comments form and comments to show on front page
comments_template( '', true );
?>
I'm trying to display comments for each post on the main-home-page stream of posts.
I'm using the Twenty Ten theme.
Try this before the <?php endwhile; ?> of the loop in loop.php:
<?php
$withcomments = "1";
comments_template();
?>
Try this:
<?php global $withcomments; $withcomments = 1; comments_template(); ?>
There is a much simpler way that doesn't involve editing PHP code. First make sure you can create comments on other pages and if that is working, go back to the home page.
On the top right, click the gear icon to show settings and near the bottom of settings change the "page attributes" "template" from "front page template" to "default template". Save and you will have comments.
However, you may lose other features of the home page (you can always change the template back). For me I didn't lose anything.
Understand that a normal WordPress blog is intended to have comments on the blogs (posts), but not on the home page. By default you shouldn't even have comments on any pages (just posts), but that is easily enabled. That is why normally there aren't any comments allowed, but if you have a one-page site this is a problem.
Also note that there are many different themes and some do allow comments on the home page.
I want to use the if statement to select specific pages that have <body class="section-category"> and output custom content for only those pages.
My PHP is not very good and I hope this is a very simple task to do. Can anyone give me a tip?
I would not check for the existence of a certain body class. Those classes are only the results of other if-else logic and they can easily be overridden or altered, breaking your page template. It's better to check the values those classes are based upon. If I were you, I would try to figure out how this body class was generated and re-use that code.
For example, if your theme's template.php does something like this:
$body_classes[] = 'section-' . form_clean_id(arg(0));
Then I would put this in my template.php:
<?php if (form_clean_id(arg(0)) == 'category'): ?>
// Do fancy stuff!
<?php endif; ?>
It sounds like you want a flag available for certain pages; one that you can read out and act upon in your theme.
In template.php:
function YOURTHEME_preprocess_page(&$vars) { //Many themes already have this function implemented
if (stripos($vars['foo-bar'], 'section')) {
$vars['is_section'] = TRUE;
}
else {
$vars['is_section'] = FALSE;
}
}
Then in your page.tpl.php
<?php if ($is_section): ?>
<p>I am in a section</p>
<?php endif; ?>
That way you keep logic where it belongs: in the preprocessors. And you keep logic out of the template, where it most certainly does not belong!
The if (stripos($vars['foo-bar'], 'section')) { can most probably be made a lot smarter, if you do a var_dump($vars) there, all available variables will dump on your screen (browser). I am certain you will find a variable that you can check against, and that is less 'fuzzy' then a body class. After all: that class is meant for one thing only: to serve as class in the body-tag. And not for checking if you are in some section.
You have the full Drupal available there too, so you can even use any function that (e,g, the section) modules provide.
I would look into a module like Context or Panels to do this- it will serve you better in the long run than coding it in.