I've got a var that is either empty or not and I want to display nothing when its empty. Code Snippet:
<?php
global $bp;
$user_id = $bp->displayed_user->id;
$user_info2 = get_userdata($user_id);
?>
<div class="meta">
<?php _e('Status','fisa'); ?>: <?php echo fidsa_fpsa_status($user_id); ?>
</div>
If The var is empty it still shows Status: is there someway to change this to not output Status: when the var is empty?
Assign fidsa_fpsa_status($user_id) to a variable, check if the variable is empty - if its not, print the content!
<?php
global $bp;
$user_id = $bp->displayed_user->id;
$user_info2 = get_userdata($user_id);
$fidsa_fpsa_status = fidsa_fpsa_status($user_id);
if (!empty($fidsa_fpsa_status)) {
?>
<div class="meta">
<?php _e('Status','fisa'); ?>: <?php echo $fidsa_fpsa_status; ?>
</div>
<?php
}
Related
I made a simple custom meta box plugin to display testimonials. I used a foreach loop to retrieve and display the data on my front-page.php page. However, using the exact same code on my page.php page returns
Warning: Invalid argument supplied for foreach().
What is the reason for this error and how can I fix it?
Here is the code used on both front-page.php and page.php:
<div id="testimonials">
<?php
$testimonials = get_post_meta($post->ID, "testimonials_data", true);
$counter = 0;
foreach($testimonials as $testimonial){
$counter++;
?>
<div class="testimonial <?php if($counter == 1){echo 'active-testimonial';}; ?>">
<p><?php echo $testimonial['field1']; ?></p>
<span><?php if(!empty($testimonial['field2'])){echo $testimonial['field2'];}; if(!empty($testimonial['field3'])){echo ' - ' . $testimonial['field3'];}; ?></span>
</div>
<?php }; ?>
</div>
EDIT:
To be more clear. My problem isn't that the foreach loop doesn't work at all. It works on the front-page.php but not page.php using the same code. How do I get it to work on both pages?
Your get_post_meta() $single attribute is set to true. Try setting it to false so that it always returns an array.
$testimonials = get_post_meta($post->ID, "testimonials_data", false);
get_post_meta( int $post_id, string $key = '', bool $single = false )
This function return (mixed) values
- It Will be an array if $single is false.
- It Will be value of meta data field if $single is true.
so if you have stored value in string testimonials_data except json_encoded.
then it will show you value in array only.
and apply some !empty check
<div id="testimonials">
<?php
$testimonials = get_post_meta($post->ID, "testimonials_data", true);
$counter = 0;
if(!empty($testimonials)){
foreach($testimonials as $testimonial){
$counter++;
?>
<div class="testimonial <?php if($counter == 1){echo 'active-testimonial';}; ?>">
<p><?php echo $testimonial['field1']; ?></p>
<span><?php if(!empty($testimonial['field2'])){echo $testimonial['field2'];}; if(!empty($testimonial['field3'])){echo ' - ' . $testimonial['field3'];}; ?></span>
</div>
<?php };
}?>
</div>
I'm trying to give structure to my code and i am facing a problem.
I'm looping through a sql query response and for each element i'm trying to retrieve other related elements. It works in my controller without problem but when i'm trying to repeat in the view I always get the same value for the related element
My controller:
<?php
include_once('class/guide.class.php');
$bdd = new DBHandler();
$req = guide::getGuides($bdd,0,5);
foreach ($req as $results => $poi)
{
$req[$results]['id'] = htmlspecialchars($poi['id']);;
$req[$results]['name'] = nl2br(htmlspecialchars($poi['name']));
$guide = new guide($results['name'],$bdd);
$guidePois = $guide->getGuidePois($poi['id']);
foreach ($guidePois as $res => $re)
{
echo $guidePois[$res]['id'];
echo $guidePois[$res]['name'];
$guidePois[$res]['id'] = htmlspecialchars($re['id']);
$guidePois[$res]['name'] = nl2br(htmlspecialchars($re['name']));
}
}
include_once('listing.php');
here, you see that I echo the ids/names of the related list of element and it works well, the output is correct for each element of the first list.
When i do it in my view:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
Somehow the first list output are the good elements, but for the 2nd list, i always get the related elements of the first item.
Do you have an idea ?
Thanks a lot for your help
This is because you only set:
$guidePois = $guide->getGuidePois($poi['id']);
once in the controller.
If you want it to work in the view, you need to insert this code right after the closing </h3>
<?php $guidePois = $guide->getGuidePois($poi['id']); ?>
So that $guidePois gets a new value in each iteration.
Complete view code:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php
$guidePois = $guide->getGuidePois($poi['id']);
foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
I have a file called productFocus.php where I use the GET variable ID: $id = $_GET["id"];
However this page is used in the product.php file in the following manner:
product.php
<?php
$page_content = 'productFocus.php'; // $id = $_GET["id"];
include('master.php');
?>
productFocus.php
<?php
include "db/db.php";
$id = $_GET["id"];
$product = get_product_by_id($id);
?>
<div class="product-focus">
<h3><?php echo $product->name ?></h3>
<img src="/images/products/<?php echo $product->image ?>">
<div id="description">
<h4>Productinformatie</h4>
<p><?php echo $product->description ?></p>
<h4>Partners</h4>
<table>
<?php
foreach($product->partners_obj as $partner) {
?>
<tr>
<td>
<a href=<?php echo $partner->$product_url ?> target="_blank">
<img id="partner" src="/images/partners/<?php echo $partner->image ?>">
</a>
</td>
<td>
<?php $partner->$product_price ?>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
master.php
//HTML code
...
<?php include($page_content);?>
...
//HTML code
When I browse to productFocus.php?id=324324 I can read the GET variable, but when I browse to product.php?id=324324 I do not have access to the GET variable ID.
Is there an elegant way to solve this issue?
You need to check the GET variable before you call the product details in the prodcts.php file before regular page load.
<?php
if (isset($_GET['id'])) {
// GET PRODUCT DATA
}else{
// LOAD PRODUCTS PAGE
}
?>
As for getting the product details, I would suggest writing the separate call to the database, but if you just need to load page content, you are not calling it properly:
$id = $_GET['id'];
$fileurl = 'products.php?id='.$id;
$pagecontent = file_get_contents('$fileurl');
Argh, this code is not pulling through my custom meta.
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
if (!empty($post_meta)) {
?>
<div class='client-testimonial'><?php echo $my_meta['testimonial']; ?></div>
<div class='client-name'><?php echo $my_meta['name']; ?></div>
<?php
}
?>
But the one below works, the only reason I am not using it is because it still shows the speach marks and dash when the fields are left empty in the admin panel
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
echo "<div class='client-testimonial'>". "'".$my_meta['testimonial']."'". "</div>";
echo "<div class='client-name'>". "-" .$my_meta['name']."</div>";
?>
Please help me on why the first code is not echoing the info. I am at the end of my tether!
You're checking if $post_meta is not empty, you don't have a variable named $post_meta
Change:
if (!empty($post_meta))
to
if (!empty($my_meta))
i think u have checked wrong variable.
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
if (isset($my_meta) && !empty($my_meta)) {
?>
<div class='client-testimonial'><?php echo $my_meta['testimonial']; ?></div>
<div class='client-name'><?php echo $my_meta['name']; ?></div>
<?php
}
?>
I have the following code:
<?php $buycheck = get_post_meta($post->ID, 'buy-link', true); ?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
Later in my code I want to be able to say that if buy-link does not exist - i.e. there is no data in that field - then do something, else do something different.
Not sure how to do this! Help appreciated!
(By the way, I posted this question to Wordpress Stack Exchange first. It was voted closed there because it apparently concerns PHP boolean logic more than Wordpress - https://wordpress.stackexchange.com/questions/60387/how-do-i-do-if-post-meta-does-not-exist#comment78412_60387)
<?php if($buycheck ==''){ /*stuff*/ } ?>
this will render $buycheck, and if it is empty == is equal to '' nothing.
You can set a global variable that you can check later to see if the buylink exists:
<?php
$buycheck = get_post_meta($post->ID, 'buy-link', true);
$GLOBALS['buy_link_exists'] = !empty($buycheck);
?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
Then later on in your code:
<?php if ($GLOBALS['buy_link_exists'])): ?>
it exists, do one thing
<?php else: ?>
it does not exist, do something else
<?php endif; ?>
If you need the actual value, you can set a global containing the return value from get_post_meta so you can use the actual value.