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">';
}
?>
Related
i need your help in this case; i have module in joomla and want to cutting title with specified a limit from joomla library string.php. i change this code :
<?php if ($params->get('show_title', 1)) : ?>
<h3 itemprop="name">
<?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
<?php echo $this->escape($displayData->title); ?>
<?php else : ?>
<?php echo $this->escape($displayData->title); ?>
<?php endif; ?>
</h3>
to this code :
<?php if ($params->get('show_title', 1)) : ?>
<h3 itemprop="name">
<?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($displayData->slug, $displayData->catid)); ?>" itemprop="url">
<?php
$limit =100;
if (strlen($this->item->text) > $limit) {
echo (substr($this->item->text, 0, $limit)) . " ... ";
}
else {
echo $this->escape($displayData->title); } ?></a><?php else : ?>
<?php endif; ?>
</h3>
but not work.
thanks for your attention guys.
Joomla has a built-in JHtmlString/truncate method which you can use, I've had good success using it with some of our templates and overrides.
This method would let you simplify your code and you could replace you entire last php block with something like the following
<?php
$limit =100;
echo JHTML::_('string.truncate', ($this->item->text), $limit, false, false);
?>
More about JHtmlString/truncate: https://docs.joomla.org/API16:JHtmlString/truncate
Some example code which might be helpful:
https://gist.github.com/2dpi/a540527a64f9f0093392
https://hotexamples.com/examples/-/JHtmlString/truncateComplex/php-jhtmlstring-truncatecomplex-method-examples.html
Good luck!
This is the code that shows a Manufacturer Part Number on my product listing
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
what I would like to do is not show the MPN: Field on the product page if the $text_mpn field is blank, e.g if no part number is listed.
You can put an if statement around it. You can close the PHP code block after the if, put your code inbetween and open a new PHP code block to close it:
<?php if ($text_mpn != ''){ ?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php };>
For blocks like this, it can be a bit messy and unclear to see where the block ends if you use normal curly braces, so you might consider the Alternative syntax for control structures for these cases:
<?php if ($text_mpn != ''):?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php endif;>
try this:
<?php if($mpn != ""){ ?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php } ?>
try this
<?php if(strlen($text_mpn) > 0) echo "<span class='p-rewards'>MPN:".$text_mpn."</span>".$mpn."</br>"; ?>
Case 1:
You can use empty() for check is variable is empty or not.
<?php if(empty($mpn)){ ?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php } ?>
Case :2
You can simply use display:none if you wants to hide control.you can simply add attribute of style.
<div <?php if(empty($mpn))
{ echo 'style="display:none"';}
else {echo 'style="display:block"';}
?> >
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
</div>
<?php
$mpn = 'Ram Pukar';
$text_mpn = 'Hello ';
echo $text = !empty($mpn) ? '<span class="p-rewards">MPN: '.$text_mpn.'</span>'.$mpn.'<br />':null;
?>
Output:
MPN: Hello Ram Pukar
I want an example in php with if then else, that is
lets say a variable have a value containing URL
so
if the URL starts with a cetrain URL www.blabla.com/... then do something
else
if the URL start with www.whateverurl.com/... then do something else ,,
I hope its clear enough Guys, please some help,
My PHP code with the embedded HTML is like that. How can I avoid all the cases and have just one if and one else and not have elseif?
<?php if ($item->getPrimaryLink()) : ?></br>
<?php if ($item->getPrimaryLink()->getUrl() == "http://www.blabla.com/index.php/article?id=3200") : ?></br>
<span><?php rc_e('READ_MORE'); ?></span>
<?php elseif ($item->getPrimaryLink()->getUrl() == "http://www.blabla.com/index.php/article?id=1508") : ?></br>
<span><?php rc_e('READ_MORE'); ?></span>
<?php elseif ($item->getPrimaryLink()->getUrl() == "http://www.blabla.com/index.php/article?id=1840") : ?></br>
<span><?php rc_e('READ_MORE'); ?></span>
<?php elseif ($item->getPrimaryLink()->getUrl() == "http://www.blabla.com/index.php/article?id=2541") : ?></br>
<span><?php rc_e('READ_MORE'); ?></span>
<?php else : ?></br>
<span><?php rc_e('READ_MORE'); ?></span>
<?php endif; ?>
Thank you all and any ideas or suggestions would be really appreciated.
You can use switch() statement.
switch ($item->getPrimaryLink()->getUrl()) {
case "http://www.blabla.com/index.php/article?id=3200":
echo "<a href=". $item->getPrimaryLink()->getUrl()." class='readon'><span>". rc_e('READ_MORE'). "</span></a>";
break;
case "http://www.blabla.com/index.php/article?id=1508":
echo "<a href=". $item->getPrimaryLink()->getUrl()." class='readon'><span>". rc_e('READ_MORE')."</span></a>";
break;
...
}
http://php.net/manual/en/control-structures.switch.php
Use the explode() function to get the first part of your url.
<?php
$url= $item->getPrimaryLink(); // www.blabla.com/index.php?id=123 <- The slash would be your divider in this case.
$url_ex= explode("/", $url);
// So now you have
//$url_ex[0] = 'www.blabla.com';
//$url_ex[1] = 'index.php?id=123';
?>
<?php if ($item->getPrimaryLink()) : ?></br>
<?php if ($url_ex[0] == 'www.blabla.com'){
echo "<a href=". $item->getPrimaryLink()->getUrl()." class='readon'><span>". rc_e('READ_MORE'). "</span></a>";
}
<?php else : ?></br>
<span><?php rc_e('READ_MORE'); ?></span>
<?php endif; ?>
I think you can use something similar to this.
http://php.net/manual/en/function.explode.php
I have a PHP if else statement however it does not seem to work and I'm not sure why.
Here is my code
<?php
if (has_post_thumbnail()) {
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php
the_post_thumbnail('full', array('class' => 'img-responsive alignleft'));
?>
</a>
<?php
} else () {
?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php
}
?>
And here is the error that I get syntax error, unexpected ')'
I'm not sure where the unexpected ) is coming from.
I am basing my PHP on this structure, however editing it as I would like to be able to put mine into HTML without using echo
<?php if ( '' != get_the_post_thumbnail() ) {
// some code
}
else {
// some code
}
?>
Let's try with this:
<?php if (has_post_thumbnail()): ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail('full', array('class' => 'img-responsive alignleft')); ?>
</a>
<?php else: ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php endif; ?>
I'm not sure why you wrote your code this way instead of concatenating it, but your if else statement has two parenthesis after else "()". You might want to get rid of these to fix your issue.
In PHP else is not a function, so you shouldn't call it like you did.
PHP's elseif () needs conditions,
try else instead:
<?php
if ( has_post_thumbnail() ) {
?>
<?php the_post_thumbnail( 'full', array( 'class' => 'img-responsive alignleft' ) ); ?>
<?php
} else { //instead of elseif () here, use else
?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />
<?php
}
?>
Please also refer to the WordPress Coding Standards to make your code easier readable and maintainable. And try not to edit the original code in your question without marking the edits clearly, otherwise SO users won't be able to understand your problem/help you further.
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()