Solved

Wordpress Conditional Statement

Posted on 2010-11-15
8
402 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

An earlier version of this article was originally posted on my personal blog at: ​http://www.brianjmatis.com/2014/07/07/enabling-nextprevious-post-links-in-wordpress/ Introduction In most blogs, a fairly common navigational element is to have li…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

18 Experts available now in Live!

Get 1:1 Help Now