How do you hide specific content if $page = name - php

How do you hide specific content if $page = name
For example:
<?php
if ($page=='special'){
echo "<div>hello</div>";
}
?>
The above example will show the div if the $page = special. How do I do the opposite of this, hide a specific div if the $page = something?
Edit:
To be more specific I would like to hide my main navigation when on the $clients page.
Do I wrap the <nav> with PHP or is it possible to hide a specific div if I give it a name, for example. <nav id="clients"> the PHP would be: if $clients then hide the id named clients.
I should also mention that the content in question has <?php echo $url; ?> and the likes contained within it.
This is the exact content I would like to hide on $clients pages.
<nav>
<ul>
<li><a <?php if ($page=="work") echo "class=\"current\"" ?> href="<?php echo $url; ?>" title="Work">Work</a></li>
<li><a <?php if ($page=="profile") echo "class=\"current\"" ?> href="<?php echo $url; ?>profile/" title="Profile">Profile</a></li>
<li><a <?php if ($page=="approach") echo "class=\"current\"" ?> href="<?php echo $url; ?>approach/" title="Approach">Approach</a></li>
<li><a <?php if ($page=="contact") echo "class=\"current\"" ?> href="<?php echo $url; ?>contact/" title="Contact">Contact</a></li>
</ul>
</nav>

hide a specific div if the $page = something?
if ($page !='special'){
echo "<div>hello</div>";
}
You just echo it if $page is different from something.
If you want, you can also echo it anyway but as 'hidden' if that's what you're trying to achieve.
if ($page =='special'){
echo "<div>hello</div>";
} else {
echo "<div style='display:hidden;'>hello</div>";
}
This way, the div will be in the DOM anyway and you can show it later on without reloading the page using JavaScript.

If you want it on every other page that is not special
<?php
if ($page!='special'){
echo "<div>hello</div>";
}
?>

Related

How to Embed HTML in a PHP Wordpress Navigation?

