Hide content on a sidebar depending on the page - php

I have a website with the following setup:
overview.php
hotel1.php
hotel2.php
hotel3.php
Where overview has a sidebar with links to hotels 1, 2 and 3 and each hotel page has a sidebar with links to the other two hotels (excluding itself) along with the overview page.
At the minute, I am hard coding the sidebar in each php page, as they are all slightly different.
I am wondering if there is a way I can code one sidebar in a separate file (sidebar.php) with links to each of the four pages and add the sidebar with a php include() function.
Then, depending on the page (which will have an identifier), show all the links, except the link to itself.
Problem is, I'm not sure how to do it, or if it can be done.
The site is php and html (with css and javascript).
And if it is relevant, I have about 100 folders containing an overview and then multiple hotels which I would like to implement this on.

The easiest way to do it is to wrap your sidebar code in a function.
sidebar.php
<?php
function print_all_sidebar_links_except_self($self = NULL){
$hotels_text = array(
"hotel1" => "Gryffindor Tower",
"hotel2" => "Ravenclaw Tower",
"hotel3" => "Slytherin Dungeons"
);
if (isset($self)){
unset($hotels_text[$self]);
}
foreach ($hotels_text as $page => $name){
echo "<a href='".$page.".php'>".$name."</a>";
}
}
?>
overview.php: print_all_sidebar_links_except_self();
hotel1.php: print_all_sidebar_links_except_self('hotel1');
hotel2.php: print_all_sidebar_links_except_self('hotel2');
hotel3.php: print_all_sidebar_links_except_self('hotel3');

Related

Website Navigation Bar: Displaying webpages while retaining the highlighted menu

Sorry if the title seems confusing or anything... I can't figure out what to put..
I'm actually a newbie but I've tried building a simple website in php before but back then I was just copying my navigation code to my every page and put 'current' to it so everytime, for example you are in the HOME page, the menu HOME in the nav bar is highlighted... now I tried following this tutorial to build my nav bar>http://dbwebb.se/kod-exempel/dynamic_php_menu/
I've got it but my problem is I don't know how to display my other pages when clicking the menu item...
I tried changing the 'url' to product.php (my products page)
<?php
$menu = array(
'home' => array('text'=> '<img class="brighten"; src="style/images/home-hover.png"/>', 'url'=>'?p=home'),
'products' => array('text'=>'Products', 'url'=>'products.php'),
'about' => array('text'=>'About', 'url'=>'about.php'),
);
?>
It displays the product page but the menu item 'Products' is not highlighted
btw my navigation bar is a separate php file and I just 'include' it in my other pages...
So how can I highlight the current choice while navigating to my 'Products' page?
I apologize if my question and explanation of the problem is confusing..
You can do it by simply check the file you pass as variable to select thr page. And if the current page is products for example. You add this class (I didn't viewed the link you gave, that's just the way I do it ..

If category/page in wp_nav_menu exist

I need your help
I have three custom menu(wp_nav_menu) which I customize via admin menu section.
<!-- first menu ->
<?php companyMenu(); ?>
<!-- second menu ->
<?php servicesMenu(); ?>
<!-- third menu ->
<?php partnersMenu(); ?>
I want to show only one nav menu to which opened post/page/category belongs
For example: when I'm on home page click "Contacts" in menu it redirects me to "Contacts" page and because this page defined (with other menu links) in companyMenu() wp_nav_menu function it shows
It would really depend on how many pages you would need to check against for my solution to be viable- the fewer pages the better.
You could wrap your menu code in if statements and use the is_page wordpress function to check if you are on that page. See the link below for more information.
http://codex.wordpress.org/Function_Reference/is_page
Code Example
if(is_page( 'Contact' )){
servicesMenu();
}
As a general rule the wordpress codex has a great wealth of knowledge which I found really helpful when starting wordpress development

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.

In Wordpress, how can I change the destinations of my links dependent on what page user is on?

I have the problem that I am using a theme where the navigation is simply 'scrolling' to a given part (eg. #contact) when pressed. However, I have implemented some seperate subpages that exits this scrollable page and thus renders the navigation ineffective.
My question is, how can I change the destination of my links or maybe change the menu entirely when users are on these subpages?
I should clarify that all the pages that need 'the new' navigation use a different page template called full_width.php. But since it is using an include header function I can't just replace the navigation.
Thank you for your time!
Here is simple solution based on templates.
if ( is_page_template('full_width.php') ) {
//menu for full width page
} else {
// Returns false when 'full_width.php' is not being used.
wp_nav_menu( $args );//read for args in codex to make menu you want.
}

Different sidebars per page subject

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

Categories