I have an input with PHP Theme editor for Wordpress, and save it to some variable.
I want to render (echo) out the variable if is not an empty variable.
If I don't use If, it'll work okay (it is proof the variable not empty)
<div class="role"><?php echo get_post_meta( get_the_ID(), 'hcf_single_role_1', true );?></div>
But when I want to echo the only variable with value, it works otherwise
<?php if(!empty($post_id['hcf_single_role_1'])) { ?> //if not empty
<div class="name"><?php echo get_post_meta( get_the_ID(), 'hcf_single_role_1', true );?></div>
<?php } else { ?> //if empty
Variable Empty
<?php } ?>
if I change to if(empty()) it'll just work otherwise, my variable recognized as an empty variable.
*edit:
I also tried if(!empty($_POST['hcf_single_role_1'])), but it's the same
Based on #ChrisHaas comment, I tried this method, and it works
<?php if(!empty(get_post_meta( get_the_ID(), 'hcf_single_role_1', true ))) { ?> //if not empty
<div class="name"><?php echo get_post_meta( get_the_ID(), 'hcf_single_role_1', true );?></div>
<?php } else { ?> //if empty
Variable Empty
<?php } ?>
The method before I messed up between array and string variables.
So I clean up with the same method I echoed with get_post_meta() as the if condition
Related
I tried adding HTML inside a PHP echo, and it works.
But the echo is not working if with a PHP function,
The original code:
if( ! function_exists("rframework_cart_button") ){
function rtframework_cart_button(){
$show_cart = get_theme_mod( RT_THEMESLUG.'_top_shortcut_buttons_cart' ) ? true : false ;
if( ! class_exists('Woocommerce') || ! $show_cart ){
return;
}
global $woocommerce;
echo '<li class="cart"><span class="icon-shopping-bag"><sub class="number empty">'. $woocommerce->cart->cart_contents_count .'</sub></span></li>'."\n";
}
}
The PHP code I want to put inside or after the echo is:
<?php $wishlist_count = YITH_WCWL()->count_products(); ?><span class=”your-counter-selector“><?php echo $wishlist_count; ?></span>
Can anyone help?
with echo, you need to change all the double quotes inside the html stuff to single quotes and keep double quotes outside.
Try this:
return "<li class='cart'><a href='#' class='cart-menu-button'><span class='icon-shopping-bag'><sub class='number empty'>{ $woocommerce->cart->cart_contents_count }</sub></span></a></li>'.'\n";
your question was quite confusing but I got an update based on your comment.
echo within a function called from another function will just output the echo to your page before the end of the other function, using return instead of echo will place that code snippet to the other function. it is the preferred way.
If in html, <?php functionWithEcho() ?> or <?php echo functionWithReturn()
if in php, call function normally
try this
echo "<li class='cart'><a href='#' class='cart-menu-button'><span class='icon-shopping-bag'><sub class='number empty'>'". $woocommerce->cart->cart_contents_count ."'</sub></span></a></li>.'\n'";
echo "<li class='heart'><a href='#' class='heart-button'><span class='icon-new-heart-1'><sub class='number empty'>0</sub></span></a></li>";
Update 2
I'll just leave this here for future references. But this is the solution I created with all the help I got here. Thanks!
<script>
function Refresh() {
location.reload();
}
</script>
<?php $number = $_SESSION["page_id"]; ?>
<?php
if (isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo do_shortcode('[RICH_REVIEWS_SHOW category="page" num="all" id="'. $number .'"]');
session_destroy();
echo ('<button class="btn btn-0001" onclick="Refresh()">Show All</button>');
}
else{ echo do_shortcode('[RICH_REVIEWS_SHOW num="all"]'); }
;
?>
Update
So I'm trying to just do a simple echo to see if the session is set using this code:
<?php if(isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo 'Set and not empty, and no undefined index error!');
};?>
But doing this breaks my page, I just get a blank page? How do I check if the session is set? When I do a echo of the session using this code:
<?php echo $_SESSION["page_id"]; ?>
It does output the correct session value?? What am I doing wrong?
I have a sessions saved with PHP and I'm using this so that the page ID from Wordpress is echo-ed in a shortcode do_shortcode('');
This is what my code looks like:
<?php $number = $_SESSION["page_id"]; ?>
<?php echo do_shortcode('[RICH_REVIEWS_SHOW category="page" num="all" id="'. $number .'"]'); ?>
<?php echo do_shortcode_all('[RICH_REVIEWS_SHOW category="page" num="all" id="all"]'); ?>
<?php echo $shortcode ;?>
<?php echo $shortcode_all ;?>
Now, what I would like to do is IF the page_id is not stored in the session it should echo all. So how do I go about this?
I found this code, and I think its something I need... But I'm not that great a programmer/coder...
<?php
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
?>
So, if I where to put these two together I would get something like this
<?php
$number = $_SESSION["page_id"];
// Evaluates to true because $var is empty
if (empty($number)) {
echo $shortcode_all ;
}
// Evaluates as true because $var is set
if (isset($number)) {
echo $shortcode ;
}
?>
Am I in the right direction?
Solution
<?php
if (isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo('Set and not empty, and no undefined index error!');
};
?>
What am I supposed to feel from the missing (?
If you are using wordpress then you should use the session_start(); in wp-config.php file so please first put in you wp-config.php at top and then check.
<?php
$number = $_SESSION["page_id"];
// Evaluates to true because $var is empty
if (empty($number) || $number=='') {
echo $shortcode_all ;
}else{
echo $shortcode ;
}
?>
I have an IF statement stating the following...
<?php
if (empty($data['footer_text'])) {
echo'<p>© ';
print(Date("Y"));
echo'<span class="sep"> | </span><a href="';
echo get_settings('home');
echo'" title="';
bloginfo( 'name' );
echo'" rel="home">';
bloginfo( 'name' );
echo'</a></p>';
}
else{
echo'<p>';
global $data;
echo $data['footer_text'];
echo'</p>';
}
?>
The problem I'm running into is that when I call it like this.
<p><?php global $data; echo $data['footer_text']; ?>;</p>
It displays my text correctly. But when I use the IF statement it always defaults to the showing the site name even when I know it's displaying the text correctly.
Is my syntax screwed up? I can't figure out why it thinks nothing is there but still shows up when I display in a p tag.
You don't declare $data to be global until you're INSIDE the if(), meaning that $data is undefined at the point you're doing the
if (empty($data[...])) {
you probably want
global $data;
if (empty($data[...])) {
instead.
Try to debug the variable $data['footer_text']:
<?php var_dump($data['footer_text']); ?>
just before IF statement. Remember, when empty() returns true.
Can't figure out what I'm doing incorrectly here
this is at the top of the template file
<?php
/**
* #package 1
* #since 1 1.0
*/
$source_name = get_post_meta($post->ID, 'Source Name', true);
$source_url = get_post_meta($post->ID, 'Source URL', true);
?>
here is the other part thats further down:
<?php if($source_url) { ?>
<div id="content-source">
<span>Source:</span> <?php echo $source_name; ?>
</div>
<?php } ?>
If I remove <?php if($source_url) { ?> and <?php } ?> it works fine, but how do I get it to work so if theres no source nothing will be displayed?
A quick look-up of the get_post_meta() function:
If there is nothing to return the function will return an empty array unless $single has been set to true, in which case an empty string is returned.
So, try:
<?php if($source_url <> "") { ?>
<div id="content-source">
<span>Source:</span> <?php echo $source_name; ?>
</div>
<?php } ?>
Previous you were checking if nothing is returned. You need to be checking for an empty string instead.
I display the form validation error in codeigniter as below:
<?php echo form_error('name', '<div class="form_error">', '</div>'); ?>
i want to do it so that if there is error, then it should print error, otherwise it should print the info div.
For example,
if form_error, then
<?php echo form_error('name', '<div class="form_error">', '</div>'); ?>
else
<div class="info">Your first and last name. </div>
As form_error is not just a simple variable that i can check if it is empty then print info. How can i do it? Thanks.
You can do something like this:
if ( form_error('name') )
{
echo form_error('name');
}
For form_error might not be a variable but it's a function that returns a string. If the string is empty (NULL, FALSE, "", 0, ...), the if statement will fail (meaning there is no error) and the form_error('name') won't be called.
This sould do it :
if (form_error('name')){
echo form_error('name', '<div class="form_error">', '</div>');
} else {
echo '<div class="info">Your first and last name. </div>';
}