Solved

Wordpress Conditional Statement

Posted on 2010-11-15
8
405 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
  • 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
 
LVL 2

Expert Comment

by:gtagliani
ID: 34139875
is this code on your sidebar.php? wp version?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now