PHP include to highlight active page - php

I am using this code in a PHP include to give a class to the active page the user is currently browsing.
<li id="nav11">
<a <?php if ($_SERVER['REQUEST_URI']=="contact") echo " class=\"actv\""; ?> href="contact">Contact</a>
</li>
My page is called http://www.domain.com/contact.php
However, nothing happens with my code above.

<li id="nav11">
<a <?php if ($_SERVER['REQUEST_URI']=="/contact.php") echo " class=\"actv\""; ?> href="contact">Contact</a>
</li>
Have you tried to change "contact" to "/contact.php"?

Related

how to load the default active class with href, while loading the page

<ul class="nav nav-pills nav-stacked project-body" style="background-color: white">
<?php foreach($get_policies as $policies) : ?>
<li role="presentation" class="<?php echo $policies['policies_id'] == $policies_id ? "active" : "" ?>"><?php echo $policies['policies_title'] ?>
</li>
<?php endforeach; ?>
</ul>
i have the above code snippet, where i can display all the policies in vertically. Now i want the first policies should be loaded to the while loading the page. make the class active by default with policies loaded.
You can use nth-child for doing this
$('.project-body li:first-child').addClass('active');
You can use jquery to check if li have active class, get href then use window location.href:
if($('.project-body li').hasClass('active'))
{
window.location.href = $(this).attr('href');
}
In php way
Assuming your URL is
<?php echo base_url().'policies/index/'.$policies['policies_id'] ?>
When you click on the <li> link, to get the polilcies_id from the URL you can use $this->uri->segment(n); // n=1 for controller, n=2 for method, etc
for example
localhost/policies/index/4 where 4 is the policies_id, this->uri->segment(3)
<li role="presentation" class="<?php echo $policies['policies_id'] == this->uri->segment(3) ? "active" : "" ?>"> YOUR LINK </li>

PHP wordpress if a text value is empty, echo

I'm building a wordpress theme. In the backend, the user has the option to enter the url for their social networks (i.e. twitter, facebook, instagram etc). These URL's are then dynamically added to images in the theme front end linking to the respective networks.
The issue I have is that if a user doesn't enter a url for a network, I don't want the image to display. I am trying to write code that says, if the url is blank, echo 'class="hidden"' - this class has display:none in the css.
here is a snippet of my php:
<ul class="icons">
<li <?php if (get_theme_mod('footer_twitter')==0) {echo 'class="hidden"'; } ?>><span class="label">Twitter</span></li>
</ul>
and the css:
ul.icons li.hidden {
display:none;
}
The php code above currently outputs the echo statement for all cases, even when a url is entered in the backend. Can anyone help me with this code
Check the return of "get_theme_mod()" You can check this by using, cause i dont think it "== 0". http://codex.wordpress.org/Function_Reference/get_theme_mod
var_dump(get_theme_mod('footer_twitter'));
//string(0)
Here is your new code:
<ul class="icons">
<li class="<?php echo empty(get_theme_mod('footer_twitter')) ? 'hidden' : ''; ?>">
<a href="<?php echo get_theme_mod('footer_twitter'); ?> " class="icon circle fa-twitter">
<span class="label">Twitter</span>
</a>
</li>
</ul>
Please check this Syntax: http://php.net/manual/en/control-structures.alternative-syntax.php its the best way to code control structures in your "View" code.
<?php if (!empty (get_theme_mod( 'set_address2' ))) {
echo get_theme_mod( 'set_address2');
}?>
This seemed to work for me in Wordpress.
Let me know if this works.

Wordpress Shortcode not working with WP Codex command and PHP

