Howto modify this short php-function? (Wordpress) - php

This function is part of the WMPL-Plugin (Language-Selector) for Wordpress. What I need is a extension (if / else) of this function which makes it possible to show another flag-image on active sites.
function language_selector_flags(){
$languages = icl_get_languages('skip_missing=0&orderby=code');
if(!empty($languages)){
foreach($languages as $l){
if(!$l['active']) echo '<a href="'.$l['url'].'">';
echo '<img src="'.$l['country_flag_url'].'" height="12"
alt="'.$l['language_code'].'" width="18" />';
if(!$l['active']) echo '</a>';
}
}
}
The CSS isn't a problem, but my php-skills are on a very low level. It would be great if anyone can tell me a solution. Thanks for your helping me!

I believe this should do the trick:
function language_selector_flags(){
$languages = icl_get_languages('skip_missing=0&orderby=code');
if(!empty($languages)){
foreach($languages as $l){
if(!$l['active']) echo '<a href="'.$l['url'].'">';
if($l['active']){
echo '<img src="'.ACTIVE_FLAG_URL.'" height="12" alt="'.$l['language_code'].'" width="18" />';
}else{
echo '<img src="'.$l['country_flag_url'].'" height="12" alt="'.$l['language_code'].'" width="18" />';
}
if(!$l['active']) echo '</a>';
}
}
}
You need to substitute "ACTIVE_FLAG_URL" with your own URL.

Related

i want to pass php variable through the dynamic href link image to another php page

here i want to pass a dynamic variable '$cost' to another php page 'onewaytrip_passanger_data.php', through dynamically generated href link image. The code for href link image write into php tag
This is my code. but it is not working..
<?php
$cost=$row['fare'];
echo '<img src="images/bookbutton.png" width="85" height="20" />';
?>
Change this :
echo '<img src="images/bookbutton.png" width="85" height="20" />';
to
echo '<img src="images/bookbutton.png" width="85" height="20" />';
use this instead:
echo '<img src="images/bookbutton.png" width="85" height="20" />';
echo "<img src=\"images/bookbutton.png\" width="85" height="20" />";
or
echo "<img src=\"images/bookbutton.png\" width="85" height="20" />";
Always keep yourself confuse free. What is the need to do echo anchor tag. When, it can be done like this way.
Now, try.
<?php $cost=$row['fare'];?>
<a href="onewaytrip_passenger_data.php?fare=<?echo $cost;">
<img src="images/bookbutton.png" width="85" height="20" />
</a>

Multiple page ids in an array

I'm fairly new to PHP so go easy.
<?php
$body_classes = get_body_class();
$ScanL="url";
if(in_array('parent-pageid-385', $body_classes))
{
echo '<img src="'. $ScanL.'/images/product-logos/image1.gif" />';
}
else if(in_array('parent-pageid-394', $body_classes)) {
echo '<img src="'. $ScanL.'/images/product-logos/image2.gif" />';
}
else if(in_array('parent-pageid-238', $body_classes)) {
echo '<img src="'. $ScanL.'/images/product-logos/image3.gif" />';
}
else if(in_array('parent-pageid-382', $body_classes)) {
echo '<img src="'. $ScanL.'/images/product-logos/image4.gif" />';
}
else if(in_array('page-id-391', $body_classes)) {
echo '<img src="'. $ScanL.'/images/product-logos/image5.gif" />';
}
else if (in_array('page-id-334', $body_classes)) {
echo '<img src="'. $ScanL.'/images/product-logos/image6.gif" />';
}
if(in_array('parent-pageid-385', $body_classes))
I need the above to be applied to more than one page id - I tried adding , '' but that broke the code and also tried or / ||
I'm sure I'm probably missing something obvious.
Thanks,

Handling URLs correctly with PHP

i have the following case within my plugin for wordpress: I have a details page of a database entry which can be accessed like this:
http://localhost:8888/studio/wp-admin/admin.php?page=myplugin-details&id=42
The next would be to add sorting options to this page and i solved it with this code:
The url would be:
http://localhost:8888/studio/wp-admin/admin.php?page=myplugin-details&id=42&orderby=answer&order=asc
<?php
if (!empty($_GET['orderby'])) {
$pos = strpos($_SERVER["REQUEST_URI"], 'orderby');
$url = substr($_SERVER["REQUEST_URI"], 0, $pos-1);
if ($_GET['order'] == 'desc') {
echo '<th class="sortable desc">';
echo '<a href="'.$url.'&orderby=answer&order=asc">';
} else {
echo '<th class="sortable asc">';
echo '<a href="'.$url.'&orderby=answer&order=desc">';
}
} else {
echo '<th class="sortable desc">';
echo '<a href="'.$_SERVER["REQUEST_URI"].'&orderby=answer&order=asc">';
}
?>
This works fine, but do i have to do that URL/REQUEST_URI stuff or is there a solution which is much simpler?
Thanks!
In my opinion you should not use strpos to extract the URL. Things could get funky if you made changes to your code later on. You could use str_replace instead :
if ($_GET['order'] == 'desc') {
echo '<th class="sortable desc">';
echo '<a href="'.str_replace("order=desc", "order=asc", $_SERVER["REQUEST_URI"]).'">';
} else {
echo '<th class="sortable asc">';
echo '<a href="'.str_replace("order=asc", "order=desc",$_SERVER["REQUEST_URI"]).'">';
}

IF/ELSE to echo image in PHP

