Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Wordpress Conditional Statement

Posted on 2010-11-15
8
Medium Priority
?
416 Views
Last Modified: 2012-05-10
Hey Experts!!

I'm trying to incorporate some primitive functionality on my WordPress blog. Basically I want to show a certain image based on the 2nd cat_ID in the $category array.  Problem is, it's not working.
I can echo the ID I need, but my else/if isn't doing what I think it should be doing.

Any help is appreciated!
<?php 
				if($category[1]->cat_ID == '1116') //skis.com
					echo ('<a href="http://mydomain.com/">
							<img src="http://content-1.s3.amazonaws.com/images/skis/tag-cloud.jpg" alt="Save On Packages" title="Save On Packages"  width="250px"/>
						</a>');
				elseif($category[1]->cat_ID == '1452'); //snowboards.net
					echo ('Snowboards.net');			
			?>

Open in new window

//This is my echo and it prints out fine.
$category = get_the_category(); 
echo $category[1]->cat_ID;

Open in new window

0
Comment
Question by:LZ1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 2

Expert Comment

by:gtagliani
ID: 34139811
try this code:


if($category[1]->cat_ID == '1116')
{
    echo ('<a href="http://mydomain.com/"><img src="http://content-1.s3.amazonaws.com/images/skis/tag-cloud.jpg" alt="Save On Packages" title="Save On Packages"  width="250px"/></a>');

}else if($category[1]->cat_ID == '1452'){

    echo ('Snowboards.net');

}

Open in new window

0
 
LVL 30

Author Comment

by:LZ1
ID: 34139840
@gtagliani: I'm not even getting the image now on the first category.
0
 
LVL 2

Expert Comment

by:gtagliani
ID: 34139860
im sorry u must have the "$category = get_the_category();" before the code.


$category = get_the_category();

if($category[1]->cat_ID == '1116')
{
    echo ('<a href="http://mydomain.com/"><img src="http://content-1.s3.amazonaws.com/images/skis/tag-cloud.jpg" alt="Save On Packages" title="Save On Packages"  width="250px"/></a>');

}else if($category[1]->cat_ID == '1452'){

    echo ('Snowboards.net');

}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:gtagliani
ID: 34139875
is this code on your sidebar.php? wp version?
0
 
LVL 30

Author Comment

by:LZ1
ID: 34139877
I do, however it's separate from the rest of this PHP code.  I have it in it's own block.

Tried the above with no love.
0
 
LVL 30

Author Comment

by:LZ1
ID: 34139888
Yep, sidebar.php and WP version 2.8
0
 
LVL 2

Accepted Solution

by:
gtagliani earned 2000 total points
ID: 34139990
at the end of your elseif   "elseif($category[1]->cat_ID == '1452');"  u have a ";" . delete it.


Im not sure why the code i gave u is not working i tested it on a wp 2.8 and it is. See if the category id is the correct one.
0
 
LVL 30

Author Closing Comment

by:LZ1
ID: 34140054
2 things:
1) I realized it's the wrong value.
2) after removing the ; it worked fine.

Thanks!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This video teaches users how to migrate an existing Wordpress website to a new domain.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question