Below is a php issue I am having, it works in other ways but doesn't work the way I wish too.
Basically I wish to add the WP login and logout commands to the the A-HREF links, but the closing short code doesn't seem to be taken effect and I would like to know why and hopefully fix this problem.
<?php
echo do_shortcode('[not-level-visitors]') .
'<div id="SignUp">
<ul>
<li><img src="http://dev.universitycompare.com/wp-content/themes/blue-and-grey/images/icons/user_icon.png" alt="User Icon My Account University Compare" />
My Account
</li>
<li>
Log Out
</li>
</ul>
</div>' .
do_shortcode('[/not-level-visitors]');
?>
Basically the above code is kind of working, but I just need the closing shortcode to work as it appears in my html and is not being recognised - I have created the above code from this below snippet that I am already using that works completely:
<?php
echo do_shortcode('[level-visitors]
<div id="SignUp">
<ul>
<li>
Sign Up
</li>
<li>
Login
</li>
</ul>
</div>
[/level-visitors]');
?>
The main difference I spotted is the single call vs multiple calls to do_shortcode, I assume it's failing to match the regex.
Maybe this will work:
echo do_shortcode('[not-level-visitors]
<div id="SignUp">
<ul>
<li><img src="http://dev.universitycompare.com/wp-content/themes/blue-and-grey/images/icons/user_icon.png" alt="User Icon My Account University Compare" />
My Account
</li>
<li>
Log Out
</li>
</ul>
</div>[/not-level-visitors]');
It looks like you need to remove the extra opening/closing parentheses and extra do_shortcode call.
<?php
echo do_shortcode('[not-level-visitors]' .
'<div id="SignUp">
<ul>
<li><img src="http://dev.universitycompare.com/wp-content/themes/blue-and-grey/images/icons/user_icon.png" alt="User Icon My Account University Compare" />
My Account
</li>
<li>
Log Out
</li>
</ul>
</div>' .
'[/not-level-visitors]');
?>

How do I apply my php code to a bootstrap code?

I am trying to display something from my database on a bootstrap bar and I have no idea how to combine or implement the two. Ive made my bootstrap html's file a .php, was that the correct thing to do? Here's my code (ignore the filler words)
<ul class="nav nav-list well">
<li class="nav-header"></li>
<li class="active">HIT INFO</li>
<li>Linky link</li>
<li class="divider"></li>
<li>ANOTHER HIT INFO</li>
<li>ANOTHER LINKY LINK</li>
<li class="divider"></li>
<li>YET ANOTHER HIT</li>
<li>AAAAAnd another link</li>
</ul>
and this is the php i want to include (the code to making what i want to display is in the file)
<?php include 'one.php'; ?>
where "linky link" is, i want to display this but it didnt seem to work when i put that php right there. Another thing i want to do is when a link from the database gets displayed, it displays as a bootstrap button. i tried and am just not sure how to implement php code in my bootstrap code.
Print " <td>".$row['link'] . "</td></tr> ";
How would i add this bootstrap code to that so when a link from my database gets displayed, it gets displayed as this button
<i class="icon-heart icon-white"></i>Do this HIT!
Your code seems right to me except to your "<td> and <tr>" tag. You're not using it right.
Try this approach
In one.php
<?php
$links = array( 0 => array("url"=>"http://google.com","text"=>"Google") );
?>
In Menu
<?php include('one.php')?>
<ul>
<li>Menu one</li>
<?php foreach( $links as $link){ ?>
<li><a href="<?php print $link['url']?>"><?php print $link['text']?></li>
<?php } ?>
</ul>

converting html into php conditional statement

I am using a wordpress plugin that requires the following code in my template:
<li><a href="#"><?php if (class_exists('MultiPostThumbnails')
&& MultiPostThumbnails::has_post_thumbnail('shanti', 'two-image')) : MultiPostThumbnails::the_post_thumbnail('shanti', 'two-image', NULL, 'big');
endif; ?></a></li>
Basically, it says "if the MultiPost Thumbnails class exists, then display the 'big' image." Im not too great with PHP, but I would like to include the <li><a href="#"> within the conditional statement. Reason is because if there is no image to spit out, I dont want a blank <li> to be displayed. Any idea how to rewrite this code to include the <li>/<a> in the conditional?
Thanks
Try advanced escaping:
<?php if (class_exists('MultiPostThumbnails')
&& MultiPostThumbnails::has_post_thumbnail('shanti', 'two-image')): ?>
<li><a href="#">
<?php MultiPostThumbnails::the_post_thumbnail('shanti', 'two-image', NULL, 'big'); ?>
</a></li>
<?php endif; ?>

Categories