PHP if statement, variable not displaying - php

I have the following code:
<?php if(the_field('required_libraries') ) { echo 'Title' . $required_libraries; } ?>
The field does exist and it displays correctly. However, the 'Title' text does not.
This works for me where there aren't any variables, so I don't quite understand why it isn't working here.

I'm not sure I understand your question. You might need to use isset in your if statement.
<?php
if (isset($required_libraries)) {
echo 'Title' . $required_libraries;
}
?>
Can you post what your "the_field" function does?
Update
Per the documentation you provided, it looks like you should use get_field() in the if statement (not the_field()).
<?php
if(get_field('required_libraries')) {
echo 'Title ' . get_field('required_libraries');
}
?>
It looks like the_field() echos the field value, so you could also do this:
<?php
if(get_field('required_libraries')) {
echo 'Title ';
the_field('required_libraries');
}
?>

Related

Php and Html code within echo

I'm writing an if statement in which a button needs to show if the cart is empty.
For this button I need to get the form key of the product for the data-url
So something like this:
Order
As mentioned above I need to wrap this button in an if statement, so something like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()){
echo 'Order';
}
else{
'<p>hello</p>';
}
?>
But obviously I can't have php echo within echo. Can anybody point me in the right direction of how to do this?
You don't put PHP inside HTML inside PHP. Since you're already in the context of PHP code, just concatenate the values you want to the output:
echo 'Order';
The resulting output is always just a string. You can simply build that string with whatever values you have.
You can just use string concatenation:
echo '<a href="#" data-url=".../' . Mage::getSingleton(...) . '"' ...
Simply don't open PHP up again. You can terminate the HTML interpretation inside an echo.
Your code should look like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()) {
echo 'Order';
}
else {
'<p>hello</p>';
}
?>

Using Meta Box Plugin to assign URL to a variable

I'm a little lost here, hoping that someone can help. I'm using the Meta Box plugin for WordPress, and I'm trying to create a process for the user to select an option from a predefined list, and then assign a URL to that option as a link. Im trying to define the URL in a variable, and then call it in a function, but I'm still a little green on PHP syntax. this is my code now:
<?php
$article_url= rwmb_meta('orion_2016_article_url', 'type=URL');
if (rwmb_meta('orion_2016_article_source') != '') {
echo '<a href= ("$article_url") target=blank>';
echo rwmb_meta('orion_2016_article_source');
echo '</a>';} ?> on <?php the_date(); ?>
Since the options are already predefined, it seems like assigning a random URL to one of the options should be pretty simple. Hopefully this makes sense!
You need to to place variables you wish to echo inside double quotes or simply concatenate strings using . as in my example. Note that I didn't check the plugin's specific syntax, only general PHP syntax.
<?php
$article_url= rwmb_meta( 'orion_2016_article_url', 'type=URL' );
if (rwmb_meta('orion_2016_article_source') != '') {
echo '' . rwmb_meta( 'orion_2016_article_source' ); . '';
} ?> on <?php the_date(); ?>

Advanced Custom Fields in Wordpress - "if" statement troubles