My wordpress navigation code is as follows:
<?php } ?>
<?php if (get_option('fudge_speakers_widget_menu')) { ?>
<li><a title="<?php echo get_option('fudge_speakers_widget_menu'); ?>" href="<?php bloginfo('url'); ?>#speakers"><?php echo get_option('fudge_speakers_widget_menu'); ?></a></li>
<?php } ?>
<?php if (get_option('fudge_sponsors_widget_menu')) { ?>
<li><a title="<?php echo get_option('fudge_sponsors_widget_menu'); ?>" href="<?php bloginfo('url'); ?>#sponsors"><?php echo get_option('fudge_sponsors_widget_menu'); ?></a></li>
I need to add in a separate page that is not from a widget. Just www.example.com. I tried <li>www.example.com</li> but that failed.
How do I add the external widgetless nav-link in the PHP file?
I don't know if it helps, but this is using the Fudge theme
<li>
is not a link but a list entry.
Click Me!
would be a link.
For www.example.com to appear on the li list, you will need to change
<li>www.example.com</li>
to
<li>www.example.com</li>
The second www.example.com can be change to whatever text you want displayed

Wordpress php else if statement with advanced custom fields

I'm trying to make an if-else-statement which works by going if there is a link print it around the name.
I have the below code which is almost there. However, the link is being printed above the text ranther than being an actual link.
<?php if( the_sub_field('corporate_link') ){ ?>
<a href="<?php the_sub_field('corporate_link'); ?>"
target="_blank"
title="<?php the_field('corporate_name'); ?>"><?php the_field('corporate_name'); ?></a>
<?php } else { ?>
<?php the_sub_field('corporate_name'); ?>
<?php } ?>
Any thoughts on how to make it link instead of printing the link if it`s there?
So what im looking to acheive is if there is a link print this
Coprate Name
If there isn't a link it just shows the corporate name.
use get_sub_field('corporate_link') instead of the_sub_field('corporate_link')
<?php
$corporate_link = get_sub_field('corporate_link');
$corporate_name = get_sub_field('corporate_name');
if( $corporate_link != '' ){ ?>
<a href="<?php echo $corporate_link; ?>"
target="_blank"
title="<?php echo $corporate_name; ?>"><?php echo $corporate_name; ?></a>
<?php } else { ?>
<?php echo $corporate_name; ?>
<?php } ?>
use get_sub_field() and get_field() instead of the_sub_field() and the_field()

<a href> get value in php

The current page someone is in on my website is highlighted on the navigation menu (the class "active"). I've done this through php, which compares the current page to a string. I'd like to simplify it even further because this string is actually the href value of the menu item.
Is there a way to get the href value of this item via php? That way I could just use that variable instead.
My php code:
<?php
function curPageName()
{
return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);
}
function activeMenuItem($href)
{
return (curPageName() == $href) ? "class=\"active\"" : "";
}
?>
and the html in the body:
<ul id="navilist">
<li><a href="index.php" <?php echo activeMenuItem('index.php'); ?>>Home</a></li>
<li><a href="resume.php" <?php echo activeMenuItem('resume.php'); ?>>Resume</a></li>
<li><a href="projects.php" <?php echo activeMenuItem('projects.php'); ?>>Projects</a></li>
<li><a href="contact.php" <?php echo activeMenuItem('contact.php'); ?>>Contact</a></li>
</ul>
Build your menu in a loop:
$menu_items = array(
'Home' => 'index.php',
'Resume' => 'resume.php'
);
<ul id="navilist">
<?php foreach ( $menu_items as $title => $href ): ?>
<li><a href="<?php echo $href; ?>" <?php echo activeMenuItem($href); ?>>
<?php echo $title; ?>
</a></li>
<?php endforeach; ?>
</ul>
You Can't get the href value using php code.
in my knowledge - one way to get the href value
You should declare the page name with one one variable like
$home="home.php";
$resume="resume.php";
.
.
.
<ul id="navilist">
<li><a href="<?php echo $home;?>" <?php echo activeMenuItem($home); ?>>Home</a></li>
<li><a href="<?php echo $resume;?>" <?php echo activeMenuItem($resume); ?>>Resume</a></li>
.
.
.
</ul>
I am not sure. Please check it.

PHP Dynamic menu highlight from database

I've been making a website for a client that is based off of 1 page and the links are to different categories or articles. I'm showing the content depending on the URL parameter like this,
<a href="index.php?cat_id=<?php echo $category['cat_id']" >
usually to do a navigation highlight depending on the page I would do something like this,
PHP
<ul>
<li <?php if($pagename == "index.php"){ echo 'class="selected"'; } ?>>
Home
</li>
<li <?php if($pagename == "about.php"){ echo 'class="selected"'; } ?>>
About
</li>
<li <?php if($pagename == "services.php"){ echo 'class="selected"'; } ?>>
Services
</li>
<ul>
but my URLS are dynamic from the database like so,
PHP
<ul>
<li>Home</li>
<?php while ($category = $statement->fetch()) { ?>
<li><?php echo $category['cat_name']; ?></li>
<?php
}
?>
</ul>
So I was wondering how can I add the class selected to the links cat_id when the URL contains that parameters id? Thanks in advance for any help!
If the category IDs do not change over time:
if($_GET['category_id'] == "1" ){ echo 'class="selected"'; }
If the categorie IDs are subject to change over time, you will need to perform a pre-query to get a map of category IDs to pages. Then you can do something like:
if($_GET['category_id'] == $categories['home_page'] ){ echo 'class="selected"'; }
<?php if($category['cat_id']==$_GET['cat_id']){ echo 'class="selected"';}?>

Highlight menu item depending on anchor selected?

I'm using the following tutorial to create a one page website:
http://www.insitedesignlab.com/how-to-make-a-single-page-website/
Is it possible to change the color of the menu item depending on what anchor is selected?
This is how I've setup the menu
<?php $current = $post->ID; $counter = 1; $mypages = get_pages(array('sort_column' => 'menu_order'));
foreach( $mypages as $page ) { ?>
<?php if ($current == $page->ID) { ?>
<li><a style="color:white!IMPORTANT;" id="menu<?php echo $counter; ?>" href="#section<?php echo $counter; ?>"><?php echo $page->post_title; ?></a></li>
<?php } else { ?>
<li><a id="menu<?php echo $counter; ?>" href="#section<?php echo $counter; ?>"><?php echo $page->post_title; ?></a></li>
<?php } ?>
<?php $counter++; } ?>
So each menu iterates by 1 each time it loops.
You can try Jquery Tabs... Maybe its easier
Sure, I think the right approach here is to bind the tabs, I mean not depending in the anchor, so everytime a tab is clicked it will change,so first you have to add a class to all tabs like so:
<li><a style="color:white!IMPORTANT;" id="menu<?php echo $counter; ?>" class="tab" href="#section<?php echo $counter; ?>"><?php echo $page->post_title; ?></a></li>
after that you just need to bind the tab elements to the click function to fire the event every time a tab is selected:
$(".tab").click(function(e){
$(".tab").css("color", ""); //Remove the color from all tabs
$(this).css("color", "white!IMPORTANT"); //Apply the "highlight" color to the selected
});
this way we remove the color from all "tab" elements, and then we apply the color to the selected tab.
It would be better if you use a css class for the link that you want to be highlighted.
CSS:
.linkactive{
color:pink;
font-weight:bold;
}
jQuery:
$("#nav a").click(){
$(".linkactive").removeClass("linkactive");
$(this).addClass("linkactive");
});
Please remember to add the class "linkactive" to your home or starting tab.

Categories