?
Solved

Wordpress Conditional Statement

Posted on 2010-11-15
8
Medium Priority
?
412 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Read about how to approach blogging and about ways to do it right. Stand out from the crowd and let your knowledge be consumed by a large audience. This article aims to explain how your blog should look like,  the most important things to do while b…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

752 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