I am trying to echo a specific image based on the results of a IF/ELSE statement, however I can't quite work out the phrasing of the IF/ELSE statement. I'm a relative newbie to PHP, so I'm sure that it's just a little error in the code somewhere, but if anyone could offer any assistance, I'd be grateful!
I'm currently at the stage below:
<?php
$fresh = if ($reviews['reviews']['freshness']) = 'fresh' {
echo '<img src="assets/images/fresh.png" class="rating" title="Fresh" alt="Fresh" />';
} else {
echo '<img src="assets/images/rotten.png" class="rating" title="Rotten" alt="Rotten" />';
}
?>
<?php
foreach($reviews['reviews'] as $rv){
if ($tmp++ < 10);
echo $fresh;
echo '<li>' . $rv['quote'] . '</li>';
}
?>
Thank you!
you cant assign if statement to a value.
if ($reviews['reviews']['freshness'] == 'fresh') {
echo '<img src="assets/images/fresh.png" class="rating" title="Fresh" alt="Fresh"/>';
} else {
echo '<img src="assets/images/rotten.png" class="rating" title="Rotten" alt="Rotten" />';
}
another prettier way would be:
if ($reviews['reviews']['freshness'] == 'fresh') {
$image = "fresh";
}
else {
$image = "rotten";
}
echo '<img src="assets/images/' . $image . '.png" class="rating" title="Rotten" alt="Rotten" />';
Yup, your code is pretty wrong, but I can see what you're trying to do.
<?php
if ($reviews['reviews']['freshness'] == 'fresh') {
$image = '<img src="assets/images/fresh.png" class="rating" title="Fresh" alt="Fresh" />';
} else {
$image = '<img src="assets/images/rotten.png" class="rating" title="Rotten" alt="Rotten" />';
}
?>
Your main mistake there is the incorrect positioning of brackets, and the fact that the IF statement does NOT return a value in PHP.
That said, I'm not sure why you're doing your foreach loop underneath, so I've not touched that; perhaps you could explain further what you're trying to achieve?
This might help you in the right direction:
<?php
if ($reviews['reviews']['freshness'] == 'fresh'){
echo '<img src="assets/images/fresh.png" class="rating" title="Fresh" alt="Fresh" />';
}
else{
echo '<img src="assets/images/rotten.png" class="rating" title="Rotten" alt="Rotten" />';
}
while($reviews['reviews']){
for($i=0;i<10;i++{
echo // What do you actually want to print out?
echo '<li>'.$reviews['reviews']['quote'].'</li>';
}
}
?>
I think this is what you're wanting...
<?php
for ($tmp = 0; $tmp < 10 && $tmp < count($reviews); $tmp++) {
if ($reviews[$tmp]['freshness'] == 'fresh') {
echo '<img src="assets/images/fresh.png" class="rating" title="Fresh" alt="Fresh" />';
} else {
echo '<img src="assets/images/rotten.png" class="rating" title="Rotten" alt="Rotten" />';
}
echo '<li>' . $reviews[$tmp]['quote'] . '</li>';
}
?>
ETA: Looked at the API and fixed a couple things.
ETAx2: For those wanting to see an example of the JSON return from the API...
{
"total": 41,
"reviews": [
{
"critic": "Joe Baltake",
"date": "2010-07-27",
"freshness": "fresh",
"publication": "Passionate Moviegoer",
"quote": "'Toy Story 3': Alternately affecting, hilarious and heartbreaking and the most original prison-escape movie ever made",
"links": {
"review": "http://thepassionatemoviegoer.blogspot.com/2010/07/perfectimperfect.html"
}
},
{
"critic": "Rafer Guzman",
"date": "2010-07-06",
"freshness": "fresh",
"publication": "Newsday",
"quote": "It's sadder and scarier than its predecessors, but it also may be the most important chapter in the tale.",
"links": {
"review": "http://www.newsday.com/entertainment/movies/toy-story-3-andy-grows-up-1.2028598"
}
},
{
"critic": "Richard Roeper",
"date": "2010-06-30",
"original_score": "5/5",
"freshness": "fresh",
"publication": "Richard Roeper.com",
"quote": "The best movie of the year so far.",
"links": {
"review": "http://www.richardroeper.com/reviews/toystory3.aspx"
}
},
...
Through trial and error, I found the following solution.
<?php
$ID=$row_RecordsetLast['ID'];
$image = '../../pics/'.$ID.'.jpg';
if (file_exists($image)) {
echo '<img src="../../pics/' . $ID . '.jpg" alt="" width="110" height="161" />';
} else {
echo '<img src="guest.png" alt="" width="110" height="161" />';
}
?>

displaying a post thumbnail depending on category in Wordpress?

Is it possible to display a specific post thumbnail image based on the category ID, something like this:
<?php if ( has_post_thumbnail() ) {
if ( cat = 2 ) {
echo '<img src="image1.jpg" width="" height="" class="live-holder-img" />';
} elseif( cat = 3 ) {
echo '<img src="image2.jpg" width="" height="" class="live-holder-img" />';
} else {
echo '<img src="default.jpg" width="" height="" class="default" />'
}
?>
You may want to look into category templates: http://codex.wordpress.org/Category_Templates
The quick solution would be something like this:
if (is_category('1')) {
echo '<img src="image1.jpg" width="" height="" class="live-holder-img" />';
} else if (is_category('2')) {
echo '<img src="image2.jpg" width="" height="" class="live-holder-img" />';
} else {
echo '<img src="default.jpg" width="" height="" class="default" />';
}
//you can also do this by name
if (is_category('Category A')) {
echo '<img src="image1.jpg" width="" height="" class="live-holder-img" />';
} else if (is_category('Category B')) {
echo '<img src="image2.jpg" width="" height="" class="live-holder-img" />';
} else {
echo '<img src="default.jpg" width="" height="" class="default" />';
}
is_category function reference: http://codex.wordpress.org/Function_Reference/is_category

Categories