WordPress Conditional if_page not working - php

my Conditional PHP is not working and i dont know why. Can you help me?
Here the Code:
<?php
if( is_page(array('5279','4945') ))
{
echo '<img src="http://www.example.de/logo_with_text.png">';
}
elseif( is_page(array('5656','5668','5672','5677','5682','5690','5735','5738','5741','5744','5749','5752')))
{
echo '<img src="http://www.example.de/logo_with_text_and_icon.png">';
}
else
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}
?>
But i need it in this way... (letters are the Pages ids)
For pages a,b,c,d i need a own logo
For pages e,f,g,h i need a own logo
For pages j,k,l,m i need a own logo
and for Page n,o,p,q i need a own logo

Try this code, literally just tidied it slightly and removed the quotation marks round the page numbers.
<?php
if(is_page(array(5279,4945) ))
{
echo '<img src="http://www.example.de/logo_with_text.png">';
}
elseif(is_page(array(5656,5668,5672,5677,5682,5690,5735,5738,5741,5744,5749,5752)))
{
echo '<img src="http://www.example.de/logo_with_text_and_icon.png">';
}
else
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}
?>
Just make sure you have the right pages. To do this go to pages in the dashboard and hover over the link to the page you want, you should see it shows the id number in the url, ie post=100. This is the id for that page and the one you will need to use. Using the id means that regardless of the page name, this code will always work for you.
An extreme test would also be using the below instead of the else
elseif(!is_page(array(5279,4945,5656,5668,5672,5677,5682,5690,5735,5738,5741,5744,5749,5752)))
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}

Related

Update PHP variable realtime in IF ELSE Statement(change image)

I have a Woocommerce website with a plugin so the costumer can add a product to
his/her wishlist by pressing a button. This button has an image with a white heart. After clicking the button the white heart has to be changed in a red heart. This works, but only after the page is reloaded. So I want to let change the image real-time. I know something like AJAX is needed. The name of the image is used by the variable $cls . This one is used in the src of the image.
I have the following code:
<?php
if(is_user_logged_in()) {
$hlink=get_permalink()."?add_to_wishlist=".$post_ID;
$a=1;
} else { $hlink='/login';$a=0; }
$is_in_wishlist = YITH_WCWL()->is_product_in_wishlist( $post_ID );
if($is_in_wishlist==1) {
$cls='red';
} else {
$cls='white';
}
?>
<a href="<?php echo $hlink;?>" <?php if($a==1){ ?> rel='nofollow' <?php } ?> data-product-id="<?php echo $post_ID ?>" data-product-type="simple" class='add_to_wishlist'><img class="botButton" id="changeheartcolor" src="//voice-overs.online/wp-content/themes/Impreza-child/images/voice-overs_online_heart-<?php echo $cls; ?>.svg">
How can I make it work so the heart (image) will change directly change without refreshing the page?
You need to use javascript to reaload the image, not a PHP question just change the src value of image by javascript, also preload it before change.

Avatar displayed Wordpress

I am trying to display an image on my wordpress-theme depending on which author wrote the page.
Therefore I am doing this:
<img src="<?php $autor=the_author_meta('display_name');
if (strpos($autor,'foo') !== false)
{echo esc_url(get_template_directory_uri() ); ?>/bilder/foo.jpg" />
<?php } else {echo esc_url( get_template_directory_uri() ); }
?>/bilder/fun.jpg" />
But this is throwing several errors. It's a) not pointing to the right path by not using get_template_directory_uri() at all. And it's b) adding me the Authorname within the url now like this
http://domain.com/Author%Name/bilder/fun.jpg
What am I doing wrong? And I don't want to use a plugin therefore - but thanks ;)
the_author_meta() is set to output the result, you want to just return it. So instead change it to get_the_author_meta().
Also I've tidied up your code, as I think there were some errors in it.
<img src="<?php $autor = get_the_author_meta('display_name');
if (strpos($autor,'foo') !== false) {
echo esc_url(get_template_directory_uri())."/bilder/foo.jpg";
} else {
echo esc_url(get_template_directory_uri())."/bilder/fun.jpg";
} ?>" />
When I replace foo with my user name, the image path is displayed as /bilder/foo.jpg, when I change it back to anything that isn't my username, it's shown as /bilder/fun.jpg, so I believe it works fine. The template URL appears fine too.
<img src="http://test.local/wp-content/themes/test/bilder/fun.jpg">

