PHP wordpress if a text value is empty, echo - php

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.

Related

Data from database in href link echos site url

Using a foreach loop in codeigniter, I am trying to display website links from database in href html tags, But it echos the siteurl instead of linking to the website link.
<?php foreach ($as as $a): {
<li>
<strong>Web</strong>
<a href="<?php echo $a['website']; ?>" target="_blank" rel="nowfollow"><?
php echo $a['website']; ?></a></div>
</li>
<?php }endforeach; ?>
for example www.instagram.com was in the database it will link similar to this
localhost/site/view/www.instagram.com/p/BgHH1TyDqvp/ instead of just linking too www.instagram.com
I've noticed that if the protocol (http or https) isn't at the front of an external link then it'll handle it as a relative path. If I know there's a chance of an external link in the database I'll check for the protocol and add it if needed.
Updated Code
<?php foreach($as as $a) :
// Check if we have the protocol
$pro = (substr($a['website'], 0, 4) != 'http' ? 'https://' : '');
?>
<li>
<strong>Web</strong>
<a href="<?= $pro . $a['website'] ?>" target="_blank" rel="nofollow">
<?= $a['website'] ?>
</a>
</li>
<?php endforeach; ?>
This is based off memory and haven't tested this specific code block.
I don't claim this to be the best way to handle this, but has worked for me in the past.

Anchor Tag automatically calling href Without Click in Codeiginitor

Here in My View:
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" data- toggle="dropdown">Dropdown Example
<span class="caret"></span></button>
<ul class="dropdown-menu">
<?php
foreach($site as $sites)
{
echo '<li >"'.$sites->site_title.'" </li>';
}
?>
</ul>
</div>
<?php
}
?>
i want to redirect when user click on $sites->site_title
but how it working is it automatically redirects to url
enter code here"<li><a href='shop/viewSiteId?id=".$sites->site_id."'>".$sites->site_title."</a></li>";
and get that id by using GET method
A redirect is the programmatic way to send a browser to a URL. In other words, a call to redirect is like clicking a link. They are not intended to work together in the way you have tried.
<?php
foreach($site as $sites) : ?>
<li>
<a href='<?= base_url("shop/viewSiteId?={$sites->site_id}"); ?>'><?=$sites->site_title; ?></a>
</li>
<?php endforeach;
If you are not familiar with the syntax, know that <?= is the shortcut way to write <?php echo
I've also used PHP Alternative Syntax for Control Structures and dropped in and out of the PHP processor a bunch of times. For me, that is the easier way to read and write this kind of thing. (Your mileage may vary.)

What does data attribute stand for in anchor(<a>) tag in PHP?

I'm using Drupal CMS. I'm a newbie in Drupal and PHP.
In one of my *.tpl.php file I've a PHP code snippet as follows:
<div class="form-section">
<h3>Job Alerts: jobs delivered to your inbox! <strong>(optional) </strong> <span class="info-ico"><em><?php echo bfstring('tooltip_register_job_alerts'); ?></em></span></h3>
<?php $alerts = bevforce_get_user_option($user->uid, 'alert', false); ?>
<ul class="jobs-alerts-list">
<?php foreach ($alerts as $a) : ?>
<li><?php echo $a['value']['alert_name']; ?> edit</li>
<?php endforeach; ?>
</ul>
<p><!--<a class="popup-loader" href="<?php echo url(); ?>?bf-ajax=create-job-alert&page=register"><strong>Create Job Alert</strong></a>-->
<a class="popup-loader" href="/?bf-ajax=create-job-alert&page=register">Create New Job Alert</a>
</p>
</div>
I'm not getting what's the purpose of data-oid="<?php echo $a['oid']; ?>" in anchor tag(<a>). I've never seen such attribute anywhere in <a> tag.
Following is another code snippet from my PHP code :
<li><a class="popup-loader" href="/?bf-ajax=delete-job-alert&eid=&oid=0">Remove</a></li>
If I hover the mouse cursor over the hypertext Remove, I'm getting the following URL:
xyz.com/bf-ajax=delete-job-alert&oid=3805462
How could this happen that I'm passing the value oid=0 in query string but it is showing some different value when I hover the hypertext? Is this happening due to the data-oid attribute we used in <a> tag above?
So in short my doubt is what's the purpose of attribute data-oid in <a> tag and how the value is getting changed from the value I set in the code?
Can anyone clear my above doubts?
Thanks in advance.
The data-* attribute is primarily for JavaScript in HTML 5. See: http://html5doctor.com/html5-custom-data-attributes/
Using the jQuery library, it's really easy to reference a data attribute: $("#someLink").data("name") for something like Click Me -- the .data("name") is simply for data-name.

Can anyone suggest a way I can get my class="selected" to work on my navigation in Wordpress?

Can anyone suggest a way I can get my class="selected" to work on my navigation in Wordpress?
I have a wordpress navigation setup:
If I am on the home page, my home page class is selected.
If I want to go to page one, I want this class to change in thenavigation so only this class loads and not the home page.
I use: class="selected" to activate my roll over effect.
I can get this to work on a fixed site, just not on wordpress, any suggestions here?
<div class="nav">
<ul>
<li class="selected">Home</li>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
<li>Six</li>
</ul>
</div>
Could I use an if statement here?
<?php if(is_home() ) {
//for example load class="selected"
} else {
//for example if other page don't load class="selected"
} ?>
From what I understood, you're using a hardcoded navigation, and not wp_nav_menu() from WordPress.
So, you can conditionally check for each page you are using:
<div class="nav">
<ul>
<li<?php echo is_home()? ' class="selected"'; '';?>>
Home
</li>
<li<?php echo is_page('Contact')? ' class="selected"'; '';?>>
Contact page
</li>
<li<?php echo is_single('My first Post')? ' class="selected"'; '';?>>
Myfirst post
</li>
</ul>
</div>
You should make use of the Conditional Tags present in WordPress.
If you look at the first <li> you will see <?php echo is_home()? ' class="selected"'; '';?> This code is expressed as a ternary operator which is the equivalent of
if( is_home() ){
echo ' class="selected"'; #echo class. white space in front so that it does not stick to the "<li"
else{
echo ''; #do nothing
}
In the example above, I used three functions:
is_home() - Returns true if you are on the home page
is_page($arg) - Returns true if you are on the page specified by $arg.
is_single($arg) - Returns true if you are on the post specified by $arg.
There are other conditional tags available that you can choose to use.

suggest proper conditional statement to fade tabs with CSS via PHP

I want to change the class of two tabs via PHP, but I am stuck in basic condition. here is my code:
CSS
.myinfo { background-color:black }
.deactive { background-color : white }
HTML
<li class="myinfo <?=$deact?>">
<a href="myaccount.php?<?=$qry_str?>" >My Info</a>
</li>
<li class="myinfo <?=$deact?>">
My Contacts
</li>
What I need is
if $_GET['mycontacts'] is active
then My Info link should have class deactive
otherwise My Contacs link should have class deactive
I tried this:
if (isset($_GET['mycontacts'])){
$deact ='deactive';
}
But it did not succeed. Please help to write this condition (I think a one line ternary condition could work).
Try this:
<li class="myinfo <?php echo ($_GET['mycontacts'] === 'active' ? 'deactive' : '') ?>">
<a href="myaccount.php?<?=$qry_str?>" >My Info</a>
</li>
<li class="myinfo <?php echo ($_GET['mycontacts'] !== 'active' ? 'deactive' : '') ?>">
My Contacts
</li>
By the way I hate php short tags! Personal preference...

Categories