Solved

Wordpress Conditional Statement

Posted on 2010-11-15
8
411 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

717 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