Div not showing in wordpress - php

<?php
if (is_user_logged_in()) {
echo '<div id="signin-box"> ' . wp_login_form() . ' </div>';
} else {
echo 'hi';
}
?>
Is what I've got. The login form is working, but it is not being wrapped in the div. Without the else/if statement, it works.

Does this work for you?
<?php
if (is_user_logged_in()) {
?>
<div id="signin-box">
<?= wp_login_form(); ?>
</div>
<?php
} else {
echo 'hi';
}
?>

http://codex.wordpress.org/Function_Reference/is_user_logged_in states it returns true if logged in and false if not.
You are probably looking for:
if(!is_user_logged_in()){
echo '<div id="signin-box">' . wp_login_form(array('echo' => false)) . '</div>';
}else{
echo 'hi';
}
Adding ! inverses true to false so if user is not logged in show login form else say hi. Also adding array('echo' => false) will give a return instead of echoing, removing array('echo' => false) you'll have to put wp_login_form() on its own line without a echo:
if(!is_user_logged_in()){
echo '<div id="signin-box">';
wp_login_form();
echo '</div>';
}else{
echo 'hi';
}

You want to add this code it seems work like this please add this code and check it.
<?php
if (is_user_logged_in()) { ?>
<div id="signin-box" class="test"><?php wp_login_form()?> </div>
<?php } else {
echo 'hi';
}
?>
In your code "echo" is not working in login form ..

Related

How to I fix this if/else tree? PHP

I am trying to program a sub-system for students on my website, I tried using these if/else statements to determine some outputs if the parameters match with the user information they have. So if a user is a student they can contact other people that are students and cant contact other users that are not students, and vice versa. The code I tried is below. The problem is that the code shows the error message and the message buttons at the same time whether or not the user is or is not a student. TIP: This PHP code is in a .phtml file, hence the amount of PHP opening and closing tags.
<?php if($dc['user']['student'] !== 0){ ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == true){ ?>
<div class="user-button user-follow-button"><?php echo Dc_GetFollowButton($dc['popover']['user_id']); ?></div>
<div class="user-button message-button"><?php echo Dc_GetMessageButton($dc['popover']['user_id']); ?></div>
<?php } ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == false) { ?>
<?php echo $dc['lang']['student_contact_warning']; ?>
<?php } ?>
<?php } ?>
<?php if($dc['user']['student'] !== 1){ ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == false){ ?>
<div class="user-button user-follow-button"><?php echo Dc_GetFollowButton($dc['popover']['user_id']); ?></div>
<div class="user-button message-button"><?php echo Dc_GetMessageButton($dc['popover']['user_id']); ?></div>
<?php } ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == true) { ?>
<?php echo $dc['lang']['student_contact_warning']; ?>
<?php } ?>
<?php } ?>
Do yourself a favour and start to use variables for repeated functions, and look up PHP's HereDoc to allow you to simplify your code. I think you'll agree the following is clearer.
The question now is what values you expect in your conditions. Run the following and check that the outputted IsStudent and Student are what you expect.
<?php
$Student=$dc['user']['student'];
$IsStudent=Dc_IsStudent($dc['popover']['user_id']);
$Dc_FollowBut=Dc_GetFollowButton($dc['popover']['user_id']);
$Dc_MessageBut=Dc_GetMessageButton($dc['popover']['user_id']);
echo"IsStudent: $IsStudent<br>";
echo"Student: $Student<br>";
if($Student!=0){
if($IsStudent==true){
echo<<<EOC
<div class="user-button user-follow-button">$Dc_FollowBut</div>
<div class="user-button message-button">$Dc_MessageBut</div>
EOC;
}else{
echo $dc['lang']['student_contact_warning'];
}
}
if($Student!=1){
if($IsStudent==false){
echo<<<EOC
<div class="user-button user-follow-button">$Dc_FollowBut</div>
<div class="user-button message-button">$Dc_MessageBut</div>
EOC;
}else{
echo $dc['lang']['student_contact_warning'];
}
}
?>
<?php
if($dc['user']['student'] != 0){
if(Dc_IsStudent($dc['popover']['user_id']) == true){
echo '<div class="user-button user-follow-button">'. Dc_GetFollowButton($dc['popover']['user_id']) .'</div>';
echo '<div class="user-button message-button">'. Dc_GetMessageButton($dc['popover']['user_id']) .'</div>';
} else {
echo $dc['lang']['student_contact_warning'];
}
} else if($dc['user']['student'] != 1){
if(Dc_IsStudent($dc['popover']['user_id']) == false){
echo '<div class="user-button user-follow-button">'. Dc_GetFollowButton($dc['popover']['user_id']) .'</div>';
echo '<div class="user-button message-button">'. Dc_GetMessageButton($dc['popover']['user_id']) .'</div>';
} else {
echo $dc['lang']['student_contact_warning'];
}
}
?>
Try putting it in one block, rather than 2 seperate blocks. If-else blocks are meant like that. I would prefer using cases here, though i tried cleaning this up a bit for you to see whats going on.
I agree with #jbes in the other awnser that you should use variables, to make things a lot easier to read. i didn't do this, but i totally agree on that part.
A typo can be made quickly if you constantly run only if blocks, with else, you can point to a different actions in the same if, without closing the complete block. Now I don't know if $dc['user']['student'] can only be 0 or 1, but else you can toss away the complete else-if statement and just change it to else to make it more cleaner. Also to check if not 0 leaves open for a lot of interpretation, what if the value is 2 or 48644. it would always trigger both blocks, so you may want to swap it around to check if the value equals 1 or 0.
I hope this points you in the right direction
I would first make the PHP code more clear. Then you will see that you didn't close some tags.
<?php
if ($dc['user']['student'] !== 0) {
if (Dc_IsStudent($dc['popover']['user_id']) == true) {
echo '<div class="user-button user-follow-button">';
echo Dc_GetFollowButton ($dc['popover']['user_id']);
echo '</div>' . "\n";
echo '<div class="user-button message-button">';
echo Dc_GetMessageButton($dc['popover']['user_id']);
echo '</div>' . "\n";
} else {
echo $dc['lang']['student_contact_warning'];
}
}
if ($dc['user']['student'] !== 1){
if (Dc_IsStudent($dc['popover']['user_id']) == false){
echo '<div class="user-button user-follow-button">';
echo Dc_GetFollowButton($dc['popover']['user_id']);
echo '</div>' . "\n";
echo '<div class="user-button message-button">';
echo Dc_GetMessageButton($dc['popover']['user_id']);
echo '</div>' . "\n";
} else {
echo $dc['lang']['student_contact_warning'];
}
}
?>
Now we can start analyzing the code.

