Have been reading through multiple similar questions and went over my syntax many times, but I can't figure out why my PHP code is executing both conditions.
I'm trying to replace the url of an element with the string from a custom field if the field is not empty. If the field is empty, I want to output the permalink normally. What happens is that the code concatenates both the string from the custom field and the permalink when the field is not empty. If i remove the string in the field, it works fine.
<div class="profile-content">
<a href="
<?php
if ( the_field('direct_resource_link') != '') {
the_field('direct_resource_link');
} else {
the_permalink($id);
} ?>
"><h5><?php the_title(); ?></h5></a>
<div class="profile-footer">
Thanks!
Dan.
EDIT after comment from original poster
My initial assessment (left below for reference) was correct. You are using function that will print/echo content instead of returning it. Your if will always evaluate to false, because you are calling function that returns nothing; and PHP thinks that nothing and empty string are the same thing.
You didn't see that when field was empty, because the_field() for empty field printed empty string (or nothing at all), i.e. it didn't modify value printed by the_permalink() in any way/
According to ACF documentation, the_field() is accompanied by get_field() which returns value instead of printing it.
Your code should look like that:
<div class="profile-content">
<a href="
<?php
if ( get_field('direct_resource_link') ) {
the_field('direct_resource_link');
} else {
the_permalink($id);
} ?>
"><h5><?php the_title(); ?></h5></a>
<div class="profile-footer">
My initial post
What happens is that you run function the_field('direct_resource_link') and compare it's return value to ''; if that value is empty, you run the_permalink($id);.
It's hard to tell what the_field() is and what it is supposed to do, but I guess that it prints value instead of returning it. So if field is empty, it prints nothing, resulting in pure run of the_permalink(). If field is not empty, it prints it content and returns nothing. Since nothing is equal to empty string, PHP proceeds with else branch and invokes the_permalink() that prints additional info.
Solution: modify the_field() to return value instead of printing it, or make additional function that will query for value and return it, and use that function in if statement.
Miroslaw Zalewski already answered your question here, so this is simply to show you the kind of code needed to fix your issue:
function get_the_field($field) {
ob_start();
the_field($field);
return ob_get_clean();
}
This code will start an output buffer (which will capture all echo'd data), run the_field and return (and delete) the output buffer (the echo'd data from the_field). This means you can simply do the following:
...
<?php
$theField = get_the_field('direct_resource_link');
if ( $theField != '') {
echo $theField;
} else {
the_permalink($id);
}
?>
...
This can all be simplified. the_field() echoes a meta value. You don't want that...Instead, you want to return the value to check it, before conditionally echoing it. You can do this using get_field().
In the simplest form, your final code would look like the following:
<a href="<?php get_field('direct_resource_link') ? the_field('direct_resource_link') : the_permalink($id); ?>">
<h5><?php the_title(); ?></h5>
</a>
Related
This question already has answers here:
Is there a difference between '<?=' and '<?php'? [duplicate]
(5 answers)
Closed last year.
I want to display only certain data from database on my page, so I end my foreach loop with if and break; statement. I checked with var_dump() and the data is being provided from database correctly, function getName() is also correct.
Moreover I inserted there a test "echo"test"; which is displaying well. But getName() is still missing.
I think that problem might be with this if statement, because when I delete it everything works correctly (except that I get data I dont want to get).
Same situation is when I insert if statement after the getName() function - this part of the code is not executed, it seems.
<?php //var_dump($leagues); ?>
<?php foreach($leagues as $leauge): ?>
<?php if($leauge->getLevel() != 'A') { break; } ?>
<li>
<i class="fas fa-long-arrow-alt-right"></i>
<?php $leauge->getName(); echo "test"; ?>
</li>
<?php endforeach; ?>
There is one echo missing.
Replace: <?php $leauge->getName(); echo "test"; ?>
With: <?php echo $leauge->getName(); ?>
Consider to use continue; instead of break;
The break statement terminates the whole loop early whereas the
continue brings the next iteration early. Reference: Geeks for Geeks
As you are using break IF $leauge->getLevel() != 'A', in case of your first iteration goes to this condition, your entire loop will terminate. Just change to continue if you wanna "skip the current league, but go to the next one and check again".
Rewriting it here so it will be more seeable. What helped me:
In this line of code
<?php $leauge->getName(); echo "test"; ?>
When i want to display results i have to use
<?= instead of <?php . Why is that i dont really know.
I'm trying to use get_field in a loop to retrieve some custom field values but
when using get_field('container', post_id) the value is always empty.
I tried to even use it inside the block file and the same happens.
$container = get_field('container'); // this works
$test_container = get_field('container', 144); //this returns empty, post id === 144
Did you try the_field() function.
In ACF the_field() function is used to display the value of a specific field. it is same as get_field().
https://www.advancedcustomfields.com/resources/the_field/
<?php if( get_field('container') ): ?>
<p><?php the_field('container', 144); ?> </p>
<?php endif; ?>
The code you using should work but if it's not working for any reason. You can also use
get_post_meta(); //to retrieve the value.
First of all this is a wordpress page so [insert_php] is a replacement for <?php.
The url of the page is as follows http://********.com/?page_id=98#highlights?sel=q1s
the code on the page is as follows
<div id="Question1" style="background-color:[insert_php] if ($_GET['sel'] == "q1s") { echo "black"; } else { echo "white"; }[/insert_php];">
this is my current code and it still showing no results, I checked the actual value I'm getting for background-color and for some reason am getting the result "white".
This is because the second parameter sel passed in the URL should be directly appended to the first argument declared with the ampersand (&) identifier.
The use of the fragment identifier (#) in the middle of your arguments is misleading as he can also be used to declare arguments to the URL. If the hash mark is meant to identify an anchor, it is better to place it at the end for avoiding errors and better readability :
http://you-url.com/?page_id=98&sel=q1s#highlights
Try something
<div id="Question1" style="background-color:[insert_php] if ($_GET['sel'] == "q1s") { echo "black"; } else { echo "white"; }[/insert_php];">
[insert_php] echo $_GET['sel'];[/insert_php]
</div>
and see what is in the $_GET
I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.
How can I show or hide div depending on result come from server, in the file that handle the request, I get a customer object from db, depending on user request, if I didn't find the corresponding customer I want to hide a div .
here's my php code
$customer=getCustomerViaCellPhone($link,$cellPhoneNo);
if(!$customer){
$error = 'No result found';
}
here's the view
<div id="customerInfo" class="<?php (is_null($customer))?'hide':''?>">
....
</div>
but it shows the div in both cases
You are missing the command echo in your example so it should be:
<div id="customerInfo" class="<?php echo (is_null($customer))?'hide':''?>">
....
</div>
obviously your css would also need to define a class called .hide that had display:hidden; in it.
According to your comment, $customer is not NULL, it's an object with NULL values in it. Which will return false for your is_null($customer) check because it's not NULLDocs - it is an object.
You can solve this by decoupling the output logic from your data a bit:
$customer=getCustomerViaCellPhone($link,$cellPhoneNo);
$hideCustomerInfo = false;
if(!$customer) {
$error = 'No result found';
$hideCustomerInfo = true;
}
and in the view then:
<div id="customerInfo" class="<?php echo $hideCustomerInfo ? 'hide' : ''; ?>">
....
</div>
In case this does not work, you can validate the $hideCustomerInfo flag already in you controller to see if it contains the value you expect. Additionally this simplifies your view.
Next to that ensure your CSS is correct as well to hide HTML elements with such a class.
<div id="customerInfo" style="display:<?php echo (is_null($customer))?'none':'block'?>">
Take a look at css' display and visibility options