Hiding empty custom field for images with Anchor CMS

The explanation for this line is that Anchor doesn't display the image from the custom field correctly. So I had to add the missing part of the path. It works just fine, but now the problem is that I get horrendous icons on Safari when there is no image fetched in the field image:
<?php echo "<img src='http://www.firstpartoftheurl/" . article_custom_field('image') . "' alt=' ' height='300' >"; ?>
May I show this line only when the custom field is populated?
And how can I hide custom fields when they are empty?
This is how I solved it:
<?php
$image = article_custom_field('image');
if (!empty($image)) {
echo "<img src= 'http://www.firstpartoftheurl".article_custom_field('image')."' alt='blabla ".article_title()."'>"; //if there is image show it
} else {
//if not do nothing
} ?>
I hope it helps. It works for me but if someone has a better solution, please let us know.
Shorter still:
<?php if (article_custom_field('featured-img')) :?>
<img src="<?php echo article_custom_field('featured-img')?>" alt="<?php echo article_title(); ?>" />
<?php endif; ?>
If article_custom_field() is not returning the full URL, there might be something up with your server configuration, because this has always worked for me. Otherwise, just prepend $_SERVER['SERVER_NAME']... this is better than hard-coding the URL.
There is documentation on the function here which also explains how to use its fallback: http://anchorcms.com/docs/function-reference/articles

Display Buddypress Profile Field

Using the latest version of Wordpress & Buddypress, I am trying to display a certain custom profile field in the WP header.php. I'm terrible with PHP, but this is what I have:
<?php
global $bp;
$the_user_id = $bp->loggedin_user->userdata->ID;
if (function_exists('bp_get_profile_field_data')) {
$bp_gamertag = bp_get_profile_field_data('field=Gamertag&user_id='.bp_loggedin_user_id());
if ($bp_gamertag) {
echo '<img src="http://avatar.xboxlive.com/avatar/$bp_gamertag/avatar-body.png" alt=""/>';
}
else
echo '<img src="http://avatar.xboxlive.com/avatar/xbox/avatar-body.png" alt=""/>';
}
?>
I can't quite figure out why it isn't working. The source shows the variable still in the URL.
Also, I don't think I need the $user_user_id variable, as it isn't really being used, do I? I'm following the instructions in this topic: http://buddypress.org/support/topic/how-to-get-user-profile-data/
Try :
echo "<img src='http://avatar.xboxlive.com/avatar/$bp_gamertag/avatar-body.png' alt='' />";
Notice the use of single vs. double quotes.

Using PHP, within a search, how do I contain an html link in an image from my MySql database?

This is my problem: I have an ecommerce website, and I would like my search results to come up with products that have their links avaiable either within the image of the product on the search page, or next to the image. My current code looks like this:
if($_GET['searchBox'] !='')
{
if(mysql_num_rows($searchresult)==0) {
echo 'Your search returned no results';
}
else
{
while ($row = mysql_fetch_assoc($searchresult))]
{
echo "<img src='".$row['image']."'/>".' '.$row['name'].' £'.$row['price'].' '.$row['ProductUrl'];
}
}
}
?>
I have spent ages trying to get an URL into the image area, but I can't make it work.
Please help!!
What the problem put image in to the anchor tag.
Have you tried like this
echo "<a href='".$row['ProductUrl']."'><img src='".$row['image']."'/></a>";
EDIT
echo "<img src='".$row['image']."'/>".' '.$row['name'].' £'.$row['price'].'
<a href="'.$row['ProductUrl']."'>".$row['ProductUrl']."</a>";

Categories