I was hoping somebody may be able to help me. I'm having trouble with if statements with the Advanced Custom Fields plugin for Wordpress. I've got three options the user can choose from, all three can be chosen, but they can also choose just one if they wish.
The issue I'm having is the code I've written displays all of the HTML tags, even the empty ones. This is causing styling issues. I want to be able to just show HTML that has been populated. I've tried the solutions on the ACF forums but to no avail.
Link: http://www.advancedcustomfields.com/resources/getting-started/code-examples/
Here's the quick (newbie!) code I've got at the minute:
<?php the_sub_field('link'); ?>
<?php the_sub_field('doc'); ?>
<p><?php the_sub_field('cap'); ?></p>
I looked on the ACF forum and tried this, but it broke the theme:
<?php if(the_sub_field('link')) {
echo '' . the_sub_field('link') . '';
} ?>
<?php if(the_sub_field('doc')) {
echo '' . the_sub_field('doc') . '';
} ?>
<?php if(the_sub_field('cap')) {
echo '<p>' . the_sub_field('cap') . '</p>';
} ?>
I'm looking for some help to make this work. I don't think I'm too far away from the right answer, however I'm a bit of a rookie with anything beyond standard front-end stuff, any thoughts would be very much appreciated.
Thanks!
Try to use get_sub_field();
<?php if(get_sub_field('link')) {
echo '' . the_sub_field('link') . '';
} ?>
<?php if(get_sub_field('doc')) {
echo '' . the_sub_field('doc') . '';
} ?>
<?php if(get_sub_field('cap')) {
echo '<p>' . the_sub_field('cap') . '</p>';
} ?>
When looping through one of these fields, this function returns a sub field from the current row.
Like Dk-Macadamia said, try to use get_sub_field() in loops instead of the_sub_field()
the difference is get_sub_field() return the value as a string, and the_sub_field() print the data,
Also get_sub_field() only work under a repeater/ fluid field type otherwise wont work,
if its not a sub field of repeater/fluid fields just try get_field()

Extracting basic PHP echo from IF statement

The following code is for a Wordpress plugin, it displays points and tank of a user:
<?php
if(function_exists('cp_displayPoints') && $authordata->ID){
echo '<span class="cubepoints_buddypress">'; cp_displayPoints($authordata->ID); echo '</span>';
if(function_exists('cp_module_ranks_getRank')) echo ' <span class="cupepoints_buddypress_rank">'.cp_module_ranks_getRank($authordata->ID).'</span>';
}
?>
I am trying to extract these two echo functions from the If statement but only succeeded with one of them. I can echo the points like this:
<?php cp_displayPoints($authordata->ID); ?>
Works fine. Now I tried doing the same with the second echo:
<?php cp_module_ranks_getRank($authordata->ID); ?>
But it did not work. Obviously, there is some basic thing that I am missing here. Do you know what it is?
The first one likely prints directly to output, while the second returns its value. So, you need to echo() the second one, just as they're doing in your sample code:
<?php echo cp_module_ranks_getRank($authordata->ID); ?>

php within a php statement

Using Wordpress, i have a plugin that inserts a playable MP3 on the page.
To call that, along with the track details, this code is inserted;
<?php if (function_exists("insert_audio_player")) {
insert_audio_player("[audio:http://thewebsite.com/thetrack.mp3|artists=Artist|titles=Titles]");
} ?>
I would like to make this editable from the backend easily, by entering some meta-data. So this;
<?php meta('track-url'); ?>
Along with other various details would replace those that are above.
Unfortunately for me, this;
<?php if (function_exists("insert_audio_player")) {
insert_audio_player("[audio:<?php meta('track-url'); ?>|artists=Jack Presto|titles=Track 1]");
} ?>
obviously does not work! This is down to my lack of understanding if PHP - can anyone help?
Cheers!
Simple! Do this:
<?php if (function_exists("insert_audio_player")) {
$trackUrl = meta('track-url');
insert_audio_player("[audio:$trackUrl|artists=Jack Presto|titles=Track 1]");
} ?>
I can't tell if the meta() function prints to the screen or returns a string. If it returns a string, do:
<?php
if (function_exists("insert_audio_player")) {
insert_audio_player('[audio:' . meta('track-url') . '|artists=Jack Presto|titles=Track 1]');
}
?>
If it prints to the screen, it's a bit more complicated. Ideally, you would have a function that DOES return a string, but as a quick hack (if you're only getting paid for a quick fix) you could do something like
<?php
if (function_exists("insert_audio_player")) {
ob_start();
meta('track-url');
$meta = ob_get_contents();
ob_end_clean();
insert_audio_player("[audio:$meta|artists=Jack Presto|titles=Track 1]");
}
?>

Categories