PHP Echo Empty Check with Photos

i have issue with my php code .
First i have Database with Photo link then i add this with echo .
I Fill the Database with /upload/photo-1.png or what ever from the User !
<?php echo$photo; ?>
I add this with :
<?php if(!empty($photo)) {echo 'uploads/photodefault.png'; } ?>
you can write like this
<?php if(!empty($photo))
{
echo $photo;
}
?>
Edit 1:
If photo is not empty then it will show link.
Change your code to this
<?php
if(empty($photo)) {
echo "uploads/photodefault.png";
} else {
echo $photo;
}
?>
With an <img> tag example
<img src="<?php if(empty($photo)) { echo "uploads/photodefault.png"; } else { echo $photo; } ?>">
try the following code
if ($photo != ''){
echo $photo;
}else{
echo "uploads/photodefault.png";
}
<?php if(!empty($photo)) { echo $photo; } else { echo 'default/default_large.png'; } ?>
This fixed my issue.

How to add a else command in php?

I have an attribute called "Merchant_Name".
The following code will place the "Merchant_Name" Just below the product name in product page.
Now I need to add an else command to this code like this:
If Merchant_Name is there then it should be "By Merchant_Name" else "XXXXX".
Hope you get me...
<div class="Merchant_Name">
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){?>By <?php echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product); } ?>
</div>
Thanks in Advance...
You mean, with an if/else?
<div class="Merchant_Name">
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name) {
echo "By " . $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo "By XXXXX";
}
?>
Or maybe this would be cleaner:
<?php
if($_product->getAttributeText('merchant_name')) {
$name = $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
$name = "XXXXX";
}
?>
<div class="Merchant_Name">By <?php echo $name ?>
Once you start the php tag try to finish your task. Don't switch in and out again and again.
<div class="Merchant_Name">
By
<?php
if ($_product->getAttributeText('merchant_name')){
echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo 'XXXXX';
}
?>
</div>
It is pretty simple and it would be simpler if you didn't switch in and out of PHP so often:
<div class="Merchant_Name">
By
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){
echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo 'XXXXX';
}
?>
</div>

Multiple if else

How can i show some default code only if BOTH if statements are false ?
I've got this code -
<?php if( get_sub_field('cta_phone')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_phone');?>"><?php the_sub_field('cta_phone');?></a></span>
<?php else: ?><?php endif; ?>
<?php if( get_sub_field('cta_mobile')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_mobile');?>"><?php the_sub_field('cta_mobile');?></a></span>
<?php else: ?><?php endif; ?>
<?php else: ?>
<img src="http://my-domain/image.jpg">
<?php endif; ?>
i'm not using the else bit at the end at the moment because i only want that to show if both 'if's are false ?
hope that it makes sense
You can use 1 php tag only to make your syntax much more readable and
avoiding
multiple <?php ?> tags.
you can also use echo to print your html markup using php scripting like this.
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a></span>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a></span>';
} else {
//do what you want to do here, if they are false.
}
Ok thanks , the 'else' works if neither of the if's are true , but if i have one that is true or both are true , it only displays the first entry but shows it twice ?
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a>';
} else {
echo '<img src="http://placehold.it/350x150">';
}
?>

If statement to not show div / h2

This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>

Categories