If Else On Php Wordpress - php

<?php
$genre = get_the_term_list($post->ID, 'genre', '<span class="genre">', ', ', '</span>');
$tags = get_the_term_list($post->ID, 'tag-series-movies','<span class="genre">', ', ', '</span>');
if($genre,$tags)
echo '<li>Genre: '. $genre . '</li>';
echo '<li>Tags: '. $genre . '</li>';
else {
echo '';
}
?>
Hi. Guys whats wrong of my code? though the code is working 50%. the $genre is working but the else is not working. please help. i want to show the nothing when no genres is not available.

Change if($genre == $genre) to if($genre){
get_the_term_list returns the list or false so you just need to check that $genre is a truthy value
In your current code when $genre is false you are checking if(false == false) which always returns true
EDIT:
you can simplify your code like this
if($genre){
echo '<li>Genre: '. $genre . '</li>';
}
if($tags){
echo '<li>Tags: '. $tags . '</li>';
}
There's no need for the else statement as you're not echoing anything

Related

Showing specific information of current user logged in, in PHP

I'm using this 2 plugins
http://www.wpexplorer.com/wordpress-user-contact-fields/
and
WooCommerce
The 1st parameter, in this case the number "1" refers to the id of the user, how con I change it to be dynamic? So, depending on the user I get its own specific information
<h2>Personal</h2>
<?php
echo '<ul>';
echo '<li>Direccion: ' .get_user_meta(1,'address',true) . '</li>';
echo '<li>CompaƱia: ' .get_user_meta(1,'company',true) . '</li>';
echo '<li>Birth dAte: ' .get_user_meta(1,'birth',true) . '</li>';
echo '<li>Gender: ' .get_user_meta(1,'gender',true) . '</li>';
echo '<li>phone: ' .get_user_meta(1,'phone',true) . '</li>';
echo '</ul>';
?>
thanks
Don't try to make a function do something it wasn't intended to do. Write your own. Especially for something this simple.
function getUserStuff($id, $item){
$item = mysql_real_escape_string($item);
$id = mysql_real_escape_string($id);
$q = mysql_query("SELECT `".$item."` FROM `users` WHERE `id` = '".$id."'");
$z = mysql_fetch_assoc($q);
return (mysql_num_rows($q) > 0) ? $z[$item] : false;
}
This is just an example. I used a deprecated function for simplicity but you should use a different API.

WordPress Custom Field conditional based on postmeta value

I've got a chunk of code that's working fine, but I need to alter it.
It works but only if there's a value in the DB. If not, it produces a value of 0.0 which is ok...but I want it to say "N/A" as opposed to 0.0.
How would I alter the following block of code so it would display a value of "N/A" if the value was 0.0 and otherwise function as it already does?
<?php
global $wp_query;
$postid = $wp_query->post->ID;
if( get_post_meta($postid, 'Rankextras', true)) {
echo '<span class="skill-bg" data-percent=" ' . get_post_meta($postid, 'Rankextras', true) . '%" style="width: ' . get_post_meta($postid, 'Rankextras', true) . '%"> </span><span class="skill-name"> Extras</span><span class="skill-progress">' . get_post_meta($postid, 'Rankextras', true) . '</span>';
}
?>
Wordpress is already going to return an empty string if the metadata does not exist. Based on the code that you posted, could it be that there is some javascript that formats empty data-percent attributes to be 0.0?
Try this (modify the block where it echos 'N/A' to your needs):
global $wp_query;
$postid = $wp_query->post->ID;
$value = get_post_meta($postid, 'Rankextras', true);
if(!$value OR $value == '0.0')
{
echo 'N/A';
}
else
{
echo '<span class="skill-bg" data-percent=" ' . $value . '%" style="width: ' . $value . '%"> </span><span class="skill-name"> Extras</span><span class="skill-progress">' . $value . '</span>';
}
I also refactored your code a bit, so it uses less queries.

How to print PHP line inside echo

I'm creating a Dynamic Navigation using PHP, and i want to insert a php code inside a php code
on an <li>, i want to insert this line of code inside a list item
<?php printf('<li' if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';>); printf('%s %s </a></li> ', $row['name'],$row['DESCRIPTION']); } ?>
Wouldn't be better this way (?):
$content = "<li ";
if (strpos($_SERVER['PHP_SELF'], 'index.php')){
$content .= "class='current'";
}
$content .= "> </a></li>" . $row['name'] . " " . $row['description'];
Try this way:
<?php
$index = ((strpos($_SERVER['PHP_SELF'], 'index.php')) ? true : false);
printf('<li' . (($index) ? 'class="current"' : '' ) . '%s %s </a></li> ', $row['name'],$row['DESCRIPTION']);
?>
<?php
printf('<li' . (strpos($_SERVER['PHP_SELF'], 'index.php')) ? 'class="current"' : '' . '>');
printf('%s %s </a></li> ', $row['name'], $row['DESCRIPTION']);
?>
$is_current = '';
if(strpos($_SERVER['PHP_SELF'], 'index.php')){ $is_current = 'class="current"'; }
printf('<li'.$is_current.'>%s %s </a></li>', $row['name'],$row['DESCRIPTION']);
Not totally clear what you are asking, but I guess you can get what you want writing it differently, e.g.
<?php
echo "<li ";
if (strpos($_SERVER['PHP_SELF'], 'index.php')) {
echo ' class="current"';
}
// missing: '><a ...>'
printf(' %s %s </a></li> ', $row['name'], $row['DESCRIPTION']);
?>
(there's a final } without an opening {, I've just stripped it, without asking myself if it had to be the closing } of the missing opening { of the if).
Or you can use the ?: operator.
For good pratices and best identification in your code, try this:
if (strpos($_SERVER['PHP_SELF'], 'index.php')){
$class = "";
}else{
$class = "current";
}
printf('<li class="current"> %s %s </a></li>', $row['name'], $row['DESCRIPTION']);

Avoiding line breaks in HTML output of PHP

Here I have my print function:
<?php
// little helper function to print the results
function printTag($tags) {
foreach($tags as $t) {
echo '<span class="' . $t['tag'] . '">';
echo $t['token'] . "/" . $t['tag'];
echo '</span>';
echo " ";
}
}
$tagger = new PosTagger('lexicon.txt');
?>
And here is what I'm outputting from an HTML form:
<?php
if($_POST['submitbutton'] == "Submit") {
//Check whether the form has been submitted
$tags = $tagger->tag($_POST['texttotag']);
printTag($tags);
}
?>
My problem is, the output in the browser results in strange line breaks in the middle of some of my <span> like so:
<span class="VB">Enter/VB</span> <span class="PRP$">your/PRP$</span> <span class="NN
">text/NN
</span> <span class="TO">to/TO</span> <span class="NN">tag/NN</span> <span class="RB
">here/RB
</span>
This means my CSS definitions don't apply to the "interrupted" spans. Any idea why this is happening and how I can stop it? I've had a good look around and haven't been able to find cause/solution. Thanks.
It seems that your $t['tag'] variable includes a line-break.
You can get rid of that using trim($t['tag']) instead.
It appears that the $t['tag'] in printTag function contains line break character. You can use str_replace function to remove these characters like:
function printTag($tags) {
foreach($tags as $t) {
echo '<span class="' . $t['tag'] . '">';
$ttag = str_replace('\n','',$t['tag']);
$ttag = str_replace('\r','',$ttag);
echo $t['token'] . "/" . $ttag;
echo '</span>';
echo " ";
}
}

PHP code problem

I'm trying to insert the following code below into the other piece of code where it says INSERT PHP CODE HERE.. but I don't now how to correctly do it?
The code I want to insert.
if (!empty($f)) {
echo $f;
} else if(!empty($u)) {
echo $u;
} else {
echo 'someting';
}
Here is the other part of the code.
if(!empty($avatar)){
echo '<li>' . $avatar . '<div>INSERT PHP CODE HERE..</div></li>';
}
$href='';
if (!empty($f)) {
$href=$f;
} else if(!empty($u)) {
$href=$u;
} else {
$href='someting';
}
if(!empty($avatar)){
echo '<li>' . $avatar . '<div>'.$href.'</div></li>';
}
You don't need to one-line everything. Break it up into separate statements.
if(!empty($avatar)){
echo '<li>' . $avatar . '<div><a href="#">';
INSERT PHP CODE HERE..
echo '</a></div></li>';
}
Just add it in like you would a variable:
echo '<li>' . $avatar . '<div>'. phpfunction() .'</div></li>';
I realized after writing this that you can just use the ternary operator:
echo '<li>' . $avatar . '<div>'. (!empty($f)?$f:(!$empty($u)?$u:'something')) .'</div></li>';
It's significantly harder to read though, so I don't think I'd recommend it.

Categories